이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define F first
#define S second
#define all(a) begin(a), end(a)
using namespace std;
int const N = 200005;
int n;
vector<int> adj[N];
string s;
void input() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a; --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin >> s;
}
int ans = 0;
int dp(int ver, int par) {
int ret = s[ver] == '1';
int suma = 0;
for (auto u : adj[ver]) if (u != par) {
int a = dp(u, ver);
if (a > 0) suma += a;
if (s[ver] == '1') {
ans = max(ans, a + 1);
}
}
ret = max(ret, suma - (s[ver] == '1'));
ans = max(ans, ret);
return ret;
}
void solve() {
dp(0, -1);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
input();
solve();
return 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... |