Submission #543315

# Submission time Handle Problem Language Result Execution time Memory
543315 2022-03-30T07:15:20 Z 8e7 Power Plant (JOI20_power) C++17
100 / 100
170 ms 27636 KB
//Challenge: Accepted
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> adj[maxn];
int a[maxn], sum[maxn], dp[maxn];

void dfs(int n, int par) {
	for (int v:adj[n]) {
		if (v != par) {
			dfs(v, n);
			sum[n] += dp[v];
		}
	}
	if (a[n]) dp[n] = max(1, sum[n] - 1);
	else dp[n] = sum[n];
	debug(n, dp[n], sum[n]);
}
void move(int u, int v) {
	sum[u] -= dp[v];
	if (a[u]) dp[u] = max(1, sum[u] - 1);
	else dp[u] = sum[u];

	sum[v] += dp[u];
	if (a[v]) dp[v] = max(1, sum[v] - 1);
	else dp[v] = sum[v];
}

int ans = 0;
void reroot(int n, int par) {
	ans = max(ans, dp[n]);	
	debug(n, dp[n], sum[n]);
	for (int v:adj[n]) {
		if (v != par) {
			move(n, v);
			reroot(v, n);
			move(v, n);
		}
	}
}
int main() {
	io
	int n;
	cin >> n;
	for (int i = 0;i < n - 1;i++) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	string s;
	cin >> s;
	int tot = 0;
	for (int i = 0;i < n;i++) {
		a[i+1] = s[i] - '0';	
		tot += s[i] - '0';
	}
	ans = min(tot, 2);
	dfs(1, 0);
	debug();
	reroot(1, 0);	
	cout << ans << endl;
}

Compilation message

power.cpp: In function 'void dfs(int, int)':
power.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
power.cpp:34:2: note: in expansion of macro 'debug'
   34 |  debug(n, dp[n], sum[n]);
      |  ^~~~~
power.cpp: In function 'void reroot(int, int)':
power.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
power.cpp:49:2: note: in expansion of macro 'debug'
   49 |  debug(n, dp[n], sum[n]);
      |  ^~~~~
power.cpp: In function 'int main()':
power.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
power.cpp:77:2: note: in expansion of macro 'debug'
   77 |  debug();
      |  ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5024 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 4 ms 5024 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4944 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5024 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 4 ms 5024 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4944 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5024 KB Output is correct
22 Correct 4 ms 5036 KB Output is correct
23 Correct 4 ms 5032 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5040 KB Output is correct
27 Correct 4 ms 5028 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 4 ms 5204 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5036 KB Output is correct
32 Correct 4 ms 5080 KB Output is correct
33 Correct 5 ms 5036 KB Output is correct
34 Correct 5 ms 5076 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 4 ms 5076 KB Output is correct
39 Correct 4 ms 5076 KB Output is correct
40 Correct 4 ms 5164 KB Output is correct
41 Correct 4 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 4 ms 5080 KB Output is correct
44 Correct 4 ms 5164 KB Output is correct
45 Correct 4 ms 5164 KB Output is correct
46 Correct 4 ms 5076 KB Output is correct
47 Correct 4 ms 5076 KB Output is correct
48 Correct 4 ms 5164 KB Output is correct
49 Correct 4 ms 5204 KB Output is correct
50 Correct 4 ms 5084 KB Output is correct
51 Correct 4 ms 5168 KB Output is correct
52 Correct 4 ms 5076 KB Output is correct
53 Correct 4 ms 5076 KB Output is correct
54 Correct 3 ms 5088 KB Output is correct
55 Correct 4 ms 5076 KB Output is correct
56 Correct 3 ms 5076 KB Output is correct
57 Correct 4 ms 5212 KB Output is correct
58 Correct 4 ms 5076 KB Output is correct
59 Correct 4 ms 5076 KB Output is correct
60 Correct 4 ms 5168 KB Output is correct
61 Correct 3 ms 5076 KB Output is correct
62 Correct 4 ms 5164 KB Output is correct
63 Correct 4 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5024 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 4 ms 5024 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4944 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5024 KB Output is correct
22 Correct 4 ms 5036 KB Output is correct
23 Correct 4 ms 5032 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5040 KB Output is correct
27 Correct 4 ms 5028 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 4 ms 5204 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5036 KB Output is correct
32 Correct 4 ms 5080 KB Output is correct
33 Correct 5 ms 5036 KB Output is correct
34 Correct 5 ms 5076 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 4 ms 5076 KB Output is correct
39 Correct 4 ms 5076 KB Output is correct
40 Correct 4 ms 5164 KB Output is correct
41 Correct 4 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 4 ms 5080 KB Output is correct
44 Correct 4 ms 5164 KB Output is correct
45 Correct 4 ms 5164 KB Output is correct
46 Correct 4 ms 5076 KB Output is correct
47 Correct 4 ms 5076 KB Output is correct
48 Correct 4 ms 5164 KB Output is correct
49 Correct 4 ms 5204 KB Output is correct
50 Correct 4 ms 5084 KB Output is correct
51 Correct 4 ms 5168 KB Output is correct
52 Correct 4 ms 5076 KB Output is correct
53 Correct 4 ms 5076 KB Output is correct
54 Correct 3 ms 5088 KB Output is correct
55 Correct 4 ms 5076 KB Output is correct
56 Correct 3 ms 5076 KB Output is correct
57 Correct 4 ms 5212 KB Output is correct
58 Correct 4 ms 5076 KB Output is correct
59 Correct 4 ms 5076 KB Output is correct
60 Correct 4 ms 5168 KB Output is correct
61 Correct 3 ms 5076 KB Output is correct
62 Correct 4 ms 5164 KB Output is correct
63 Correct 4 ms 5076 KB Output is correct
64 Correct 155 ms 16748 KB Output is correct
65 Correct 143 ms 16784 KB Output is correct
66 Correct 147 ms 16776 KB Output is correct
67 Correct 140 ms 16736 KB Output is correct
68 Correct 139 ms 16884 KB Output is correct
69 Correct 151 ms 16648 KB Output is correct
70 Correct 140 ms 16768 KB Output is correct
71 Correct 139 ms 16764 KB Output is correct
72 Correct 141 ms 16684 KB Output is correct
73 Correct 167 ms 26928 KB Output is correct
74 Correct 170 ms 26764 KB Output is correct
75 Correct 132 ms 16616 KB Output is correct
76 Correct 143 ms 16676 KB Output is correct
77 Correct 145 ms 16648 KB Output is correct
78 Correct 147 ms 16556 KB Output is correct
79 Correct 142 ms 16664 KB Output is correct
80 Correct 157 ms 16620 KB Output is correct
81 Correct 113 ms 17000 KB Output is correct
82 Correct 132 ms 16868 KB Output is correct
83 Correct 152 ms 20892 KB Output is correct
84 Correct 163 ms 22552 KB Output is correct
85 Correct 155 ms 22240 KB Output is correct
86 Correct 150 ms 17744 KB Output is correct
87 Correct 144 ms 17264 KB Output is correct
88 Correct 142 ms 17828 KB Output is correct
89 Correct 134 ms 17976 KB Output is correct
90 Correct 146 ms 17700 KB Output is correct
91 Correct 138 ms 17856 KB Output is correct
92 Correct 133 ms 18148 KB Output is correct
93 Correct 129 ms 19920 KB Output is correct
94 Correct 120 ms 21420 KB Output is correct
95 Correct 123 ms 20208 KB Output is correct
96 Correct 134 ms 18912 KB Output is correct
97 Correct 130 ms 17580 KB Output is correct
98 Correct 124 ms 17208 KB Output is correct
99 Correct 139 ms 17032 KB Output is correct
100 Correct 132 ms 19732 KB Output is correct
101 Correct 134 ms 18028 KB Output is correct
102 Correct 135 ms 17400 KB Output is correct
103 Correct 139 ms 17212 KB Output is correct
104 Correct 75 ms 17616 KB Output is correct
105 Correct 163 ms 24292 KB Output is correct
106 Correct 69 ms 17504 KB Output is correct
107 Correct 71 ms 17472 KB Output is correct
108 Correct 166 ms 27396 KB Output is correct
109 Correct 69 ms 17552 KB Output is correct
110 Correct 160 ms 27636 KB Output is correct
111 Correct 124 ms 16664 KB Output is correct