# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1127369 | Peter2017 | Power Plant (JOI20_power) | C++20 | 121 ms | 26104 KiB |
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define mem(a, b) memset(a, b, sizeof(a))
#define name "test"
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
int n;
int ans;
int f[N];
string s;
vector<int> adj[N];
void load(){
cin.tie(0)->sync_with_stdio(0);
if (fopen(name".inp", "r")){
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
}
void input(){
cin >> n;
for (int i = 1; i < n; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
s = " " + s;
}
void dfs(int u, int pre){
int sum = 0;
int x = (s[u] == '1');
f[u] = x;
for (int v : adj[u]) if (v != pre){
dfs(v, u);
maxi(ans, f[v] + x);
sum += f[v];
}
maxi(f[u], sum - x);
maxi(ans, f[u]);
}
void solve(){
dfs(1, 0);
cout << ans;
}
int main(){
load();
input();
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |