# UVa 699 - The Falling Leaves --- # 題目大意 給一個二元樹的前序式,要求從左到右輸出垂直節點的權值和 --- # 題解 直接用preorder邊輸入邊加總即可。 要注意輸出格式最後不能有空格。還有,我原本想先建圖在跑一遍的,後來才發現節點的值可能很大QQ --- ```=C++ #include <bits/stdc++.h> #define ll long long #define pb push_back #define pf push_front #define ft first #define sec second #define pr pair<int,int> #define ISCC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; int t ,n ,m ,ans ,sum[1005] ,st = 500 ,L ,R ,cas; int sol(int pos) { int now; cin >> now; if(now!=-1) { sum[pos] += now; L = min(L ,pos); R = max(R ,pos); sol(pos-1); sol(pos+1); } return now; } int main() { L = 1e9; R = 0; while(sol(st) != -1) { printf("Case %d:\n%d" ,++cas ,sum[L]); for(int i=L+1 ;i<=R ;i++) cout << ' ' << sum[i]; cout << "\n\n"; memset(sum,0,sizeof(sum)); L = 1e9; R = 0; } return 0; } ```