/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI acos(-1.0)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x, i) (1 & ((x) >> (i)))
#define MASK(x) (1LL << (x))
#define task "tnc"
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int mo = 998244353;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using pii = pair<pair<ll, ll>, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
vector<int> G[maxn];
string s;
int re[maxn];
int dp[maxn];
int tong[maxn];
void dfs(int u, int pa)
{
for (auto v : G[u])
{
if (v == pa)
continue;
dfs(v, u);
dp[u] += dp[v];
tong[u] += dp[v];
}
if (s[u] == '1')
dp[u] = max(dp[u] - 1, 1);
}
int ans = 0;
void dfs_re(int u, int pa)
{
if (pa != -1)
{
re[u] = re[pa] + tong[pa] - dp[u];
if (s[pa] == '1')
{
re[u] = max(1, re[u] - 1);
}
ans = max(ans, re[u] + dp[u]);
if (s[u] == '1')
{
ans = max(ans, 1);
}
}
for (auto v : G[u])
{
if (v == pa)
continue;
dfs_re(v, u);
}
}
signed main()
{
cin.tie(0), cout.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
cin >> s;
s = ' ' + s;
dfs(1, -1);
ans = max(ans, dp[1]);
dfs_re(1, -1);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23724 KB |
Output is correct |
4 |
Correct |
11 ms |
23824 KB |
Output is correct |
5 |
Correct |
12 ms |
23824 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23716 KB |
Output is correct |
8 |
Correct |
12 ms |
23744 KB |
Output is correct |
9 |
Correct |
11 ms |
23820 KB |
Output is correct |
10 |
Correct |
12 ms |
23760 KB |
Output is correct |
11 |
Correct |
11 ms |
23820 KB |
Output is correct |
12 |
Correct |
12 ms |
23816 KB |
Output is correct |
13 |
Correct |
11 ms |
23816 KB |
Output is correct |
14 |
Correct |
12 ms |
23728 KB |
Output is correct |
15 |
Correct |
11 ms |
23824 KB |
Output is correct |
16 |
Correct |
14 ms |
23724 KB |
Output is correct |
17 |
Correct |
12 ms |
23800 KB |
Output is correct |
18 |
Correct |
13 ms |
23840 KB |
Output is correct |
19 |
Correct |
14 ms |
23760 KB |
Output is correct |
20 |
Correct |
12 ms |
23732 KB |
Output is correct |
21 |
Correct |
12 ms |
23816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23724 KB |
Output is correct |
4 |
Correct |
11 ms |
23824 KB |
Output is correct |
5 |
Correct |
12 ms |
23824 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23716 KB |
Output is correct |
8 |
Correct |
12 ms |
23744 KB |
Output is correct |
9 |
Correct |
11 ms |
23820 KB |
Output is correct |
10 |
Correct |
12 ms |
23760 KB |
Output is correct |
11 |
Correct |
11 ms |
23820 KB |
Output is correct |
12 |
Correct |
12 ms |
23816 KB |
Output is correct |
13 |
Correct |
11 ms |
23816 KB |
Output is correct |
14 |
Correct |
12 ms |
23728 KB |
Output is correct |
15 |
Correct |
11 ms |
23824 KB |
Output is correct |
16 |
Correct |
14 ms |
23724 KB |
Output is correct |
17 |
Correct |
12 ms |
23800 KB |
Output is correct |
18 |
Correct |
13 ms |
23840 KB |
Output is correct |
19 |
Correct |
14 ms |
23760 KB |
Output is correct |
20 |
Correct |
12 ms |
23732 KB |
Output is correct |
21 |
Correct |
12 ms |
23816 KB |
Output is correct |
22 |
Correct |
12 ms |
23832 KB |
Output is correct |
23 |
Correct |
15 ms |
23936 KB |
Output is correct |
24 |
Correct |
13 ms |
23832 KB |
Output is correct |
25 |
Correct |
14 ms |
23836 KB |
Output is correct |
26 |
Correct |
14 ms |
23832 KB |
Output is correct |
27 |
Correct |
12 ms |
23876 KB |
Output is correct |
28 |
Correct |
12 ms |
23848 KB |
Output is correct |
29 |
Correct |
12 ms |
24016 KB |
Output is correct |
30 |
Correct |
12 ms |
23924 KB |
Output is correct |
31 |
Correct |
12 ms |
23900 KB |
Output is correct |
32 |
Correct |
12 ms |
23892 KB |
Output is correct |
33 |
Correct |
12 ms |
23832 KB |
Output is correct |
34 |
Correct |
14 ms |
23832 KB |
Output is correct |
35 |
Correct |
12 ms |
23892 KB |
Output is correct |
36 |
Correct |
14 ms |
23892 KB |
Output is correct |
37 |
Correct |
14 ms |
23844 KB |
Output is correct |
38 |
Correct |
12 ms |
23836 KB |
Output is correct |
39 |
Correct |
12 ms |
23964 KB |
Output is correct |
40 |
Correct |
13 ms |
23904 KB |
Output is correct |
41 |
Correct |
13 ms |
23892 KB |
Output is correct |
42 |
Correct |
12 ms |
23892 KB |
Output is correct |
43 |
Correct |
13 ms |
23960 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
13 ms |
23892 KB |
Output is correct |
46 |
Correct |
13 ms |
23852 KB |
Output is correct |
47 |
Correct |
13 ms |
23952 KB |
Output is correct |
48 |
Correct |
12 ms |
24020 KB |
Output is correct |
49 |
Correct |
12 ms |
23960 KB |
Output is correct |
50 |
Correct |
12 ms |
23892 KB |
Output is correct |
51 |
Correct |
15 ms |
23892 KB |
Output is correct |
52 |
Correct |
13 ms |
23892 KB |
Output is correct |
53 |
Correct |
12 ms |
23892 KB |
Output is correct |
54 |
Correct |
14 ms |
23892 KB |
Output is correct |
55 |
Correct |
12 ms |
23892 KB |
Output is correct |
56 |
Correct |
12 ms |
23892 KB |
Output is correct |
57 |
Correct |
13 ms |
24048 KB |
Output is correct |
58 |
Correct |
13 ms |
23892 KB |
Output is correct |
59 |
Correct |
14 ms |
23892 KB |
Output is correct |
60 |
Correct |
14 ms |
24012 KB |
Output is correct |
61 |
Correct |
12 ms |
23924 KB |
Output is correct |
62 |
Correct |
16 ms |
24016 KB |
Output is correct |
63 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23724 KB |
Output is correct |
4 |
Correct |
11 ms |
23824 KB |
Output is correct |
5 |
Correct |
12 ms |
23824 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23716 KB |
Output is correct |
8 |
Correct |
12 ms |
23744 KB |
Output is correct |
9 |
Correct |
11 ms |
23820 KB |
Output is correct |
10 |
Correct |
12 ms |
23760 KB |
Output is correct |
11 |
Correct |
11 ms |
23820 KB |
Output is correct |
12 |
Correct |
12 ms |
23816 KB |
Output is correct |
13 |
Correct |
11 ms |
23816 KB |
Output is correct |
14 |
Correct |
12 ms |
23728 KB |
Output is correct |
15 |
Correct |
11 ms |
23824 KB |
Output is correct |
16 |
Correct |
14 ms |
23724 KB |
Output is correct |
17 |
Correct |
12 ms |
23800 KB |
Output is correct |
18 |
Correct |
13 ms |
23840 KB |
Output is correct |
19 |
Correct |
14 ms |
23760 KB |
Output is correct |
20 |
Correct |
12 ms |
23732 KB |
Output is correct |
21 |
Correct |
12 ms |
23816 KB |
Output is correct |
22 |
Correct |
12 ms |
23832 KB |
Output is correct |
23 |
Correct |
15 ms |
23936 KB |
Output is correct |
24 |
Correct |
13 ms |
23832 KB |
Output is correct |
25 |
Correct |
14 ms |
23836 KB |
Output is correct |
26 |
Correct |
14 ms |
23832 KB |
Output is correct |
27 |
Correct |
12 ms |
23876 KB |
Output is correct |
28 |
Correct |
12 ms |
23848 KB |
Output is correct |
29 |
Correct |
12 ms |
24016 KB |
Output is correct |
30 |
Correct |
12 ms |
23924 KB |
Output is correct |
31 |
Correct |
12 ms |
23900 KB |
Output is correct |
32 |
Correct |
12 ms |
23892 KB |
Output is correct |
33 |
Correct |
12 ms |
23832 KB |
Output is correct |
34 |
Correct |
14 ms |
23832 KB |
Output is correct |
35 |
Correct |
12 ms |
23892 KB |
Output is correct |
36 |
Correct |
14 ms |
23892 KB |
Output is correct |
37 |
Correct |
14 ms |
23844 KB |
Output is correct |
38 |
Correct |
12 ms |
23836 KB |
Output is correct |
39 |
Correct |
12 ms |
23964 KB |
Output is correct |
40 |
Correct |
13 ms |
23904 KB |
Output is correct |
41 |
Correct |
13 ms |
23892 KB |
Output is correct |
42 |
Correct |
12 ms |
23892 KB |
Output is correct |
43 |
Correct |
13 ms |
23960 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
13 ms |
23892 KB |
Output is correct |
46 |
Correct |
13 ms |
23852 KB |
Output is correct |
47 |
Correct |
13 ms |
23952 KB |
Output is correct |
48 |
Correct |
12 ms |
24020 KB |
Output is correct |
49 |
Correct |
12 ms |
23960 KB |
Output is correct |
50 |
Correct |
12 ms |
23892 KB |
Output is correct |
51 |
Correct |
15 ms |
23892 KB |
Output is correct |
52 |
Correct |
13 ms |
23892 KB |
Output is correct |
53 |
Correct |
12 ms |
23892 KB |
Output is correct |
54 |
Correct |
14 ms |
23892 KB |
Output is correct |
55 |
Correct |
12 ms |
23892 KB |
Output is correct |
56 |
Correct |
12 ms |
23892 KB |
Output is correct |
57 |
Correct |
13 ms |
24048 KB |
Output is correct |
58 |
Correct |
13 ms |
23892 KB |
Output is correct |
59 |
Correct |
14 ms |
23892 KB |
Output is correct |
60 |
Correct |
14 ms |
24012 KB |
Output is correct |
61 |
Correct |
12 ms |
23924 KB |
Output is correct |
62 |
Correct |
16 ms |
24016 KB |
Output is correct |
63 |
Correct |
12 ms |
23892 KB |
Output is correct |
64 |
Correct |
100 ms |
35572 KB |
Output is correct |
65 |
Correct |
105 ms |
35656 KB |
Output is correct |
66 |
Correct |
95 ms |
35584 KB |
Output is correct |
67 |
Correct |
127 ms |
35528 KB |
Output is correct |
68 |
Correct |
97 ms |
35548 KB |
Output is correct |
69 |
Correct |
98 ms |
35648 KB |
Output is correct |
70 |
Correct |
113 ms |
35544 KB |
Output is correct |
71 |
Correct |
97 ms |
35488 KB |
Output is correct |
72 |
Correct |
105 ms |
35604 KB |
Output is correct |
73 |
Correct |
153 ms |
48088 KB |
Output is correct |
74 |
Correct |
149 ms |
48112 KB |
Output is correct |
75 |
Correct |
101 ms |
35480 KB |
Output is correct |
76 |
Correct |
97 ms |
35480 KB |
Output is correct |
77 |
Correct |
98 ms |
35472 KB |
Output is correct |
78 |
Correct |
142 ms |
35400 KB |
Output is correct |
79 |
Correct |
98 ms |
35484 KB |
Output is correct |
80 |
Correct |
100 ms |
35472 KB |
Output is correct |
81 |
Correct |
89 ms |
35908 KB |
Output is correct |
82 |
Correct |
93 ms |
35624 KB |
Output is correct |
83 |
Correct |
139 ms |
40684 KB |
Output is correct |
84 |
Correct |
152 ms |
42828 KB |
Output is correct |
85 |
Correct |
151 ms |
42428 KB |
Output is correct |
86 |
Correct |
96 ms |
36924 KB |
Output is correct |
87 |
Correct |
96 ms |
36292 KB |
Output is correct |
88 |
Correct |
102 ms |
36740 KB |
Output is correct |
89 |
Correct |
110 ms |
36828 KB |
Output is correct |
90 |
Correct |
113 ms |
36608 KB |
Output is correct |
91 |
Correct |
137 ms |
36760 KB |
Output is correct |
92 |
Correct |
95 ms |
37156 KB |
Output is correct |
93 |
Correct |
137 ms |
39472 KB |
Output is correct |
94 |
Correct |
88 ms |
41272 KB |
Output is correct |
95 |
Correct |
97 ms |
39852 KB |
Output is correct |
96 |
Correct |
98 ms |
38088 KB |
Output is correct |
97 |
Correct |
116 ms |
36672 KB |
Output is correct |
98 |
Correct |
87 ms |
36056 KB |
Output is correct |
99 |
Correct |
89 ms |
35908 KB |
Output is correct |
100 |
Correct |
119 ms |
39120 KB |
Output is correct |
101 |
Correct |
94 ms |
37104 KB |
Output is correct |
102 |
Correct |
88 ms |
36348 KB |
Output is correct |
103 |
Correct |
89 ms |
36072 KB |
Output is correct |
104 |
Correct |
59 ms |
34920 KB |
Output is correct |
105 |
Correct |
120 ms |
45000 KB |
Output is correct |
106 |
Correct |
64 ms |
35520 KB |
Output is correct |
107 |
Correct |
74 ms |
35520 KB |
Output is correct |
108 |
Correct |
128 ms |
48968 KB |
Output is correct |
109 |
Correct |
71 ms |
34856 KB |
Output is correct |
110 |
Correct |
169 ms |
49204 KB |
Output is correct |
111 |
Correct |
98 ms |
35388 KB |
Output is correct |