#include<bits/stdc++.h>
//#pragma GCC optimize ("O2,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define pc __builtin_popcount
#define cl __builtin_clz
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define debug(x) cerr<<#x<<" : &"<<x<<"&\n"
#define wall() cerr<<'\n'<<"-------------------------------\n"
typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;
typedef pair<bool , pii> piii;
const int
MX=2e5+5,
M=600,
MOD = 1e9+7,
inf = 1e9+20,
//infl = 1e16,
LOG = 18,
A = 27,
del = 10067;
int n, dp[MX][3];
bool mark[MX];
vector<int> adj[MX];
// 0: vasat valid, 1: marz=vasat invalid, 2: birun
void dfs(int u, int p=0){
int cnt=0;
dp[u][0] = -mark[u];
dp[u][1] = -inf;
for(int v:adj[u]) if(v!=p) {
dfs(v, u);
if(max(max((mark[v]?1:-inf), dp[v][0]), dp[v][1]-2*mark[v])>-inf){
cnt++;
dp[u][0]+=max(max((mark[v]?1:-inf), dp[v][0]), dp[v][1]-2*mark[v]);
}
dp[u][1] = max(dp[u][1], mark[u]+max(max((mark[v]?1:-inf), dp[v][0]), dp[v][1]-2*mark[v]));
dp[u][2] = max(max(dp[u][2], dp[v][0]), max(dp[v][2], (mark[v]?dp[v][1]:-inf)));
dp[u][2] = max(dp[u][2], (int)mark[v]);
}
if(cnt<2) dp[u][0] = -inf;
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
FOR(i, 1, n-1){
int u, v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
FOR(i, 1, n){
char c;
cin>>c;
mark[i] = c=='1';
}
dfs(1);
int res=0;
res = max(dp[1][0], res);
res = max(dp[1][2], res);
if(mark[1]) res = max(res, dp[1][1]);
if(mark[1] and !res) res=1;
cout<<res<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
5028 KB |
Output is correct |
12 |
Correct |
4 ms |
4984 KB |
Output is correct |
13 |
Correct |
3 ms |
5028 KB |
Output is correct |
14 |
Correct |
3 ms |
5032 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
5028 KB |
Output is correct |
12 |
Correct |
4 ms |
4984 KB |
Output is correct |
13 |
Correct |
3 ms |
5028 KB |
Output is correct |
14 |
Correct |
3 ms |
5032 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5044 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
6 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5040 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5164 KB |
Output is correct |
30 |
Correct |
3 ms |
5164 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5032 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5044 KB |
Output is correct |
38 |
Correct |
3 ms |
5036 KB |
Output is correct |
39 |
Correct |
4 ms |
5168 KB |
Output is correct |
40 |
Correct |
5 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5168 KB |
Output is correct |
42 |
Correct |
3 ms |
5168 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
5 ms |
5172 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
3 ms |
5076 KB |
Output is correct |
51 |
Correct |
4 ms |
5076 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
5 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5044 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5172 KB |
Output is correct |
58 |
Correct |
3 ms |
5036 KB |
Output is correct |
59 |
Correct |
4 ms |
5080 KB |
Output is correct |
60 |
Correct |
4 ms |
5164 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5168 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
5028 KB |
Output is correct |
12 |
Correct |
4 ms |
4984 KB |
Output is correct |
13 |
Correct |
3 ms |
5028 KB |
Output is correct |
14 |
Correct |
3 ms |
5032 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5044 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
6 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5040 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5164 KB |
Output is correct |
30 |
Correct |
3 ms |
5164 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5032 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5044 KB |
Output is correct |
38 |
Correct |
3 ms |
5036 KB |
Output is correct |
39 |
Correct |
4 ms |
5168 KB |
Output is correct |
40 |
Correct |
5 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5168 KB |
Output is correct |
42 |
Correct |
3 ms |
5168 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
5 ms |
5172 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
3 ms |
5076 KB |
Output is correct |
51 |
Correct |
4 ms |
5076 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
5 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5044 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5172 KB |
Output is correct |
58 |
Correct |
3 ms |
5036 KB |
Output is correct |
59 |
Correct |
4 ms |
5080 KB |
Output is correct |
60 |
Correct |
4 ms |
5164 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5168 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
64 |
Correct |
133 ms |
16632 KB |
Output is correct |
65 |
Correct |
133 ms |
16520 KB |
Output is correct |
66 |
Correct |
158 ms |
16564 KB |
Output is correct |
67 |
Correct |
137 ms |
16708 KB |
Output is correct |
68 |
Correct |
143 ms |
16588 KB |
Output is correct |
69 |
Correct |
141 ms |
16592 KB |
Output is correct |
70 |
Correct |
179 ms |
16632 KB |
Output is correct |
71 |
Correct |
135 ms |
16612 KB |
Output is correct |
72 |
Correct |
135 ms |
16596 KB |
Output is correct |
73 |
Correct |
153 ms |
29212 KB |
Output is correct |
74 |
Correct |
163 ms |
29196 KB |
Output is correct |
75 |
Correct |
131 ms |
16524 KB |
Output is correct |
76 |
Correct |
143 ms |
16524 KB |
Output is correct |
77 |
Correct |
130 ms |
16504 KB |
Output is correct |
78 |
Correct |
172 ms |
16460 KB |
Output is correct |
79 |
Correct |
134 ms |
16428 KB |
Output is correct |
80 |
Correct |
133 ms |
16424 KB |
Output is correct |
81 |
Correct |
131 ms |
16848 KB |
Output is correct |
82 |
Correct |
143 ms |
16688 KB |
Output is correct |
83 |
Correct |
148 ms |
21672 KB |
Output is correct |
84 |
Correct |
141 ms |
23808 KB |
Output is correct |
85 |
Correct |
165 ms |
23556 KB |
Output is correct |
86 |
Correct |
155 ms |
18020 KB |
Output is correct |
87 |
Correct |
128 ms |
17336 KB |
Output is correct |
88 |
Correct |
120 ms |
17964 KB |
Output is correct |
89 |
Correct |
130 ms |
17868 KB |
Output is correct |
90 |
Correct |
121 ms |
17596 KB |
Output is correct |
91 |
Correct |
149 ms |
17884 KB |
Output is correct |
92 |
Correct |
121 ms |
18324 KB |
Output is correct |
93 |
Correct |
131 ms |
20512 KB |
Output is correct |
94 |
Correct |
117 ms |
22180 KB |
Output is correct |
95 |
Correct |
119 ms |
20988 KB |
Output is correct |
96 |
Correct |
137 ms |
19236 KB |
Output is correct |
97 |
Correct |
120 ms |
17684 KB |
Output is correct |
98 |
Correct |
120 ms |
17196 KB |
Output is correct |
99 |
Correct |
118 ms |
16968 KB |
Output is correct |
100 |
Correct |
147 ms |
20120 KB |
Output is correct |
101 |
Correct |
124 ms |
18148 KB |
Output is correct |
102 |
Correct |
130 ms |
17376 KB |
Output is correct |
103 |
Correct |
143 ms |
17200 KB |
Output is correct |
104 |
Correct |
77 ms |
17324 KB |
Output is correct |
105 |
Correct |
144 ms |
26052 KB |
Output is correct |
106 |
Correct |
77 ms |
17220 KB |
Output is correct |
107 |
Correct |
77 ms |
17172 KB |
Output is correct |
108 |
Correct |
158 ms |
29988 KB |
Output is correct |
109 |
Correct |
97 ms |
17356 KB |
Output is correct |
110 |
Correct |
141 ms |
30236 KB |
Output is correct |
111 |
Correct |
116 ms |
16472 KB |
Output is correct |