/*
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];
int dfs(int v, int p, bool start = false)
{
dp[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 += dfs(to, v);
mx = max(dp[to], mx);
}
dp[v] = max(dp[v], sum - (s[v] - '0'));
if(start)
dp[v] = max(dp[v], (s[v] - '0') + mx);
return dp[v];
}
LL myRand()
{
return ((LL)rand() << 16) + rand();
}
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;
int ans = 0;
vector<int> p;
for(int i = 1; i <= n; i++)
if(s[i] == '1')
p.PB(i);
srand(696969);
for(int i = 0; i < 10000000 / n; i++)
{
int v = p[myRand() % p.size()];
ans = max(ans, dfs(v, -1, true));
}
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 'int dfs(int, int, bool)':
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++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
12052 KB |
Output is correct |
2 |
Correct |
99 ms |
12048 KB |
Output is correct |
3 |
Correct |
97 ms |
12048 KB |
Output is correct |
4 |
Correct |
101 ms |
12048 KB |
Output is correct |
5 |
Correct |
113 ms |
12048 KB |
Output is correct |
6 |
Correct |
106 ms |
12060 KB |
Output is correct |
7 |
Correct |
103 ms |
12052 KB |
Output is correct |
8 |
Correct |
105 ms |
12056 KB |
Output is correct |
9 |
Correct |
104 ms |
12052 KB |
Output is correct |
10 |
Correct |
104 ms |
11988 KB |
Output is correct |
11 |
Correct |
106 ms |
12048 KB |
Output is correct |
12 |
Correct |
101 ms |
12044 KB |
Output is correct |
13 |
Correct |
104 ms |
11988 KB |
Output is correct |
14 |
Correct |
106 ms |
12048 KB |
Output is correct |
15 |
Correct |
93 ms |
11988 KB |
Output is correct |
16 |
Correct |
102 ms |
12060 KB |
Output is correct |
17 |
Correct |
104 ms |
12044 KB |
Output is correct |
18 |
Correct |
101 ms |
12052 KB |
Output is correct |
19 |
Correct |
100 ms |
11988 KB |
Output is correct |
20 |
Correct |
104 ms |
11988 KB |
Output is correct |
21 |
Correct |
102 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
12052 KB |
Output is correct |
2 |
Correct |
99 ms |
12048 KB |
Output is correct |
3 |
Correct |
97 ms |
12048 KB |
Output is correct |
4 |
Correct |
101 ms |
12048 KB |
Output is correct |
5 |
Correct |
113 ms |
12048 KB |
Output is correct |
6 |
Correct |
106 ms |
12060 KB |
Output is correct |
7 |
Correct |
103 ms |
12052 KB |
Output is correct |
8 |
Correct |
105 ms |
12056 KB |
Output is correct |
9 |
Correct |
104 ms |
12052 KB |
Output is correct |
10 |
Correct |
104 ms |
11988 KB |
Output is correct |
11 |
Correct |
106 ms |
12048 KB |
Output is correct |
12 |
Correct |
101 ms |
12044 KB |
Output is correct |
13 |
Correct |
104 ms |
11988 KB |
Output is correct |
14 |
Correct |
106 ms |
12048 KB |
Output is correct |
15 |
Correct |
93 ms |
11988 KB |
Output is correct |
16 |
Correct |
102 ms |
12060 KB |
Output is correct |
17 |
Correct |
104 ms |
12044 KB |
Output is correct |
18 |
Correct |
101 ms |
12052 KB |
Output is correct |
19 |
Correct |
100 ms |
11988 KB |
Output is correct |
20 |
Correct |
104 ms |
11988 KB |
Output is correct |
21 |
Correct |
102 ms |
11988 KB |
Output is correct |
22 |
Correct |
112 ms |
12144 KB |
Output is correct |
23 |
Correct |
112 ms |
12148 KB |
Output is correct |
24 |
Correct |
134 ms |
12116 KB |
Output is correct |
25 |
Correct |
117 ms |
12140 KB |
Output is correct |
26 |
Correct |
112 ms |
12116 KB |
Output is correct |
27 |
Correct |
111 ms |
12128 KB |
Output is correct |
28 |
Correct |
112 ms |
12116 KB |
Output is correct |
29 |
Correct |
156 ms |
12324 KB |
Output is correct |
30 |
Correct |
152 ms |
12316 KB |
Output is correct |
31 |
Correct |
107 ms |
12140 KB |
Output is correct |
32 |
Correct |
105 ms |
12132 KB |
Output is correct |
33 |
Correct |
117 ms |
12140 KB |
Output is correct |
34 |
Correct |
116 ms |
12132 KB |
Output is correct |
35 |
Correct |
116 ms |
12140 KB |
Output is correct |
36 |
Correct |
115 ms |
12116 KB |
Output is correct |
37 |
Correct |
120 ms |
12116 KB |
Output is correct |
38 |
Correct |
103 ms |
12116 KB |
Output is correct |
39 |
Correct |
138 ms |
12244 KB |
Output is correct |
40 |
Correct |
132 ms |
12244 KB |
Output is correct |
41 |
Correct |
130 ms |
12216 KB |
Output is correct |
42 |
Correct |
134 ms |
12180 KB |
Output is correct |
43 |
Correct |
124 ms |
12164 KB |
Output is correct |
44 |
Correct |
117 ms |
12192 KB |
Output is correct |
45 |
Correct |
114 ms |
12180 KB |
Output is correct |
46 |
Correct |
114 ms |
12176 KB |
Output is correct |
47 |
Correct |
114 ms |
12164 KB |
Output is correct |
48 |
Correct |
135 ms |
12244 KB |
Output is correct |
49 |
Correct |
115 ms |
12240 KB |
Output is correct |
50 |
Correct |
113 ms |
12208 KB |
Output is correct |
51 |
Correct |
114 ms |
12184 KB |
Output is correct |
52 |
Correct |
114 ms |
12152 KB |
Output is correct |
53 |
Correct |
135 ms |
12116 KB |
Output is correct |
54 |
Correct |
111 ms |
12116 KB |
Output is correct |
55 |
Correct |
110 ms |
12116 KB |
Output is correct |
56 |
Correct |
77 ms |
12132 KB |
Output is correct |
57 |
Correct |
150 ms |
12308 KB |
Output is correct |
58 |
Correct |
78 ms |
12152 KB |
Output is correct |
59 |
Correct |
78 ms |
12116 KB |
Output is correct |
60 |
Correct |
154 ms |
12312 KB |
Output is correct |
61 |
Correct |
78 ms |
12136 KB |
Output is correct |
62 |
Correct |
154 ms |
12308 KB |
Output is correct |
63 |
Correct |
97 ms |
12144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
12052 KB |
Output is correct |
2 |
Correct |
99 ms |
12048 KB |
Output is correct |
3 |
Correct |
97 ms |
12048 KB |
Output is correct |
4 |
Correct |
101 ms |
12048 KB |
Output is correct |
5 |
Correct |
113 ms |
12048 KB |
Output is correct |
6 |
Correct |
106 ms |
12060 KB |
Output is correct |
7 |
Correct |
103 ms |
12052 KB |
Output is correct |
8 |
Correct |
105 ms |
12056 KB |
Output is correct |
9 |
Correct |
104 ms |
12052 KB |
Output is correct |
10 |
Correct |
104 ms |
11988 KB |
Output is correct |
11 |
Correct |
106 ms |
12048 KB |
Output is correct |
12 |
Correct |
101 ms |
12044 KB |
Output is correct |
13 |
Correct |
104 ms |
11988 KB |
Output is correct |
14 |
Correct |
106 ms |
12048 KB |
Output is correct |
15 |
Correct |
93 ms |
11988 KB |
Output is correct |
16 |
Correct |
102 ms |
12060 KB |
Output is correct |
17 |
Correct |
104 ms |
12044 KB |
Output is correct |
18 |
Correct |
101 ms |
12052 KB |
Output is correct |
19 |
Correct |
100 ms |
11988 KB |
Output is correct |
20 |
Correct |
104 ms |
11988 KB |
Output is correct |
21 |
Correct |
102 ms |
11988 KB |
Output is correct |
22 |
Correct |
112 ms |
12144 KB |
Output is correct |
23 |
Correct |
112 ms |
12148 KB |
Output is correct |
24 |
Correct |
134 ms |
12116 KB |
Output is correct |
25 |
Correct |
117 ms |
12140 KB |
Output is correct |
26 |
Correct |
112 ms |
12116 KB |
Output is correct |
27 |
Correct |
111 ms |
12128 KB |
Output is correct |
28 |
Correct |
112 ms |
12116 KB |
Output is correct |
29 |
Correct |
156 ms |
12324 KB |
Output is correct |
30 |
Correct |
152 ms |
12316 KB |
Output is correct |
31 |
Correct |
107 ms |
12140 KB |
Output is correct |
32 |
Correct |
105 ms |
12132 KB |
Output is correct |
33 |
Correct |
117 ms |
12140 KB |
Output is correct |
34 |
Correct |
116 ms |
12132 KB |
Output is correct |
35 |
Correct |
116 ms |
12140 KB |
Output is correct |
36 |
Correct |
115 ms |
12116 KB |
Output is correct |
37 |
Correct |
120 ms |
12116 KB |
Output is correct |
38 |
Correct |
103 ms |
12116 KB |
Output is correct |
39 |
Correct |
138 ms |
12244 KB |
Output is correct |
40 |
Correct |
132 ms |
12244 KB |
Output is correct |
41 |
Correct |
130 ms |
12216 KB |
Output is correct |
42 |
Correct |
134 ms |
12180 KB |
Output is correct |
43 |
Correct |
124 ms |
12164 KB |
Output is correct |
44 |
Correct |
117 ms |
12192 KB |
Output is correct |
45 |
Correct |
114 ms |
12180 KB |
Output is correct |
46 |
Correct |
114 ms |
12176 KB |
Output is correct |
47 |
Correct |
114 ms |
12164 KB |
Output is correct |
48 |
Correct |
135 ms |
12244 KB |
Output is correct |
49 |
Correct |
115 ms |
12240 KB |
Output is correct |
50 |
Correct |
113 ms |
12208 KB |
Output is correct |
51 |
Correct |
114 ms |
12184 KB |
Output is correct |
52 |
Correct |
114 ms |
12152 KB |
Output is correct |
53 |
Correct |
135 ms |
12116 KB |
Output is correct |
54 |
Correct |
111 ms |
12116 KB |
Output is correct |
55 |
Correct |
110 ms |
12116 KB |
Output is correct |
56 |
Correct |
77 ms |
12132 KB |
Output is correct |
57 |
Correct |
150 ms |
12308 KB |
Output is correct |
58 |
Correct |
78 ms |
12152 KB |
Output is correct |
59 |
Correct |
78 ms |
12116 KB |
Output is correct |
60 |
Correct |
154 ms |
12312 KB |
Output is correct |
61 |
Correct |
78 ms |
12136 KB |
Output is correct |
62 |
Correct |
154 ms |
12308 KB |
Output is correct |
63 |
Correct |
97 ms |
12144 KB |
Output is correct |
64 |
Correct |
693 ms |
20732 KB |
Output is correct |
65 |
Correct |
790 ms |
23368 KB |
Output is correct |
66 |
Correct |
791 ms |
23252 KB |
Output is correct |
67 |
Correct |
660 ms |
23060 KB |
Output is correct |
68 |
Correct |
732 ms |
22604 KB |
Output is correct |
69 |
Correct |
731 ms |
22356 KB |
Output is correct |
70 |
Correct |
759 ms |
22272 KB |
Output is correct |
71 |
Correct |
839 ms |
22280 KB |
Output is correct |
72 |
Correct |
792 ms |
22276 KB |
Output is correct |
73 |
Execution timed out |
1567 ms |
40680 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |