//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int get() {
char ch;
cin >> ch;
return int(ch - '0');
}
#define ONLINE_JUDGE
void solve() {
int n;
cin >> n;
vector <int> vec(n);
for(int &i : vec) {
i = get();
}
vector <int> adj[n +1];
for(int i = 1; i <= n -1; i++) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
function <int(int, int)> dfs = [&](int node, int par) -> int {
if(adj[node].size() >= 2) {
return 1;
}
int res = 1;
for(int &child : adj[node]) {
if(child != par) {
res = max(res, dfs(child, node) +1);
}
}
return res;
};
int mn = n +1;
for(int i = 1; i <= n; i++) {
if(adj[i].size() == 1) {
mn = min(mn, dfs(i, 0) +1);
}
}
cout << n * 2 - mn;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
89 ms |
23744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
125 ms |
28500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |