#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int INF = 1e9;
vector<int> adj[200200];
int dp[200200][3];
char on[200200];
void dfs(int s, int pa = -1){
//printf("IN: %d\n", s);
dp[s][1] = 0, dp[s][2] = -INF;
for (auto &v:adj[s]) if (v!=pa){
dfs(v, s);
}
///Case 1-1. 루트가 꺼져있고, 2개이상 서브트리에서 선택
int tmp = 0, val = 0;
if (on[s]=='1') tmp = -1;
for (auto &v:adj[s]) if (v!=pa){
val += max(dp[v][1], dp[v][2] - 2);
}
dp[s][1] = max(dp[s][1], val + tmp);
///Case 1-2. 루트가 꺼져있고, 서브트리 1개에서 선택
for (auto &v:adj[s]) if (v!=pa){
dp[s][1] = max(dp[s][1], dp[v][1]+tmp);
dp[s][2] = max(dp[s][2], dp[v][2]+tmp);
}
///Case 2. 루트가 켜져있을 때 (강제로 서브트리 1개에서만 선택)
if (on[s]=='1'){
dp[s][1] = max(dp[s][1], 1); ///루트만 선택
for (auto &v:adj[s]) if (v!=pa){
dp[s][2] = max(dp[s][2], max(dp[v][1]+1, dp[v][2]+1-2));
}
}
dp[s][0] = max(dp[s][1], dp[s][2]);
//printf("%d: %d %d\n", s, dp[s][1], dp[s][2]);
//printf("OUT: %d\n", s);
}
int main(){
int n;
scanf("%d", &n);
for (int i=0;i<n-1;i++){
int x, y;
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
scanf("%s", on+1);
dfs(1);
int ans = 0;
for (int i=1;i<=n;i++) ans = max(ans, dp[i][0]);
printf("%d\n", ans);
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
power.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
power.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%s", on+1);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4912 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4912 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5196 KB |
Output is correct |
30 |
Correct |
3 ms |
5184 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5060 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
4 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5068 KB |
Output is correct |
47 |
Correct |
3 ms |
5068 KB |
Output is correct |
48 |
Correct |
3 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 |
3 ms |
5068 KB |
Output is correct |
52 |
Correct |
4 ms |
5068 KB |
Output is correct |
53 |
Correct |
3 ms |
5068 KB |
Output is correct |
54 |
Correct |
3 ms |
5068 KB |
Output is correct |
55 |
Correct |
4 ms |
5068 KB |
Output is correct |
56 |
Correct |
3 ms |
5068 KB |
Output is correct |
57 |
Correct |
3 ms |
5224 KB |
Output is correct |
58 |
Correct |
3 ms |
5068 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
3 ms |
5068 KB |
Output is correct |
61 |
Correct |
3 ms |
5068 KB |
Output is correct |
62 |
Correct |
3 ms |
5196 KB |
Output is correct |
63 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4912 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5196 KB |
Output is correct |
30 |
Correct |
3 ms |
5184 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5060 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
4 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5068 KB |
Output is correct |
47 |
Correct |
3 ms |
5068 KB |
Output is correct |
48 |
Correct |
3 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 |
3 ms |
5068 KB |
Output is correct |
52 |
Correct |
4 ms |
5068 KB |
Output is correct |
53 |
Correct |
3 ms |
5068 KB |
Output is correct |
54 |
Correct |
3 ms |
5068 KB |
Output is correct |
55 |
Correct |
4 ms |
5068 KB |
Output is correct |
56 |
Correct |
3 ms |
5068 KB |
Output is correct |
57 |
Correct |
3 ms |
5224 KB |
Output is correct |
58 |
Correct |
3 ms |
5068 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
3 ms |
5068 KB |
Output is correct |
61 |
Correct |
3 ms |
5068 KB |
Output is correct |
62 |
Correct |
3 ms |
5196 KB |
Output is correct |
63 |
Correct |
3 ms |
5068 KB |
Output is correct |
64 |
Correct |
149 ms |
13816 KB |
Output is correct |
65 |
Correct |
155 ms |
13796 KB |
Output is correct |
66 |
Correct |
140 ms |
13832 KB |
Output is correct |
67 |
Correct |
123 ms |
13856 KB |
Output is correct |
68 |
Correct |
151 ms |
13904 KB |
Output is correct |
69 |
Correct |
139 ms |
13888 KB |
Output is correct |
70 |
Correct |
123 ms |
13820 KB |
Output is correct |
71 |
Correct |
118 ms |
13880 KB |
Output is correct |
72 |
Correct |
127 ms |
13800 KB |
Output is correct |
73 |
Correct |
165 ms |
26436 KB |
Output is correct |
74 |
Correct |
161 ms |
26420 KB |
Output is correct |
75 |
Correct |
182 ms |
13716 KB |
Output is correct |
76 |
Correct |
128 ms |
13708 KB |
Output is correct |
77 |
Correct |
131 ms |
13760 KB |
Output is correct |
78 |
Correct |
131 ms |
13852 KB |
Output is correct |
79 |
Correct |
126 ms |
13680 KB |
Output is correct |
80 |
Correct |
126 ms |
13764 KB |
Output is correct |
81 |
Correct |
136 ms |
14212 KB |
Output is correct |
82 |
Correct |
110 ms |
14016 KB |
Output is correct |
83 |
Correct |
143 ms |
18976 KB |
Output is correct |
84 |
Correct |
149 ms |
21064 KB |
Output is correct |
85 |
Correct |
143 ms |
20896 KB |
Output is correct |
86 |
Correct |
136 ms |
15232 KB |
Output is correct |
87 |
Correct |
129 ms |
14568 KB |
Output is correct |
88 |
Correct |
117 ms |
15116 KB |
Output is correct |
89 |
Correct |
122 ms |
15044 KB |
Output is correct |
90 |
Correct |
144 ms |
14908 KB |
Output is correct |
91 |
Correct |
115 ms |
15132 KB |
Output is correct |
92 |
Correct |
115 ms |
15468 KB |
Output is correct |
93 |
Correct |
149 ms |
17852 KB |
Output is correct |
94 |
Correct |
123 ms |
19512 KB |
Output is correct |
95 |
Correct |
118 ms |
18192 KB |
Output is correct |
96 |
Correct |
125 ms |
16464 KB |
Output is correct |
97 |
Correct |
120 ms |
14948 KB |
Output is correct |
98 |
Correct |
118 ms |
14348 KB |
Output is correct |
99 |
Correct |
125 ms |
14276 KB |
Output is correct |
100 |
Correct |
125 ms |
17396 KB |
Output is correct |
101 |
Correct |
128 ms |
15300 KB |
Output is correct |
102 |
Correct |
125 ms |
14620 KB |
Output is correct |
103 |
Correct |
123 ms |
14428 KB |
Output is correct |
104 |
Correct |
83 ms |
14464 KB |
Output is correct |
105 |
Correct |
152 ms |
23288 KB |
Output is correct |
106 |
Correct |
95 ms |
14500 KB |
Output is correct |
107 |
Correct |
91 ms |
14492 KB |
Output is correct |
108 |
Correct |
167 ms |
27268 KB |
Output is correct |
109 |
Correct |
78 ms |
14488 KB |
Output is correct |
110 |
Correct |
145 ms |
27544 KB |
Output is correct |
111 |
Correct |
115 ms |
13732 KB |
Output is correct |