#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "BAI1"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
inline int readInt() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();int n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline ll readLong() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();ll n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline string readString() {char c;while(c=getchar(),c==' '||c=='\n'||c=='\t');string s({c});while(c=getchar(),c!=EOF&&c!=' '&&c!='\n'&&c!='\t')s+=c;return s;}
const int N = 2e5 + 3;
const int M = 1e3 + 3;
const int LOG = 50;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int n;
vector<int> g[N];
string s;
int dp[N][2];
int res = 0;
void dfs(int u, int p)
{
for(int v : g[u]) if(v != p)
{
dfs(v, u);
maximize(dp[u][0], max(dp[v][0], dp[v][1]));
}
dp[u][1] = s[u - 1] - '0';
int cur = -(s[u - 1] - '0');
for(int v : g[u]) if(v != p)
cur += max(0, dp[v][1]);
int Max = 0;
for(int v : g[u]) if(v != p) maximize(Max, dp[v][1]);
maximize(dp[u][1], cur);
maximize(res, dp[u][1]);
maximize(res, Max + (s[u - 1] - '0'));
}
void solve()
{
cin >> n;
rep(i, n - 1)
{
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
cin >> s;
dfs(1, 1);
cout << res;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1;
if(fopen("note.inp", "r"))
{
freopen("note.inp", "r", stdin);
freopen("note.out", "w", stdout);
}
while(TC--)
{
solve();
cout << endl;
}
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("note.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen("note.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
3 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
5208 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
5152 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
3 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
5208 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
5152 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5208 KB |
Output is correct |
25 |
Correct |
3 ms |
5212 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5232 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5212 KB |
Output is correct |
34 |
Correct |
3 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5172 KB |
Output is correct |
36 |
Correct |
3 ms |
5464 KB |
Output is correct |
37 |
Correct |
2 ms |
5176 KB |
Output is correct |
38 |
Correct |
2 ms |
5208 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5168 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5208 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
3 ms |
5212 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5212 KB |
Output is correct |
49 |
Correct |
3 ms |
5212 KB |
Output is correct |
50 |
Correct |
3 ms |
5216 KB |
Output is correct |
51 |
Correct |
3 ms |
5252 KB |
Output is correct |
52 |
Correct |
3 ms |
5212 KB |
Output is correct |
53 |
Correct |
3 ms |
5212 KB |
Output is correct |
54 |
Correct |
3 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 |
3 ms |
5212 KB |
Output is correct |
58 |
Correct |
5 ms |
5212 KB |
Output is correct |
59 |
Correct |
3 ms |
5464 KB |
Output is correct |
60 |
Correct |
4 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5212 KB |
Output is correct |
63 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
3 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
5208 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
5152 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5208 KB |
Output is correct |
25 |
Correct |
3 ms |
5212 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5232 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5212 KB |
Output is correct |
34 |
Correct |
3 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5172 KB |
Output is correct |
36 |
Correct |
3 ms |
5464 KB |
Output is correct |
37 |
Correct |
2 ms |
5176 KB |
Output is correct |
38 |
Correct |
2 ms |
5208 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5168 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5208 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
3 ms |
5212 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5212 KB |
Output is correct |
49 |
Correct |
3 ms |
5212 KB |
Output is correct |
50 |
Correct |
3 ms |
5216 KB |
Output is correct |
51 |
Correct |
3 ms |
5252 KB |
Output is correct |
52 |
Correct |
3 ms |
5212 KB |
Output is correct |
53 |
Correct |
3 ms |
5212 KB |
Output is correct |
54 |
Correct |
3 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 |
3 ms |
5212 KB |
Output is correct |
58 |
Correct |
5 ms |
5212 KB |
Output is correct |
59 |
Correct |
3 ms |
5464 KB |
Output is correct |
60 |
Correct |
4 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5212 KB |
Output is correct |
63 |
Correct |
3 ms |
5208 KB |
Output is correct |
64 |
Correct |
69 ms |
16024 KB |
Output is correct |
65 |
Correct |
65 ms |
16092 KB |
Output is correct |
66 |
Correct |
60 ms |
15980 KB |
Output is correct |
67 |
Correct |
66 ms |
16016 KB |
Output is correct |
68 |
Correct |
68 ms |
16004 KB |
Output is correct |
69 |
Correct |
69 ms |
16024 KB |
Output is correct |
70 |
Correct |
62 ms |
16032 KB |
Output is correct |
71 |
Correct |
60 ms |
16032 KB |
Output is correct |
72 |
Correct |
70 ms |
16016 KB |
Output is correct |
73 |
Correct |
70 ms |
28564 KB |
Output is correct |
74 |
Correct |
76 ms |
28668 KB |
Output is correct |
75 |
Correct |
61 ms |
15820 KB |
Output is correct |
76 |
Correct |
61 ms |
15768 KB |
Output is correct |
77 |
Correct |
73 ms |
16176 KB |
Output is correct |
78 |
Correct |
67 ms |
15984 KB |
Output is correct |
79 |
Correct |
63 ms |
15824 KB |
Output is correct |
80 |
Correct |
60 ms |
15860 KB |
Output is correct |
81 |
Correct |
52 ms |
16332 KB |
Output is correct |
82 |
Correct |
69 ms |
16204 KB |
Output is correct |
83 |
Correct |
66 ms |
21144 KB |
Output is correct |
84 |
Correct |
79 ms |
23204 KB |
Output is correct |
85 |
Correct |
72 ms |
22944 KB |
Output is correct |
86 |
Correct |
65 ms |
17300 KB |
Output is correct |
87 |
Correct |
71 ms |
16780 KB |
Output is correct |
88 |
Correct |
62 ms |
17100 KB |
Output is correct |
89 |
Correct |
63 ms |
17312 KB |
Output is correct |
90 |
Correct |
67 ms |
17136 KB |
Output is correct |
91 |
Correct |
66 ms |
17304 KB |
Output is correct |
92 |
Correct |
60 ms |
17496 KB |
Output is correct |
93 |
Correct |
66 ms |
20056 KB |
Output is correct |
94 |
Correct |
60 ms |
21704 KB |
Output is correct |
95 |
Correct |
58 ms |
20300 KB |
Output is correct |
96 |
Correct |
59 ms |
18680 KB |
Output is correct |
97 |
Correct |
68 ms |
17132 KB |
Output is correct |
98 |
Correct |
60 ms |
16576 KB |
Output is correct |
99 |
Correct |
60 ms |
16296 KB |
Output is correct |
100 |
Correct |
66 ms |
19584 KB |
Output is correct |
101 |
Correct |
61 ms |
17420 KB |
Output is correct |
102 |
Correct |
56 ms |
16736 KB |
Output is correct |
103 |
Correct |
59 ms |
16564 KB |
Output is correct |
104 |
Correct |
39 ms |
16840 KB |
Output is correct |
105 |
Correct |
72 ms |
25412 KB |
Output is correct |
106 |
Correct |
42 ms |
16848 KB |
Output is correct |
107 |
Correct |
42 ms |
16736 KB |
Output is correct |
108 |
Correct |
81 ms |
29356 KB |
Output is correct |
109 |
Correct |
41 ms |
16840 KB |
Output is correct |
110 |
Correct |
78 ms |
29584 KB |
Output is correct |
111 |
Correct |
55 ms |
15776 KB |
Output is correct |