#include <bits/stdc++.h>
using namespace std;
void dfs(int x, int p, vector<vector<int>> &AR, string &S, vector<int> &dp)
{
for (int i = 0; i < AR[x].size(); i++)
{
if (AR[x][i] == p)
continue;
dfs(AR[x][i], x, AR, S, dp);
dp[x] += dp[AR[x][i]];
}
if (S[x] == '1')
dp[x] = max(dp[x] - 1, 1);
}
void dfs2(int x, int p, vector<vector<int>> &AR, vector<int> &dp, string &S, int val)
{
dp[x] += val;
int sum = 0, best = 0;
for (int i = 0; i < AR[x].size(); i++)
{
if (AR[x][i] == p)
continue;
sum += dp[AR[x][i]];
best = max(best, dp[AR[x][i]]);
}
sum += val;
best = max(best, val);
if (S[x] == '1')
{
dp[x] = max(sum - 1, best + 1);
}
for (int i = 0; i < AR[x].size(); i++)
{
if (AR[x][i] == p)
continue;
int next = sum - dp[AR[x][i]];
if (S[x] == '1')
next = max(next - 1, 1);
dfs2(AR[x][i], x, AR, dp, S, next);
}
}
int main()
{
int N;
cin >> N;
vector<vector<int>> AR(N);
for (int i = 0; i < N - 1; i++)
{
int a, b;
cin >> a >> b;
a--, b--;
AR[a].push_back(b);
AR[b].push_back(a);
}
string S;
cin >> S;
vector<int> dp(N);
dfs(0, 0, AR, S, dp);
dfs2(0, 0, AR, dp, S, 0);
int best = 0;
for (int i = 0; i < N; i++)
best = max(best, dp[i]);
cout << best;
}
Compilation message
power.cpp: In function 'void dfs(int, int, std::vector<std::vector<int> >&, std::string&, std::vector<int>&)':
power.cpp:5:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | for (int i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
power.cpp: In function 'void dfs2(int, int, std::vector<std::vector<int> >&, std::vector<int>&, std::string&, int)':
power.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
power.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
432 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
440 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
436 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
432 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
440 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
436 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
436 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
448 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
444 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
448 KB |
Output is correct |
48 |
Correct |
2 ms |
664 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
1 ms |
440 KB |
Output is correct |
52 |
Correct |
2 ms |
548 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
568 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
2 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
432 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
440 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
436 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
436 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
448 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
444 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
448 KB |
Output is correct |
48 |
Correct |
2 ms |
664 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
1 ms |
440 KB |
Output is correct |
52 |
Correct |
2 ms |
548 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
568 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
2 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
2 ms |
348 KB |
Output is correct |
64 |
Correct |
157 ms |
15328 KB |
Output is correct |
65 |
Correct |
113 ms |
15160 KB |
Output is correct |
66 |
Correct |
117 ms |
15248 KB |
Output is correct |
67 |
Correct |
112 ms |
15156 KB |
Output is correct |
68 |
Correct |
134 ms |
15080 KB |
Output is correct |
69 |
Correct |
130 ms |
15072 KB |
Output is correct |
70 |
Correct |
110 ms |
15272 KB |
Output is correct |
71 |
Correct |
108 ms |
15112 KB |
Output is correct |
72 |
Correct |
111 ms |
15080 KB |
Output is correct |
73 |
Correct |
158 ms |
30404 KB |
Output is correct |
74 |
Correct |
125 ms |
30180 KB |
Output is correct |
75 |
Correct |
109 ms |
15076 KB |
Output is correct |
76 |
Correct |
118 ms |
15096 KB |
Output is correct |
77 |
Correct |
109 ms |
15180 KB |
Output is correct |
78 |
Correct |
109 ms |
15028 KB |
Output is correct |
79 |
Correct |
100 ms |
15076 KB |
Output is correct |
80 |
Correct |
125 ms |
15000 KB |
Output is correct |
81 |
Correct |
106 ms |
15552 KB |
Output is correct |
82 |
Correct |
113 ms |
15312 KB |
Output is correct |
83 |
Correct |
154 ms |
21220 KB |
Output is correct |
84 |
Correct |
134 ms |
23904 KB |
Output is correct |
85 |
Correct |
120 ms |
23528 KB |
Output is correct |
86 |
Correct |
111 ms |
16868 KB |
Output is correct |
87 |
Correct |
139 ms |
16096 KB |
Output is correct |
88 |
Correct |
119 ms |
16628 KB |
Output is correct |
89 |
Correct |
148 ms |
16616 KB |
Output is correct |
90 |
Correct |
131 ms |
16424 KB |
Output is correct |
91 |
Correct |
115 ms |
16616 KB |
Output is correct |
92 |
Correct |
115 ms |
16868 KB |
Output is correct |
93 |
Correct |
108 ms |
19808 KB |
Output is correct |
94 |
Correct |
108 ms |
21888 KB |
Output is correct |
95 |
Correct |
102 ms |
20320 KB |
Output is correct |
96 |
Correct |
119 ms |
18148 KB |
Output is correct |
97 |
Correct |
110 ms |
16356 KB |
Output is correct |
98 |
Correct |
106 ms |
15900 KB |
Output is correct |
99 |
Correct |
110 ms |
15504 KB |
Output is correct |
100 |
Correct |
109 ms |
19428 KB |
Output is correct |
101 |
Correct |
113 ms |
16864 KB |
Output is correct |
102 |
Correct |
104 ms |
16112 KB |
Output is correct |
103 |
Correct |
112 ms |
15848 KB |
Output is correct |
104 |
Correct |
88 ms |
16172 KB |
Output is correct |
105 |
Correct |
191 ms |
26596 KB |
Output is correct |
106 |
Correct |
87 ms |
15896 KB |
Output is correct |
107 |
Correct |
89 ms |
15816 KB |
Output is correct |
108 |
Correct |
149 ms |
31200 KB |
Output is correct |
109 |
Correct |
101 ms |
15988 KB |
Output is correct |
110 |
Correct |
125 ms |
31524 KB |
Output is correct |
111 |
Correct |
100 ms |
15072 KB |
Output is correct |