Submission #1254086

#TimeUsernameProblemLanguageResultExecution timeMemory
1254086sinatbtfardPower Plant (JOI20_power)C++20
100 / 100
139 ms27280 KiB
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int maxn = 2e5 + 10;

int n;
vector <int> adj[maxn];
string s;

void read (){
	cin >> n;
	for (int x, y, i = 0; i < n - 1; i++){
		cin >> x >> y;
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	cin >> s;
}

int ans, dp[maxn];

void dfs (int v = 1, int p = 0){
	int sum = 0;
	for (auto u : adj[v]){
		if (u == p) continue;
		dfs(u, v);
		if (s[v - 1] == '1')
			ans = max(ans, dp[u] + 1);
		sum += dp[u];
	}
	dp[v] = max(sum - (s[v - 1] == '1' && sum > 0), (int)(s[v - 1] == '1'));
	ans = max(ans, dp[v]);
//	cout << v << " " << dp[v] << '\n';
}

void solve (){
	dfs();
}

void print (){
	if (s != string(n, '0'))
		ans = max(ans, 1ll);
	cout << ans;
}

int32_t main (){
	ios_base::sync_with_stdio(0), cin.tie(0);
	read();
	solve();
	print();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...