/*
DavitMarg
In pr honky-tonk,
Down in Mexico
*/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <random>
#include <chrono>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 500005;
int n;
string s;
vector<int> g[N];
int dp[N], dp2[N];
int ans = 0;
void dfs(int v, int p)
{
dp[v] = s[v] - '0';
int sum = 0;
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to == p)
continue;
dfs(to, v);
sum += dp[to];
}
dp2[v] = dp[v] = max(dp[v], sum - (s[v] - '0'));
}
void dfs2(int v, int p)
{
dp2[v] = s[v] - '0';
int sum = 0;
int mx = 0;
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to == p)
continue;
sum += dp[to];
mx = max(dp[to], mx);
}
if(p > -1)
{
sum += dp2[p];
mx = max(dp2[p], mx);
}
dp2[v] = max(dp2[v], sum - (s[v] - '0'));
ans = max(ans, max(dp2[v], (s[v] - '0') + mx));
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to == p)
continue;
dfs(to, v);
}
dp2[v] = dp[v];
}
LL myRand()
{
return ((LL)rand() << 16) + rand();
}
void solve()
{
cin >> n;
for(int i = 1; i <= n - 1; i++)
{
int a, b;
cin >> a >> b;
g[a].PB(b);
g[b].PB(a);
}
cin>>s;
s = '#' + s;
vector<int> p;
for(int i = 1; i <= n; i++)
if(s[i] == '1')
p.PB(i);
int st = p[0];
dfs(st, -1);
dfs2(st, -1);
cout << ans << endl;
}
int main()
{
fastIO;
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
/*
cd /mnt/d/Davit/Projects/VSCode; g++ -std=c++17 -O2 -Wall -Wextra -Ddeath -Wshadow source.cpp -o orz; ./orz; rm orz
*/
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:45:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
power.cpp: In function 'void dfs2(int, int)':
power.cpp:61:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
power.cpp:79:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |