/*input
16
7 10
5 11
9 4
14 12
2 11
14 16
4 2
1 13
11 3
7 1
15 9
2 1
11 6
14 9
8 9
0111111001001110
*/
/**
Author: Kristopher Paul
Date Created: 30-09-2020
**/
#include<bits/stdc++.h>
#include<stdio.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define int ll
#define pb push_back
#define INF 1e18
//#define MOD 1000000007
#define MOD 998244353
#define mp make_pair
const double PI=3.141592653589793238462643383279502884197169399375105820974944;
#define REP(i,n) for (int i = 0; i < n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define umap map
#define pii pair<int,int>
#define F first
#define S second
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define itr :: iterator it
#define all(v) v.begin(),v.end()
#define WL(t) while(t--)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define out(x) cout << #x << " is " << x << endl
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
//Uncomment for File I/O
//ifstream fin("input.in")
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds; // set
//typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> pbds; // multiset
int ModExp(int x,int y,int m){
int res = 1;
x = x % m;
while (y > 0)
{
if (y & 1)
res = (res*x) % m;
y = y>>1;
x = (x*x) % m;
}
return res;
}
//dp[u][0] -> max profit in subtree when there is no activated generator outside subtree
//dp[u][1] -> max profit in subtree when there is activated generator outside subtree
vector<int> adj[200005];
int type[200005] = {};
int dp[200005][2];
void dfs(int cv,int p){
dp[cv][1] = 0;
int mx0 = 0,mx1 = 0;
FOR(i,0,adj[cv].size()){
if(adj[cv][i] != p){
dfs(adj[cv][i],cv);
dp[cv][1] += dp[adj[cv][i]][1];
remax(mx1,dp[adj[cv][i]][1]);
remax(mx0,dp[adj[cv][i]][0]);
}
}
dp[cv][1] = max(type[cv],dp[cv][1]-type[cv]);
dp[cv][0] = dp[cv][1];
remax(dp[cv][0],mx0);
remax(dp[cv][0],type[cv]+mx1);
}
void solve(){
int n;
cin >> n;
FOR(i,0,n-1){
int u,v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
string s;
cin >> s;
FOR(i,1,n+1){
type[i] = s[i-1]-'0';
}
dfs(1,0);
cout << dp[1][0] << endl;
}
signed main(){
FastIO;
int t = 1;
// cin >> t;
WL(t){
solve();
}
}
Compilation message
power.cpp: In function 'void dfs(long long int, long long int)':
power.cpp:37:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | #define FOR(i,a,b) for (int i = a; i < b; i++)
......
90 | FOR(i,0,adj[cv].size()){
| ~~~~~~~~~~~~~~~~~~
power.cpp:90:5: note: in expansion of macro 'FOR'
90 | FOR(i,0,adj[cv].size()){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 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 |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
5 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 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 |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
5 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 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 |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 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 |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5376 KB |
Output is correct |
31 |
Correct |
4 ms |
5248 KB |
Output is correct |
32 |
Correct |
4 ms |
5248 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 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 |
5 ms |
5248 KB |
Output is correct |
40 |
Correct |
5 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5248 KB |
Output is correct |
43 |
Correct |
5 ms |
5248 KB |
Output is correct |
44 |
Correct |
4 ms |
5248 KB |
Output is correct |
45 |
Correct |
5 ms |
5248 KB |
Output is correct |
46 |
Correct |
5 ms |
5248 KB |
Output is correct |
47 |
Correct |
5 ms |
5248 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5248 KB |
Output is correct |
51 |
Correct |
4 ms |
5248 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5248 KB |
Output is correct |
54 |
Correct |
4 ms |
5248 KB |
Output is correct |
55 |
Correct |
5 ms |
5248 KB |
Output is correct |
56 |
Correct |
4 ms |
5248 KB |
Output is correct |
57 |
Correct |
4 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5248 KB |
Output is correct |
59 |
Correct |
4 ms |
5248 KB |
Output is correct |
60 |
Correct |
5 ms |
5248 KB |
Output is correct |
61 |
Correct |
5 ms |
5248 KB |
Output is correct |
62 |
Correct |
5 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 |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 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 |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
5 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 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 |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 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 |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5376 KB |
Output is correct |
31 |
Correct |
4 ms |
5248 KB |
Output is correct |
32 |
Correct |
4 ms |
5248 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 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 |
5 ms |
5248 KB |
Output is correct |
40 |
Correct |
5 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5248 KB |
Output is correct |
43 |
Correct |
5 ms |
5248 KB |
Output is correct |
44 |
Correct |
4 ms |
5248 KB |
Output is correct |
45 |
Correct |
5 ms |
5248 KB |
Output is correct |
46 |
Correct |
5 ms |
5248 KB |
Output is correct |
47 |
Correct |
5 ms |
5248 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5248 KB |
Output is correct |
51 |
Correct |
4 ms |
5248 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5248 KB |
Output is correct |
54 |
Correct |
4 ms |
5248 KB |
Output is correct |
55 |
Correct |
5 ms |
5248 KB |
Output is correct |
56 |
Correct |
4 ms |
5248 KB |
Output is correct |
57 |
Correct |
4 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5248 KB |
Output is correct |
59 |
Correct |
4 ms |
5248 KB |
Output is correct |
60 |
Correct |
5 ms |
5248 KB |
Output is correct |
61 |
Correct |
5 ms |
5248 KB |
Output is correct |
62 |
Correct |
5 ms |
5248 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
64 |
Correct |
193 ms |
20156 KB |
Output is correct |
65 |
Correct |
179 ms |
20100 KB |
Output is correct |
66 |
Correct |
180 ms |
20264 KB |
Output is correct |
67 |
Correct |
179 ms |
20148 KB |
Output is correct |
68 |
Correct |
180 ms |
20264 KB |
Output is correct |
69 |
Correct |
178 ms |
20276 KB |
Output is correct |
70 |
Correct |
187 ms |
20296 KB |
Output is correct |
71 |
Correct |
198 ms |
20164 KB |
Output is correct |
72 |
Correct |
186 ms |
20148 KB |
Output is correct |
73 |
Correct |
207 ms |
31804 KB |
Output is correct |
74 |
Correct |
211 ms |
31672 KB |
Output is correct |
75 |
Correct |
181 ms |
21300 KB |
Output is correct |
76 |
Correct |
206 ms |
21308 KB |
Output is correct |
77 |
Correct |
192 ms |
20052 KB |
Output is correct |
78 |
Correct |
193 ms |
20152 KB |
Output is correct |
79 |
Correct |
196 ms |
20156 KB |
Output is correct |
80 |
Correct |
191 ms |
20276 KB |
Output is correct |
81 |
Correct |
151 ms |
20204 KB |
Output is correct |
82 |
Correct |
176 ms |
20204 KB |
Output is correct |
83 |
Correct |
196 ms |
25276 KB |
Output is correct |
84 |
Correct |
199 ms |
27336 KB |
Output is correct |
85 |
Correct |
200 ms |
27076 KB |
Output is correct |
86 |
Correct |
184 ms |
21560 KB |
Output is correct |
87 |
Correct |
190 ms |
20916 KB |
Output is correct |
88 |
Correct |
165 ms |
21108 KB |
Output is correct |
89 |
Correct |
177 ms |
21060 KB |
Output is correct |
90 |
Correct |
198 ms |
20888 KB |
Output is correct |
91 |
Correct |
168 ms |
21016 KB |
Output is correct |
92 |
Correct |
201 ms |
21416 KB |
Output is correct |
93 |
Correct |
176 ms |
23668 KB |
Output is correct |
94 |
Correct |
167 ms |
25324 KB |
Output is correct |
95 |
Correct |
169 ms |
24040 KB |
Output is correct |
96 |
Correct |
167 ms |
22328 KB |
Output is correct |
97 |
Correct |
179 ms |
20716 KB |
Output is correct |
98 |
Correct |
171 ms |
20328 KB |
Output is correct |
99 |
Correct |
184 ms |
20276 KB |
Output is correct |
100 |
Correct |
174 ms |
23204 KB |
Output is correct |
101 |
Correct |
170 ms |
21044 KB |
Output is correct |
102 |
Correct |
160 ms |
20488 KB |
Output is correct |
103 |
Correct |
173 ms |
20440 KB |
Output is correct |
104 |
Correct |
110 ms |
20752 KB |
Output is correct |
105 |
Correct |
208 ms |
28600 KB |
Output is correct |
106 |
Correct |
105 ms |
20712 KB |
Output is correct |
107 |
Correct |
104 ms |
20840 KB |
Output is correct |
108 |
Correct |
212 ms |
32568 KB |
Output is correct |
109 |
Correct |
111 ms |
20964 KB |
Output is correct |
110 |
Correct |
210 ms |
32808 KB |
Output is correct |
111 |
Correct |
171 ms |
20168 KB |
Output is correct |