/*
DavitMarg
In pr honky-tonk,
Down in Mexico
*/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <random>
#include <chrono>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 500005;
int n;
string s;
vector<int> g[N];
int dp[N], dp2[N];
int ans = 0;
void dfs(int v, int p)
{
dp[v] = s[v] - '0';
int sum = 0;
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to == p)
continue;
dfs(to, v);
sum += dp[to];
}
dp2[v] = dp[v] = max(dp[v], sum - (s[v] - '0'));
}
void dfs2(int v, int p)
{
dp2[v] = s[v] - '0';
int sum = 0;
int mx = 0;
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to == p)
continue;
sum += dp[to];
mx = max(dp[to], mx);
}
// sum += dp2[p];
// mx = max(dp2[p], mx);
dp2[v] = max(dp2[v], sum - (s[v] - '0'));
ans = max(ans, max(dp2[v], (s[v] - '0') + mx));
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to == p)
continue;
dfs2(to, v);
}
}
void solve()
{
cin >> n;
for(int i = 1; i <= n - 1; i++)
{
int a, b;
cin >> a >> b;
g[a].PB(b);
g[b].PB(a);
}
cin>>s;
s = '#' + s;
vector<int> p;
for(int i = 1; i <= n; i++)
if(s[i] == '1')
p.PB(i);
int st = p[0];
dfs(st, 0);
dfs2(st, 0);
cout << ans << endl;
}
int main()
{
fastIO;
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
/*
cd /mnt/d/Davit/Projects/VSCode; g++ -std=c++17 -O2 -Wall -Wextra -Ddeath -Wshadow source.cpp -o orz; ./orz; rm orz
*/
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:45:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
power.cpp: In function 'void dfs2(int, int)':
power.cpp:61:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
power.cpp:76:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12076 KB |
Output is correct |
4 |
Correct |
5 ms |
11988 KB |
Output is correct |
5 |
Correct |
5 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
12076 KB |
Output is correct |
7 |
Correct |
6 ms |
11984 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
5 ms |
11968 KB |
Output is correct |
11 |
Correct |
5 ms |
12068 KB |
Output is correct |
12 |
Correct |
6 ms |
12076 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
12080 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
12012 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
8 ms |
12104 KB |
Output is correct |
21 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12076 KB |
Output is correct |
4 |
Correct |
5 ms |
11988 KB |
Output is correct |
5 |
Correct |
5 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
12076 KB |
Output is correct |
7 |
Correct |
6 ms |
11984 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
5 ms |
11968 KB |
Output is correct |
11 |
Correct |
5 ms |
12068 KB |
Output is correct |
12 |
Correct |
6 ms |
12076 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
12080 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
12012 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
8 ms |
12104 KB |
Output is correct |
21 |
Correct |
7 ms |
11988 KB |
Output is correct |
22 |
Correct |
9 ms |
12084 KB |
Output is correct |
23 |
Correct |
7 ms |
12136 KB |
Output is correct |
24 |
Correct |
9 ms |
12088 KB |
Output is correct |
25 |
Correct |
9 ms |
12084 KB |
Output is correct |
26 |
Correct |
6 ms |
12116 KB |
Output is correct |
27 |
Correct |
7 ms |
12116 KB |
Output is correct |
28 |
Correct |
6 ms |
12116 KB |
Output is correct |
29 |
Correct |
6 ms |
12244 KB |
Output is correct |
30 |
Correct |
6 ms |
12212 KB |
Output is correct |
31 |
Correct |
6 ms |
12116 KB |
Output is correct |
32 |
Correct |
7 ms |
12088 KB |
Output is correct |
33 |
Correct |
7 ms |
12116 KB |
Output is correct |
34 |
Correct |
7 ms |
12116 KB |
Output is correct |
35 |
Correct |
6 ms |
12116 KB |
Output is correct |
36 |
Correct |
7 ms |
12088 KB |
Output is correct |
37 |
Correct |
6 ms |
12088 KB |
Output is correct |
38 |
Correct |
7 ms |
12116 KB |
Output is correct |
39 |
Correct |
7 ms |
12244 KB |
Output is correct |
40 |
Correct |
7 ms |
12212 KB |
Output is correct |
41 |
Correct |
7 ms |
12116 KB |
Output is correct |
42 |
Correct |
6 ms |
12216 KB |
Output is correct |
43 |
Correct |
7 ms |
12116 KB |
Output is correct |
44 |
Correct |
6 ms |
12236 KB |
Output is correct |
45 |
Correct |
7 ms |
12212 KB |
Output is correct |
46 |
Correct |
6 ms |
12116 KB |
Output is correct |
47 |
Correct |
8 ms |
12092 KB |
Output is correct |
48 |
Correct |
6 ms |
12212 KB |
Output is correct |
49 |
Correct |
6 ms |
12244 KB |
Output is correct |
50 |
Correct |
6 ms |
12212 KB |
Output is correct |
51 |
Correct |
6 ms |
12216 KB |
Output is correct |
52 |
Correct |
6 ms |
12116 KB |
Output is correct |
53 |
Correct |
8 ms |
12132 KB |
Output is correct |
54 |
Correct |
6 ms |
12112 KB |
Output is correct |
55 |
Correct |
8 ms |
12116 KB |
Output is correct |
56 |
Correct |
7 ms |
12216 KB |
Output is correct |
57 |
Correct |
8 ms |
12220 KB |
Output is correct |
58 |
Correct |
7 ms |
12212 KB |
Output is correct |
59 |
Correct |
6 ms |
12116 KB |
Output is correct |
60 |
Correct |
7 ms |
12216 KB |
Output is correct |
61 |
Correct |
6 ms |
12116 KB |
Output is correct |
62 |
Correct |
7 ms |
12372 KB |
Output is correct |
63 |
Correct |
6 ms |
12084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12076 KB |
Output is correct |
4 |
Correct |
5 ms |
11988 KB |
Output is correct |
5 |
Correct |
5 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
12076 KB |
Output is correct |
7 |
Correct |
6 ms |
11984 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
5 ms |
11968 KB |
Output is correct |
11 |
Correct |
5 ms |
12068 KB |
Output is correct |
12 |
Correct |
6 ms |
12076 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
12080 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
12012 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
8 ms |
12104 KB |
Output is correct |
21 |
Correct |
7 ms |
11988 KB |
Output is correct |
22 |
Correct |
9 ms |
12084 KB |
Output is correct |
23 |
Correct |
7 ms |
12136 KB |
Output is correct |
24 |
Correct |
9 ms |
12088 KB |
Output is correct |
25 |
Correct |
9 ms |
12084 KB |
Output is correct |
26 |
Correct |
6 ms |
12116 KB |
Output is correct |
27 |
Correct |
7 ms |
12116 KB |
Output is correct |
28 |
Correct |
6 ms |
12116 KB |
Output is correct |
29 |
Correct |
6 ms |
12244 KB |
Output is correct |
30 |
Correct |
6 ms |
12212 KB |
Output is correct |
31 |
Correct |
6 ms |
12116 KB |
Output is correct |
32 |
Correct |
7 ms |
12088 KB |
Output is correct |
33 |
Correct |
7 ms |
12116 KB |
Output is correct |
34 |
Correct |
7 ms |
12116 KB |
Output is correct |
35 |
Correct |
6 ms |
12116 KB |
Output is correct |
36 |
Correct |
7 ms |
12088 KB |
Output is correct |
37 |
Correct |
6 ms |
12088 KB |
Output is correct |
38 |
Correct |
7 ms |
12116 KB |
Output is correct |
39 |
Correct |
7 ms |
12244 KB |
Output is correct |
40 |
Correct |
7 ms |
12212 KB |
Output is correct |
41 |
Correct |
7 ms |
12116 KB |
Output is correct |
42 |
Correct |
6 ms |
12216 KB |
Output is correct |
43 |
Correct |
7 ms |
12116 KB |
Output is correct |
44 |
Correct |
6 ms |
12236 KB |
Output is correct |
45 |
Correct |
7 ms |
12212 KB |
Output is correct |
46 |
Correct |
6 ms |
12116 KB |
Output is correct |
47 |
Correct |
8 ms |
12092 KB |
Output is correct |
48 |
Correct |
6 ms |
12212 KB |
Output is correct |
49 |
Correct |
6 ms |
12244 KB |
Output is correct |
50 |
Correct |
6 ms |
12212 KB |
Output is correct |
51 |
Correct |
6 ms |
12216 KB |
Output is correct |
52 |
Correct |
6 ms |
12116 KB |
Output is correct |
53 |
Correct |
8 ms |
12132 KB |
Output is correct |
54 |
Correct |
6 ms |
12112 KB |
Output is correct |
55 |
Correct |
8 ms |
12116 KB |
Output is correct |
56 |
Correct |
7 ms |
12216 KB |
Output is correct |
57 |
Correct |
8 ms |
12220 KB |
Output is correct |
58 |
Correct |
7 ms |
12212 KB |
Output is correct |
59 |
Correct |
6 ms |
12116 KB |
Output is correct |
60 |
Correct |
7 ms |
12216 KB |
Output is correct |
61 |
Correct |
6 ms |
12116 KB |
Output is correct |
62 |
Correct |
7 ms |
12372 KB |
Output is correct |
63 |
Correct |
6 ms |
12084 KB |
Output is correct |
64 |
Correct |
95 ms |
22556 KB |
Output is correct |
65 |
Correct |
122 ms |
22324 KB |
Output is correct |
66 |
Correct |
127 ms |
22228 KB |
Output is correct |
67 |
Correct |
89 ms |
22024 KB |
Output is correct |
68 |
Correct |
93 ms |
21508 KB |
Output is correct |
69 |
Correct |
110 ms |
21308 KB |
Output is correct |
70 |
Correct |
91 ms |
21168 KB |
Output is correct |
71 |
Correct |
93 ms |
21220 KB |
Output is correct |
72 |
Correct |
93 ms |
21188 KB |
Output is correct |
73 |
Correct |
125 ms |
34940 KB |
Output is correct |
74 |
Correct |
140 ms |
29668 KB |
Output is correct |
75 |
Correct |
118 ms |
22292 KB |
Output is correct |
76 |
Correct |
91 ms |
21844 KB |
Output is correct |
77 |
Correct |
121 ms |
22096 KB |
Output is correct |
78 |
Correct |
94 ms |
21876 KB |
Output is correct |
79 |
Correct |
111 ms |
21512 KB |
Output is correct |
80 |
Correct |
96 ms |
21136 KB |
Output is correct |
81 |
Correct |
83 ms |
22060 KB |
Output is correct |
82 |
Correct |
87 ms |
21700 KB |
Output is correct |
83 |
Correct |
107 ms |
27388 KB |
Output is correct |
84 |
Correct |
109 ms |
30652 KB |
Output is correct |
85 |
Correct |
137 ms |
30956 KB |
Output is correct |
86 |
Correct |
93 ms |
24716 KB |
Output is correct |
87 |
Correct |
94 ms |
24004 KB |
Output is correct |
88 |
Correct |
90 ms |
24920 KB |
Output is correct |
89 |
Correct |
112 ms |
25388 KB |
Output is correct |
90 |
Correct |
110 ms |
24736 KB |
Output is correct |
91 |
Correct |
93 ms |
25236 KB |
Output is correct |
92 |
Correct |
96 ms |
24964 KB |
Output is correct |
93 |
Correct |
132 ms |
27864 KB |
Output is correct |
94 |
Correct |
91 ms |
29348 KB |
Output is correct |
95 |
Correct |
89 ms |
28124 KB |
Output is correct |
96 |
Correct |
93 ms |
26508 KB |
Output is correct |
97 |
Correct |
127 ms |
24944 KB |
Output is correct |
98 |
Correct |
83 ms |
24280 KB |
Output is correct |
99 |
Correct |
84 ms |
24400 KB |
Output is correct |
100 |
Correct |
90 ms |
27724 KB |
Output is correct |
101 |
Correct |
87 ms |
25476 KB |
Output is correct |
102 |
Correct |
112 ms |
24952 KB |
Output is correct |
103 |
Correct |
88 ms |
24296 KB |
Output is correct |
104 |
Correct |
59 ms |
23828 KB |
Output is correct |
105 |
Correct |
115 ms |
38472 KB |
Output is correct |
106 |
Correct |
61 ms |
24412 KB |
Output is correct |
107 |
Correct |
63 ms |
24304 KB |
Output is correct |
108 |
Correct |
126 ms |
38480 KB |
Output is correct |
109 |
Correct |
63 ms |
23880 KB |
Output is correct |
110 |
Correct |
134 ms |
38472 KB |
Output is correct |
111 |
Correct |
94 ms |
23684 KB |
Output is correct |