#include <bits/stdc++.h>
#define gsn "Power Plant"
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r) (rng);
}
const int maxn = 200005;
int n, ans;
int dp[maxn];
string s;
vector<int> o[maxn];
void dfs(int u, int par)
{
int sum = 0, mx = 0;
for(int v : o[u])
{
if(v == par) continue;
dfs(v, u);
sum += dp[v], mx = max(mx, dp[v]);
}
ans = max({ ans, sum - (s[u] == '1'), mx + (s[u] == '1') });
dp[u] = max({ (int)(s[u] == '1'), sum - (s[u] == '1'), mx - (s[u] == '1') });
}
void solve()
{
cin >> n;
fi(i, 1, n - 1)
{
int u, v;
cin >> u >> v;
o[u].push_back(v);
o[v].push_back(u);
}
cin >> s, s = ' ' + s;
dfs(1, -1);
cout << ans ;
}
int main()
{
if(fopen(gsn".inp", "r"))
{
freopen(gsn".inp", "r", stdin);
freopen(gsn".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int nTest = 1;
// cin >> nTest;
while(nTest --)
{
solve();
}
/// ------------------check time!-----------------///
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(gsn".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
power.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen(gsn".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
5132 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4968 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
5132 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4968 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
2 ms |
5044 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5220 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
2 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5212 KB |
Output is correct |
45 |
Correct |
2 ms |
5212 KB |
Output is correct |
46 |
Correct |
2 ms |
5212 KB |
Output is correct |
47 |
Correct |
2 ms |
5212 KB |
Output is correct |
48 |
Correct |
2 ms |
5464 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
2 ms |
5212 KB |
Output is correct |
52 |
Correct |
2 ms |
5212 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
2 ms |
5212 KB |
Output is correct |
55 |
Correct |
2 ms |
5212 KB |
Output is correct |
56 |
Correct |
2 ms |
5212 KB |
Output is correct |
57 |
Correct |
2 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
3 ms |
5212 KB |
Output is correct |
60 |
Correct |
3 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5464 KB |
Output is correct |
63 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
5132 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4968 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
2 ms |
5044 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5220 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
2 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5212 KB |
Output is correct |
45 |
Correct |
2 ms |
5212 KB |
Output is correct |
46 |
Correct |
2 ms |
5212 KB |
Output is correct |
47 |
Correct |
2 ms |
5212 KB |
Output is correct |
48 |
Correct |
2 ms |
5464 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
2 ms |
5212 KB |
Output is correct |
52 |
Correct |
2 ms |
5212 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
2 ms |
5212 KB |
Output is correct |
55 |
Correct |
2 ms |
5212 KB |
Output is correct |
56 |
Correct |
2 ms |
5212 KB |
Output is correct |
57 |
Correct |
2 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
3 ms |
5212 KB |
Output is correct |
60 |
Correct |
3 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5464 KB |
Output is correct |
63 |
Correct |
3 ms |
5208 KB |
Output is correct |
64 |
Correct |
62 ms |
12500 KB |
Output is correct |
65 |
Correct |
61 ms |
12496 KB |
Output is correct |
66 |
Correct |
82 ms |
12880 KB |
Output is correct |
67 |
Correct |
90 ms |
12436 KB |
Output is correct |
68 |
Correct |
59 ms |
12476 KB |
Output is correct |
69 |
Correct |
65 ms |
12876 KB |
Output is correct |
70 |
Correct |
78 ms |
12760 KB |
Output is correct |
71 |
Correct |
63 ms |
12508 KB |
Output is correct |
72 |
Correct |
62 ms |
12492 KB |
Output is correct |
73 |
Correct |
82 ms |
25032 KB |
Output is correct |
74 |
Correct |
107 ms |
25040 KB |
Output is correct |
75 |
Correct |
66 ms |
12476 KB |
Output is correct |
76 |
Correct |
62 ms |
12500 KB |
Output is correct |
77 |
Correct |
61 ms |
12512 KB |
Output is correct |
78 |
Correct |
65 ms |
12456 KB |
Output is correct |
79 |
Correct |
61 ms |
12516 KB |
Output is correct |
80 |
Correct |
93 ms |
12492 KB |
Output is correct |
81 |
Correct |
61 ms |
12900 KB |
Output is correct |
82 |
Correct |
58 ms |
12748 KB |
Output is correct |
83 |
Correct |
97 ms |
17480 KB |
Output is correct |
84 |
Correct |
74 ms |
19728 KB |
Output is correct |
85 |
Correct |
74 ms |
19424 KB |
Output is correct |
86 |
Correct |
99 ms |
14192 KB |
Output is correct |
87 |
Correct |
61 ms |
13260 KB |
Output is correct |
88 |
Correct |
66 ms |
13844 KB |
Output is correct |
89 |
Correct |
94 ms |
13788 KB |
Output is correct |
90 |
Correct |
88 ms |
13484 KB |
Output is correct |
91 |
Correct |
92 ms |
13804 KB |
Output is correct |
92 |
Correct |
64 ms |
14128 KB |
Output is correct |
93 |
Correct |
66 ms |
16544 KB |
Output is correct |
94 |
Correct |
67 ms |
18376 KB |
Output is correct |
95 |
Correct |
65 ms |
16772 KB |
Output is correct |
96 |
Correct |
91 ms |
15152 KB |
Output is correct |
97 |
Correct |
70 ms |
13500 KB |
Output is correct |
98 |
Correct |
62 ms |
13056 KB |
Output is correct |
99 |
Correct |
65 ms |
12748 KB |
Output is correct |
100 |
Correct |
66 ms |
16140 KB |
Output is correct |
101 |
Correct |
88 ms |
14148 KB |
Output is correct |
102 |
Correct |
57 ms |
13216 KB |
Output is correct |
103 |
Correct |
83 ms |
13080 KB |
Output is correct |
104 |
Correct |
45 ms |
13248 KB |
Output is correct |
105 |
Correct |
102 ms |
21860 KB |
Output is correct |
106 |
Correct |
64 ms |
13776 KB |
Output is correct |
107 |
Correct |
73 ms |
13404 KB |
Output is correct |
108 |
Correct |
96 ms |
25836 KB |
Output is correct |
109 |
Correct |
46 ms |
13364 KB |
Output is correct |
110 |
Correct |
83 ms |
26268 KB |
Output is correct |
111 |
Correct |
83 ms |
12508 KB |
Output is correct |