Submission #438709

# Submission time Handle Problem Language Result Execution time Memory
438709 2021-06-28T14:08:50 Z soroush Power Plant (JOI20_power) C++17
100 / 100
950 ms 26336 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 2e5 + 10;
const ll mod = 1e9+7;

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n;
string s;
vector < int > adj[maxn];

int sz[maxn];
bool hide[maxn];

void plant(int v){
	sz[v] = 1, hide[v] = 1;
	for(auto u : adj[v])if(!hide[u])
		plant(u), sz[v] += sz[u];
	hide[v] = 0;
}

int cen(int v , int n , int par = 0 , bool found = 0){
	while(!found){
		found = 1;
		for(auto u : adj[v])if(!hide[u] and u^par and sz[u] * 2 > n){
			found = 0 , par = v , v = u;
			break;
		}
	}
	return v;
}

int dfs(int v){
	int res = 0;
	hide[v] = 1;
	for(int u : adj[v])if(!hide[u])
		res += dfs(u);
	hide[v] = 0;
	if(s[v] == '1')
		res = max(res - 1 , 1);
	return res;
}

int Solve(int v){
	hide[v] = 1;
	int ans = 0;
	if(s[v] == '1'){
		int mx = 0 , sum = 0;
		for(int u : adj[v])if(!hide[u]){
			int res = dfs(u);
			mx = max(mx , res);
			sum += max(res , 0);
		}
		ans = max(ans, mx + 1);
		ans = max(ans, sum - 1);
	}
	else{
		for(int u : adj[v])if(!hide[u])
			ans += max(0 , dfs(u));
	}
	return ans;
}

int solve(int v = 0){
	plant(v);
	v = cen(v , sz[v]);
	int ans = Solve(v);
	for(auto u : adj[v])if(!hide[u])
		ans = max(ans, solve(u));
	return ans;
}

int32_t main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1 ; i < n ; i ++){
		int u , v;
		cin >> u >> v;
		u -- , v --;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	cin >> s;
	cout << solve();
	return(0);
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5020 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5020 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 4940 KB Output is correct
22 Correct 6 ms 5092 KB Output is correct
23 Correct 5 ms 5068 KB Output is correct
24 Correct 7 ms 5068 KB Output is correct
25 Correct 5 ms 5100 KB Output is correct
26 Correct 5 ms 5068 KB Output is correct
27 Correct 6 ms 5068 KB Output is correct
28 Correct 5 ms 5068 KB Output is correct
29 Correct 5 ms 5196 KB Output is correct
30 Correct 5 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 5 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 5 ms 5068 KB Output is correct
35 Correct 5 ms 5068 KB Output is correct
36 Correct 5 ms 5080 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 5 ms 5068 KB Output is correct
39 Correct 6 ms 5068 KB Output is correct
40 Correct 7 ms 5068 KB Output is correct
41 Correct 5 ms 5068 KB Output is correct
42 Correct 7 ms 5068 KB Output is correct
43 Correct 6 ms 5068 KB Output is correct
44 Correct 4 ms 5068 KB Output is correct
45 Correct 5 ms 5068 KB Output is correct
46 Correct 5 ms 5112 KB Output is correct
47 Correct 5 ms 5092 KB Output is correct
48 Correct 4 ms 5068 KB Output is correct
49 Correct 4 ms 5068 KB Output is correct
50 Correct 4 ms 5068 KB Output is correct
51 Correct 4 ms 5128 KB Output is correct
52 Correct 4 ms 5068 KB Output is correct
53 Correct 5 ms 5068 KB Output is correct
54 Correct 4 ms 5068 KB Output is correct
55 Correct 4 ms 5068 KB Output is correct
56 Correct 4 ms 5020 KB Output is correct
57 Correct 5 ms 5196 KB Output is correct
58 Correct 4 ms 5068 KB Output is correct
59 Correct 4 ms 5068 KB Output is correct
60 Correct 6 ms 5068 KB Output is correct
61 Correct 4 ms 5068 KB Output is correct
62 Correct 5 ms 5068 KB Output is correct
63 Correct 4 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5020 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 4940 KB Output is correct
22 Correct 6 ms 5092 KB Output is correct
23 Correct 5 ms 5068 KB Output is correct
24 Correct 7 ms 5068 KB Output is correct
25 Correct 5 ms 5100 KB Output is correct
26 Correct 5 ms 5068 KB Output is correct
27 Correct 6 ms 5068 KB Output is correct
28 Correct 5 ms 5068 KB Output is correct
29 Correct 5 ms 5196 KB Output is correct
30 Correct 5 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 5 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 5 ms 5068 KB Output is correct
35 Correct 5 ms 5068 KB Output is correct
36 Correct 5 ms 5080 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 5 ms 5068 KB Output is correct
39 Correct 6 ms 5068 KB Output is correct
40 Correct 7 ms 5068 KB Output is correct
41 Correct 5 ms 5068 KB Output is correct
42 Correct 7 ms 5068 KB Output is correct
43 Correct 6 ms 5068 KB Output is correct
44 Correct 4 ms 5068 KB Output is correct
45 Correct 5 ms 5068 KB Output is correct
46 Correct 5 ms 5112 KB Output is correct
47 Correct 5 ms 5092 KB Output is correct
48 Correct 4 ms 5068 KB Output is correct
49 Correct 4 ms 5068 KB Output is correct
50 Correct 4 ms 5068 KB Output is correct
51 Correct 4 ms 5128 KB Output is correct
52 Correct 4 ms 5068 KB Output is correct
53 Correct 5 ms 5068 KB Output is correct
54 Correct 4 ms 5068 KB Output is correct
55 Correct 4 ms 5068 KB Output is correct
56 Correct 4 ms 5020 KB Output is correct
57 Correct 5 ms 5196 KB Output is correct
58 Correct 4 ms 5068 KB Output is correct
59 Correct 4 ms 5068 KB Output is correct
60 Correct 6 ms 5068 KB Output is correct
61 Correct 4 ms 5068 KB Output is correct
62 Correct 5 ms 5068 KB Output is correct
63 Correct 4 ms 5068 KB Output is correct
64 Correct 483 ms 12664 KB Output is correct
65 Correct 446 ms 15360 KB Output is correct
66 Correct 544 ms 15344 KB Output is correct
67 Correct 488 ms 15364 KB Output is correct
68 Correct 450 ms 15340 KB Output is correct
69 Correct 513 ms 15332 KB Output is correct
70 Correct 483 ms 15348 KB Output is correct
71 Correct 553 ms 15376 KB Output is correct
72 Correct 638 ms 15364 KB Output is correct
73 Correct 948 ms 25460 KB Output is correct
74 Correct 863 ms 25352 KB Output is correct
75 Correct 611 ms 15200 KB Output is correct
76 Correct 551 ms 15300 KB Output is correct
77 Correct 672 ms 15272 KB Output is correct
78 Correct 547 ms 15260 KB Output is correct
79 Correct 698 ms 15244 KB Output is correct
80 Correct 559 ms 15232 KB Output is correct
81 Correct 199 ms 15612 KB Output is correct
82 Correct 259 ms 15504 KB Output is correct
83 Correct 766 ms 19508 KB Output is correct
84 Correct 781 ms 21088 KB Output is correct
85 Correct 847 ms 20796 KB Output is correct
86 Correct 745 ms 16436 KB Output is correct
87 Correct 579 ms 15940 KB Output is correct
88 Correct 482 ms 16452 KB Output is correct
89 Correct 416 ms 16400 KB Output is correct
90 Correct 444 ms 16332 KB Output is correct
91 Correct 453 ms 16468 KB Output is correct
92 Correct 390 ms 16772 KB Output is correct
93 Correct 530 ms 20008 KB Output is correct
94 Correct 546 ms 20944 KB Output is correct
95 Correct 550 ms 18876 KB Output is correct
96 Correct 451 ms 17520 KB Output is correct
97 Correct 393 ms 16344 KB Output is correct
98 Correct 378 ms 15924 KB Output is correct
99 Correct 428 ms 15712 KB Output is correct
100 Correct 461 ms 18276 KB Output is correct
101 Correct 548 ms 16680 KB Output is correct
102 Correct 404 ms 16000 KB Output is correct
103 Correct 449 ms 15908 KB Output is correct
104 Correct 158 ms 16176 KB Output is correct
105 Correct 890 ms 22812 KB Output is correct
106 Correct 149 ms 16056 KB Output is correct
107 Correct 121 ms 16140 KB Output is correct
108 Correct 950 ms 26036 KB Output is correct
109 Correct 108 ms 16244 KB Output is correct
110 Correct 913 ms 26336 KB Output is correct
111 Correct 324 ms 15252 KB Output is correct