#include<bits/stdc++.h>
#define fo(i,l,r) for(int i = l ; i <= r ; i ++)
#define fod(i,r,l) for(int i = r ; i >= l ; i--)
#define pb emplace_back
#define int long long
#define maxn 1000010
#define N 1010
#define inf (int)1e18
#define bitcount(x) __builtint_popcount(x)
#define bit(x,i) ((x >> i) & 1)
#define onbit(x,i) (x | (1ll << i))
#define offbit(x,i) (x ^ (1ll << i))
#define lb(x) x & -x
#define all(x) x.begin(),x.end()
#define ss(x) (int)x.size()
#define en cout << "\n";
#define vi vector<int>
#define pii pair<int,int>
#define vii vector<pii>
using namespace std;
bool maximize(int &a,int b)
{
if(a <= b)
{
a = b;
return 1;
}
return 0;
}
bool minimize(int &a,int b)
{
if(a >= b)
{
a = b;
return 1;
}
return 0;
}
int n,a[maxn],f[maxn];
vi ke[maxn];
int ans = 0;
void dfs(int u,int par = 0)
{
int sum = 0,maxx = 0;
for(int v : ke[u]) if(v != par)
{
dfs(v,u);
maximize(maxx,f[v]);
sum += f[v];
}
if(a[u])
{
maximize(ans,maxx + 1);
maximize(ans,sum - 1);
}
else maximize(ans,sum);
f[u] = max(a[u],sum - a[u]);
}
main()
{
#define name "TASK"
if(fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
fo(i,1,n - 1)
{
int u,v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
fo(i,1,n)
{
char t;
cin >> t;
a[i] = t - '0';
}
dfs(1);
cout << ans;
}
Compilation message
power.cpp:59:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
59 | main()
| ^~~~
power.cpp: In function 'int main()':
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(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
power.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
23900 KB |
Output is correct |
3 |
Correct |
12 ms |
23880 KB |
Output is correct |
4 |
Correct |
13 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23796 KB |
Output is correct |
7 |
Correct |
12 ms |
24152 KB |
Output is correct |
8 |
Correct |
12 ms |
23900 KB |
Output is correct |
9 |
Correct |
12 ms |
23956 KB |
Output is correct |
10 |
Correct |
11 ms |
23944 KB |
Output is correct |
11 |
Correct |
12 ms |
23748 KB |
Output is correct |
12 |
Correct |
15 ms |
24156 KB |
Output is correct |
13 |
Correct |
13 ms |
23792 KB |
Output is correct |
14 |
Correct |
11 ms |
23900 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
11 ms |
23900 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
11 ms |
23876 KB |
Output is correct |
19 |
Correct |
11 ms |
23896 KB |
Output is correct |
20 |
Correct |
12 ms |
23900 KB |
Output is correct |
21 |
Correct |
17 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
23900 KB |
Output is correct |
3 |
Correct |
12 ms |
23880 KB |
Output is correct |
4 |
Correct |
13 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23796 KB |
Output is correct |
7 |
Correct |
12 ms |
24152 KB |
Output is correct |
8 |
Correct |
12 ms |
23900 KB |
Output is correct |
9 |
Correct |
12 ms |
23956 KB |
Output is correct |
10 |
Correct |
11 ms |
23944 KB |
Output is correct |
11 |
Correct |
12 ms |
23748 KB |
Output is correct |
12 |
Correct |
15 ms |
24156 KB |
Output is correct |
13 |
Correct |
13 ms |
23792 KB |
Output is correct |
14 |
Correct |
11 ms |
23900 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
11 ms |
23900 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
11 ms |
23876 KB |
Output is correct |
19 |
Correct |
11 ms |
23896 KB |
Output is correct |
20 |
Correct |
12 ms |
23900 KB |
Output is correct |
21 |
Correct |
17 ms |
23900 KB |
Output is correct |
22 |
Correct |
13 ms |
23900 KB |
Output is correct |
23 |
Correct |
12 ms |
23900 KB |
Output is correct |
24 |
Correct |
11 ms |
23900 KB |
Output is correct |
25 |
Correct |
13 ms |
24088 KB |
Output is correct |
26 |
Correct |
14 ms |
23904 KB |
Output is correct |
27 |
Correct |
13 ms |
23900 KB |
Output is correct |
28 |
Correct |
11 ms |
24080 KB |
Output is correct |
29 |
Correct |
12 ms |
23968 KB |
Output is correct |
30 |
Correct |
12 ms |
24156 KB |
Output is correct |
31 |
Correct |
11 ms |
24044 KB |
Output is correct |
32 |
Correct |
11 ms |
24052 KB |
Output is correct |
33 |
Correct |
12 ms |
23900 KB |
Output is correct |
34 |
Correct |
12 ms |
23900 KB |
Output is correct |
35 |
Correct |
12 ms |
23896 KB |
Output is correct |
36 |
Correct |
12 ms |
23916 KB |
Output is correct |
37 |
Correct |
13 ms |
24156 KB |
Output is correct |
38 |
Correct |
17 ms |
24048 KB |
Output is correct |
39 |
Correct |
11 ms |
23896 KB |
Output is correct |
40 |
Correct |
15 ms |
23884 KB |
Output is correct |
41 |
Correct |
13 ms |
23900 KB |
Output is correct |
42 |
Correct |
13 ms |
23900 KB |
Output is correct |
43 |
Correct |
12 ms |
23900 KB |
Output is correct |
44 |
Correct |
12 ms |
23900 KB |
Output is correct |
45 |
Correct |
12 ms |
23900 KB |
Output is correct |
46 |
Correct |
12 ms |
23900 KB |
Output is correct |
47 |
Correct |
13 ms |
23900 KB |
Output is correct |
48 |
Correct |
12 ms |
24032 KB |
Output is correct |
49 |
Correct |
11 ms |
24156 KB |
Output is correct |
50 |
Correct |
14 ms |
24124 KB |
Output is correct |
51 |
Correct |
10 ms |
24028 KB |
Output is correct |
52 |
Correct |
11 ms |
23904 KB |
Output is correct |
53 |
Correct |
12 ms |
23896 KB |
Output is correct |
54 |
Correct |
12 ms |
24108 KB |
Output is correct |
55 |
Correct |
11 ms |
23900 KB |
Output is correct |
56 |
Correct |
11 ms |
23900 KB |
Output is correct |
57 |
Correct |
12 ms |
23960 KB |
Output is correct |
58 |
Correct |
12 ms |
24088 KB |
Output is correct |
59 |
Correct |
12 ms |
24092 KB |
Output is correct |
60 |
Correct |
11 ms |
23996 KB |
Output is correct |
61 |
Correct |
14 ms |
23900 KB |
Output is correct |
62 |
Correct |
13 ms |
24156 KB |
Output is correct |
63 |
Correct |
12 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
23900 KB |
Output is correct |
3 |
Correct |
12 ms |
23880 KB |
Output is correct |
4 |
Correct |
13 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23796 KB |
Output is correct |
7 |
Correct |
12 ms |
24152 KB |
Output is correct |
8 |
Correct |
12 ms |
23900 KB |
Output is correct |
9 |
Correct |
12 ms |
23956 KB |
Output is correct |
10 |
Correct |
11 ms |
23944 KB |
Output is correct |
11 |
Correct |
12 ms |
23748 KB |
Output is correct |
12 |
Correct |
15 ms |
24156 KB |
Output is correct |
13 |
Correct |
13 ms |
23792 KB |
Output is correct |
14 |
Correct |
11 ms |
23900 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
11 ms |
23900 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
11 ms |
23876 KB |
Output is correct |
19 |
Correct |
11 ms |
23896 KB |
Output is correct |
20 |
Correct |
12 ms |
23900 KB |
Output is correct |
21 |
Correct |
17 ms |
23900 KB |
Output is correct |
22 |
Correct |
13 ms |
23900 KB |
Output is correct |
23 |
Correct |
12 ms |
23900 KB |
Output is correct |
24 |
Correct |
11 ms |
23900 KB |
Output is correct |
25 |
Correct |
13 ms |
24088 KB |
Output is correct |
26 |
Correct |
14 ms |
23904 KB |
Output is correct |
27 |
Correct |
13 ms |
23900 KB |
Output is correct |
28 |
Correct |
11 ms |
24080 KB |
Output is correct |
29 |
Correct |
12 ms |
23968 KB |
Output is correct |
30 |
Correct |
12 ms |
24156 KB |
Output is correct |
31 |
Correct |
11 ms |
24044 KB |
Output is correct |
32 |
Correct |
11 ms |
24052 KB |
Output is correct |
33 |
Correct |
12 ms |
23900 KB |
Output is correct |
34 |
Correct |
12 ms |
23900 KB |
Output is correct |
35 |
Correct |
12 ms |
23896 KB |
Output is correct |
36 |
Correct |
12 ms |
23916 KB |
Output is correct |
37 |
Correct |
13 ms |
24156 KB |
Output is correct |
38 |
Correct |
17 ms |
24048 KB |
Output is correct |
39 |
Correct |
11 ms |
23896 KB |
Output is correct |
40 |
Correct |
15 ms |
23884 KB |
Output is correct |
41 |
Correct |
13 ms |
23900 KB |
Output is correct |
42 |
Correct |
13 ms |
23900 KB |
Output is correct |
43 |
Correct |
12 ms |
23900 KB |
Output is correct |
44 |
Correct |
12 ms |
23900 KB |
Output is correct |
45 |
Correct |
12 ms |
23900 KB |
Output is correct |
46 |
Correct |
12 ms |
23900 KB |
Output is correct |
47 |
Correct |
13 ms |
23900 KB |
Output is correct |
48 |
Correct |
12 ms |
24032 KB |
Output is correct |
49 |
Correct |
11 ms |
24156 KB |
Output is correct |
50 |
Correct |
14 ms |
24124 KB |
Output is correct |
51 |
Correct |
10 ms |
24028 KB |
Output is correct |
52 |
Correct |
11 ms |
23904 KB |
Output is correct |
53 |
Correct |
12 ms |
23896 KB |
Output is correct |
54 |
Correct |
12 ms |
24108 KB |
Output is correct |
55 |
Correct |
11 ms |
23900 KB |
Output is correct |
56 |
Correct |
11 ms |
23900 KB |
Output is correct |
57 |
Correct |
12 ms |
23960 KB |
Output is correct |
58 |
Correct |
12 ms |
24088 KB |
Output is correct |
59 |
Correct |
12 ms |
24092 KB |
Output is correct |
60 |
Correct |
11 ms |
23996 KB |
Output is correct |
61 |
Correct |
14 ms |
23900 KB |
Output is correct |
62 |
Correct |
13 ms |
24156 KB |
Output is correct |
63 |
Correct |
12 ms |
23900 KB |
Output is correct |
64 |
Correct |
88 ms |
37004 KB |
Output is correct |
65 |
Correct |
83 ms |
36948 KB |
Output is correct |
66 |
Correct |
82 ms |
36948 KB |
Output is correct |
67 |
Correct |
95 ms |
37004 KB |
Output is correct |
68 |
Correct |
85 ms |
37120 KB |
Output is correct |
69 |
Correct |
88 ms |
36944 KB |
Output is correct |
70 |
Correct |
75 ms |
36948 KB |
Output is correct |
71 |
Correct |
102 ms |
37132 KB |
Output is correct |
72 |
Correct |
96 ms |
36940 KB |
Output is correct |
73 |
Correct |
99 ms |
48748 KB |
Output is correct |
74 |
Correct |
95 ms |
48464 KB |
Output is correct |
75 |
Correct |
96 ms |
38220 KB |
Output is correct |
76 |
Correct |
121 ms |
38248 KB |
Output is correct |
77 |
Correct |
85 ms |
36944 KB |
Output is correct |
78 |
Correct |
105 ms |
36988 KB |
Output is correct |
79 |
Correct |
92 ms |
37100 KB |
Output is correct |
80 |
Correct |
108 ms |
36880 KB |
Output is correct |
81 |
Correct |
80 ms |
37064 KB |
Output is correct |
82 |
Correct |
105 ms |
36812 KB |
Output is correct |
83 |
Correct |
80 ms |
42064 KB |
Output is correct |
84 |
Correct |
88 ms |
44280 KB |
Output is correct |
85 |
Correct |
125 ms |
43864 KB |
Output is correct |
86 |
Correct |
85 ms |
38484 KB |
Output is correct |
87 |
Correct |
102 ms |
37672 KB |
Output is correct |
88 |
Correct |
83 ms |
37968 KB |
Output is correct |
89 |
Correct |
98 ms |
37908 KB |
Output is correct |
90 |
Correct |
96 ms |
37612 KB |
Output is correct |
91 |
Correct |
81 ms |
37968 KB |
Output is correct |
92 |
Correct |
79 ms |
38304 KB |
Output is correct |
93 |
Correct |
74 ms |
40484 KB |
Output is correct |
94 |
Correct |
80 ms |
41992 KB |
Output is correct |
95 |
Correct |
73 ms |
40820 KB |
Output is correct |
96 |
Correct |
74 ms |
39196 KB |
Output is correct |
97 |
Correct |
82 ms |
37712 KB |
Output is correct |
98 |
Correct |
71 ms |
37092 KB |
Output is correct |
99 |
Correct |
92 ms |
36988 KB |
Output is correct |
100 |
Correct |
75 ms |
40272 KB |
Output is correct |
101 |
Correct |
82 ms |
37948 KB |
Output is correct |
102 |
Correct |
77 ms |
37200 KB |
Output is correct |
103 |
Correct |
84 ms |
37200 KB |
Output is correct |
104 |
Correct |
57 ms |
37568 KB |
Output is correct |
105 |
Correct |
86 ms |
45396 KB |
Output is correct |
106 |
Correct |
56 ms |
37312 KB |
Output is correct |
107 |
Correct |
56 ms |
37316 KB |
Output is correct |
108 |
Correct |
89 ms |
49352 KB |
Output is correct |
109 |
Correct |
72 ms |
37572 KB |
Output is correct |
110 |
Correct |
87 ms |
49748 KB |
Output is correct |
111 |
Correct |
70 ms |
37080 KB |
Output is correct |