# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
293186 | CaroLinda | Power Plant (JOI20_power) | C++14 | 216 ms | 27148 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 pb push_back
#define ll long long
#define pii pair<int,int>
#define mk make_pair
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define debug printf
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
#define sz(x) (int)(x.size())
const int MAXN = 2e5+10 ;
using namespace std ;
int N ;
int dp[MAXN][2] ;
vector<int> adj[MAXN] ;
bool isPower[MAXN] ;
char str[MAXN] ;
void dfs(int x, int dad)
{
int mx[2] = {0,0} ;
int soma1 = 0 ;
for(auto e : adj[x] )
{
if(e == dad) continue ;
dfs(e,x) ;
lp(i,0,2) mx[i] = max(mx[i] , dp[e][i]) ;
soma1 += dp[e][1] ;
}
if(!isPower[x])
{
dp[x][0] = max( soma1, mx[0] ) ;
dp[x][1] = soma1 ;
}
else
{
dp[x][0] = max( mx[1] + 1 , soma1 - 1 ) ;
dp[x][0] = max( dp[x][0] , mx[0] ) ;
dp[x][1] = max( 1 , soma1 - 1 ) ;
}
}
int main()
{
scanf("%d", &N) ;
for(int i = 1 , u ,v ; i < N ; i++ )
{
scanf("%d%d", &u, &v ) ;
adj[u].pb(v) ;
adj[v].pb(u) ;
}
scanf("%s", str ) ;
lp(i,0,N) isPower[i+1] = ( str[i] == '1' ) ;
dfs(1,-1) ;
printf("%d\n" , dp[1][0]) ;
}
/*
6
2 1
2 3
2 4
2 5
2 6
111111
6
2 1
2 3
2 4
2 5
2 6
011111
5
1 2
2 3
3 4
4 5
11100
6
2 1
2 3
2 4
2 5
2 6
010101
16
14 10
14 13
14 16
13 11
11 12
13 15
13 5
16 3
16 8
16 1
12 4
4 7
10 2
7 6
16 9
1111111111111111
10
1 4
2 4
3 4
4 5
5 7
7 6
7 8
7 9
7 10
1111111111
*/
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... |