/*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];
int 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[cv][1]);
remax(mx0,dp[cv][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 'long long int 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()){
| ^~~
power.cpp:102:1: warning: no return statement in function returning non-void [-Wreturn-type]
102 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
10112 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
10112 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
10112 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |