#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
vector<int> adj[MAXN];
char c[MAXN];
int dpf(int node, int prev) {
int sum = 0;
int mn = 1e9;
for(int x: adj[node]) {
if(x != prev) {
int d = dpf(x, node);
sum += d;
mn = min(mn, d);
}
}
if(prev == -1 && (mn == 0 || adj[node].size() == 1)) {
return sum + 1;
}
if(c[node] == '0') {
return sum;
}
else {
return max(1ll, sum - 1);
}
}
void solve(int tc) {
int N;
cin >> N;
for(int i=1; i<N; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
if(N == 1) {
cout << "1\n"; return;
}
for(int i=1; i<=N; i++) cin >> c[i];
int ans = 0;
for(int i=1; i<=N; i++) {
if(c[i] == '1') {
ans = max(ans, dpf(i, -1));
}
}
cout << ans << "\n";
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7368 KB |
Output is correct |
7 |
Correct |
5 ms |
7300 KB |
Output is correct |
8 |
Correct |
5 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7392 KB |
Output is correct |
10 |
Correct |
4 ms |
7368 KB |
Output is correct |
11 |
Correct |
6 ms |
7244 KB |
Output is correct |
12 |
Incorrect |
5 ms |
7364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7368 KB |
Output is correct |
7 |
Correct |
5 ms |
7300 KB |
Output is correct |
8 |
Correct |
5 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7392 KB |
Output is correct |
10 |
Correct |
4 ms |
7368 KB |
Output is correct |
11 |
Correct |
6 ms |
7244 KB |
Output is correct |
12 |
Incorrect |
5 ms |
7364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7368 KB |
Output is correct |
7 |
Correct |
5 ms |
7300 KB |
Output is correct |
8 |
Correct |
5 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7392 KB |
Output is correct |
10 |
Correct |
4 ms |
7368 KB |
Output is correct |
11 |
Correct |
6 ms |
7244 KB |
Output is correct |
12 |
Incorrect |
5 ms |
7364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |