이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define gcd __gcd
#define endl '\n'
const int N=200050,M=1000000007;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll n, u, v, cnt[N], ans;
bool plant[N], visited[N];
char c;
vector<vector<ll>> adj;
ll dfs(ll u){
	visited[u]=1;
	ll res=0, sum=0;
	for(auto v:adj[u]){
		if(!visited[v]){ 
			dfs(v);
			cnt[u]+=cnt[v];
			res=max(res, cnt[v]);
		}
	}
	ans=max(ans, res+plant[u]);
	// cout<<u<<" "<<ans<<" "<<cnt[u]<<" ";
	cnt[u]=max(cnt[u]-plant[u], 1LL*plant[u]);
	// cout<<u<<" "<<cnt[u]<<endl;
	return res;
}
signed main(){
	ios_base::sync_with_stdio(NULL); 
	cin.tie(nullptr); cout.tie(nullptr);
	cin>>n;
	adj.assign(n+1, {});
	for(ll i=1; i<n; ++i){
		cin>>u>>v;
		adj[u].emplace_back(v);
		adj[v].emplace_back(u);
	}
	for(ll i=1; i<=n; ++i){
		cin>>c;
		plant[i]=c-'0';
		// if(plant[i]) cout<<i<<" ";
	}
	// cout<<endl;
	dfs(1);
	cout<<max(ans, cnt[1]+plant[1]);
    return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
power.cpp: In function 'll dfs(ll)':
power.cpp:20:12: warning: unused variable 'sum' [-Wunused-variable]
   20 |  ll res=0, sum=0;
      |            ^~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |