제출 #683868

#제출 시각아이디문제언어결과실행 시간메모리
683868abc864197532Power Plant (JOI20_power)C++17
0 / 100
3 ms5032 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
const int N = 200000;

vector <int> adj[N];
int sz[N];
string s;
int par[N];

void dfs(int v, int pa) {
	sz[v] = s[v] == '1';
	par[v] = pa;
	for (int u : adj[v]) if (u != pa) {
		dfs(u, v);
		sz[v] += sz[u];
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int n;
	cin >> n;
	for (int i = 0, u, v; i < n - 1; ++i) {
		cin >> u >> v, --u, --v;
		adj[u].pb(v), adj[v].pb(u);
	}
	cin >> s;
	dfs(0, -1);
	int tot = sz[0];
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		int cnt = 0;
		for (int j : adj[i]) if (j != par[i]) {
			cnt += sz[j] > 0;
		}
		cnt += tot - sz[i] > 0;
		if (cnt >= 2 && s[i] == '1') {
			cnt--;
		}
		ans = max(ans, cnt);
	}
	cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...