/***Farhan132***/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<ll> vll;
typedef pair<ll , ll> ii;
typedef vector< ii > vii;
typedef pair < pair < ll , ll > , pair < ll , ll > > cm;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define f0(b) for(int i=0;i<(b);i++)
#define f00(b) for(int j=0;j<(b);j++)
#define f1(b) for(int i=1;i<=(b);i++)
#define f11(b) for(int j=1;j<=(b);j++)
#define f2(a,b) for(int i=(a);i<=(b);i++)
#define f22(a,b) for(int j=(a);j<=(b);j++)
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld", &a , &b)
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n", a , b)
#define PI 3.14159265359
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clzll(n)
#define back_zero(n) __builtin_ctz(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
const ll mod = (ll) 998244353;
const ll maxn = (ll)2e5 + 5;
const int nnn = 1048590;
const int inf = numeric_limits<int>::max()-1;
//const ll INF = numeric_limits<ll>::max()-1;
//const ll INF = (ll)1e18;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};
bool USACO = 0;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less< ll >, rb_tree_tag,tree_order_statistics_node_update>
const ll N = 5e5 + 5;
string s;
vector < ll > v[N];
ll res = 1;
ll dp(ll node, ll p){
ll ans = 0, mx = 0;
for(auto u : v[node]){
if(u - p){
ll k = dp(u, node);
ans += k;
mx = max(mx, k);
}
}
ll k = (s[node] == '1');
res = max({res , mx + k , ans});
ans = max(k , ans - k);
return ans;
}
void solve(){
ll n; cin >> n;
for(ll i = 1; i < n; i++){
ll x , y; cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
cin >> s;
s = '1' + s;
for(ll i = 1; i <= n; i++){
if(v[i].size() <= 1){
dp(i, 0);
break;
}
}
cout << res << '\n';
return;
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#else
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//pre(N);
ll T = 1;
//cin >> T;
ll CT = 0;
while(T--){
// cout << "Case #" << ++CT <<": " ;
solve();
}
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:125:8: warning: unused variable 'CT' [-Wunused-variable]
125 | ll CT = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12060 KB |
Output is correct |
5 |
Correct |
6 ms |
11968 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
12028 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
11932 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Incorrect |
6 ms |
11980 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12060 KB |
Output is correct |
5 |
Correct |
6 ms |
11968 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
12028 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
11932 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Incorrect |
6 ms |
11980 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12060 KB |
Output is correct |
5 |
Correct |
6 ms |
11968 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
12028 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
11932 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Incorrect |
6 ms |
11980 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |