#pragma GCC optimize("Ofast")
#pragma GCC target("sse4,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ts to_string
#define ub upper_bound
#define lb lower_bound
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, y, z) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, z, 0)
#define trav(x, y) for(auto&x:y)
const string nl = "\n";
using ll = int64_t;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
using vpii = vector<pii>;
using vpll = vector<pll>;
template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void usaco(string x=""){
ios_base::sync_with_stdio(0); cin.tie(0); x=x;
#ifndef D
if(siz(x)){
freopen((x+".in").c_str(), "r", stdin);
freopen((x+".out").c_str(), "w", stdout);
}
#endif
}
str ts(char c) { return str(1,c); }
str ts(bool b) { return b ? "true" : "false"; }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
template<class A> str ts(complex<A> c) {
stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) {
str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
return res; }
template<class A, class B> str ts(pair<A,B> p);
template<class T> str ts(T v) {
bool fst = 1; str res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += ts(x);
}
res += "}"; return res;
}
template<class A, class B> str ts(pair<A,B> p) {
return "("+ts(p.f)+", "+ts(p.s)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << ts(h); if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef D
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
const int mxN = 2e5 + 10;
int n, dp[mxN], a[mxN], ans = 0;
vi g[mxN];
void dfs(int u, int p){
int mx = 0, sum = 0;
trav(v, g[u]){
if(v!=p){
dfs(v, u);
ckmax(mx, dp[v]); sum += dp[v];
}
}
if(a[u]){
ckmax(ans, max(mx + 1, sum - 1));
dp[u]=max(sum-1, 1);
}
else{
ckmax(ans, sum);
dp[u]=sum;
}
}
signed main(){
usaco("");
cin >> n;
F0R(i, n-1){
int e1, e2; cin >> e1 >> e2;
e1--; e2--;
g[e1].pb(e2);
g[e2].pb(e1);
}
F0R(i, n){
char x; cin >>x;
a[i] = (x=='1');
}
dfs(0,-1);
cout << ans << nl;
}
/*
* Stuff I should try:
* Is it Monotonic? -> Binary Search
* Is N small? -> Bitsets or other exponential
* Edge Cases? Look Carefully
* Can't figure anything out? Brute Force and see patterns
* Try to prove greedies
* Prefix Sums, Amortization, DP, Data Structures
*/
Compilation message
power.cpp: In function 'void usaco(std::string)':
power.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
41 | freopen((x+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
42 | freopen((x+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
3 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
3 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5248 KB |
Output is correct |
31 |
Correct |
6 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5120 KB |
Output is correct |
42 |
Correct |
4 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
4 ms |
5120 KB |
Output is correct |
47 |
Correct |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
4 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
4 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
4 ms |
5248 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
3 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5248 KB |
Output is correct |
31 |
Correct |
6 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5120 KB |
Output is correct |
42 |
Correct |
4 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
4 ms |
5120 KB |
Output is correct |
47 |
Correct |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
4 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
4 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
4 ms |
5248 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
64 |
Correct |
168 ms |
12920 KB |
Output is correct |
65 |
Correct |
164 ms |
12920 KB |
Output is correct |
66 |
Correct |
174 ms |
12920 KB |
Output is correct |
67 |
Correct |
167 ms |
12920 KB |
Output is correct |
68 |
Correct |
163 ms |
12920 KB |
Output is correct |
69 |
Correct |
163 ms |
12920 KB |
Output is correct |
70 |
Correct |
158 ms |
12920 KB |
Output is correct |
71 |
Correct |
159 ms |
12920 KB |
Output is correct |
72 |
Correct |
168 ms |
12924 KB |
Output is correct |
73 |
Correct |
184 ms |
25636 KB |
Output is correct |
74 |
Correct |
186 ms |
25464 KB |
Output is correct |
75 |
Correct |
168 ms |
12816 KB |
Output is correct |
76 |
Correct |
170 ms |
12796 KB |
Output is correct |
77 |
Correct |
172 ms |
12848 KB |
Output is correct |
78 |
Correct |
180 ms |
12792 KB |
Output is correct |
79 |
Correct |
170 ms |
12924 KB |
Output is correct |
80 |
Correct |
165 ms |
12792 KB |
Output is correct |
81 |
Correct |
141 ms |
13300 KB |
Output is correct |
82 |
Correct |
155 ms |
13044 KB |
Output is correct |
83 |
Correct |
176 ms |
18044 KB |
Output is correct |
84 |
Correct |
176 ms |
20216 KB |
Output is correct |
85 |
Correct |
177 ms |
19960 KB |
Output is correct |
86 |
Correct |
172 ms |
14328 KB |
Output is correct |
87 |
Correct |
169 ms |
13812 KB |
Output is correct |
88 |
Correct |
158 ms |
14200 KB |
Output is correct |
89 |
Correct |
156 ms |
14200 KB |
Output is correct |
90 |
Correct |
156 ms |
13944 KB |
Output is correct |
91 |
Correct |
161 ms |
14328 KB |
Output is correct |
92 |
Correct |
159 ms |
14584 KB |
Output is correct |
93 |
Correct |
149 ms |
17044 KB |
Output is correct |
94 |
Correct |
144 ms |
18760 KB |
Output is correct |
95 |
Correct |
147 ms |
17396 KB |
Output is correct |
96 |
Correct |
156 ms |
15480 KB |
Output is correct |
97 |
Correct |
144 ms |
14072 KB |
Output is correct |
98 |
Correct |
151 ms |
13560 KB |
Output is correct |
99 |
Correct |
163 ms |
13304 KB |
Output is correct |
100 |
Correct |
153 ms |
16504 KB |
Output is correct |
101 |
Correct |
174 ms |
14456 KB |
Output is correct |
102 |
Correct |
155 ms |
13772 KB |
Output is correct |
103 |
Correct |
157 ms |
13560 KB |
Output is correct |
104 |
Correct |
95 ms |
13672 KB |
Output is correct |
105 |
Correct |
184 ms |
22392 KB |
Output is correct |
106 |
Correct |
99 ms |
13928 KB |
Output is correct |
107 |
Correct |
96 ms |
13696 KB |
Output is correct |
108 |
Correct |
187 ms |
26336 KB |
Output is correct |
109 |
Correct |
104 ms |
13676 KB |
Output is correct |
110 |
Correct |
191 ms |
26744 KB |
Output is correct |
111 |
Correct |
157 ms |
12792 KB |
Output is correct |