# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
868759 | prohacker | Power Plant (JOI20_power) | C++14 | 0 ms | 348 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const int N = 2010;
const int INF = INT_MAX;
const int mod = 1e9+7;
int n,ans,dp[N];
vector<int> adj[N];
string s;
void dfs(int u, int pre) {
dp[u] = 0;
int res = 0;
for(int v:adj[u]) {
if(v == pre) {
continue;
}
dfs(v,u);
res += dp[v];
}
dp[u] = max(res-(s[u] == '1'),(s[u] == '1' ? 1 : 0));
if(pre == 0) {
int cnt = 0;
for(int v:adj[u]) {
if(v == pre) {
continue;
}
if(dp[v] > 0) {
cnt++;
}
}
ans = max(ans,dp[u]+(cnt == 1));
}
}
signed main()
{
if (fopen("solve.inp", "r")) {
freopen("solve.inp", "r", stdin);
freopen("solve.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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;
for(int i = 1 ; i <= n ; i++) {
if(s[i] == '1') {
dfs(i,0);
}
}
cout << ans;
return 0;
}
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... |