Problem E. Split The Tree
Time Limit: 7000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 501 Accepted Submission(s): 146
Problem Description
You are given a tree with n vertices, numbered from 1 to n. ith vertex has a value wi
We define the weight of a tree as the number of different vertex value in the tree.
If we delete one edge in the tree, the tree will split into two trees. The score is the sum of these two trees’ weights.
We want the know the maximal score we can get if we delete the edge optimally
We define the weight of a tree as the number of different vertex value in the tree.
If we delete one edge in the tree, the tree will split into two trees. The score is the sum of these two trees’ weights.
We want the know the maximal score we can get if we delete the edge optimally
Input
Input is given from Standard Input in the following format:
n
p2 p3 . . . pn
w1 w2 . . . wn
Constraints
2 ≤ n ≤ 100000
1 ≤ pi < i
1 ≤ wi ≤ 100000(1 ≤ i ≤ n), and they are integers
pi means there is a edge between pi and i
n
p2 p3 . . . pn
w1 w2 . . . wn
Constraints
2 ≤ n ≤ 100000
1 ≤ pi < i
1 ≤ wi ≤ 100000(1 ≤ i ≤ n), and they are integers
pi means there is a edge between pi and i
Output
Print one number denotes the maximal score
Sample Input
3 1 1 1 2 2 3 1 1 1 1 1
Sample Output
3 2
Source
Recommend
Solution
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 10;
int n, ecnt, head[maxn];
int vcnt, dfn[maxn], id[maxn], siz[maxn];
int w[maxn], tree[maxn], last[maxn], ans[maxn];
struct edge { int to, nxt; } e[maxn];
vector<p> vec[maxn];
template<typename T = int>
inline const T read()
{
T x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
return x * f;
}
template<typename T>
inline void write(T x)
{
if (x < 0) { putchar('-'); x = -x; }
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
void addEdge(int u, int v)
{
e[ecnt].to = v;
e[ecnt].nxt = head[u];
head[u] = ecnt++;
}
void dfs(int u, int f)
{
dfn[u] = ++vcnt;
id[vcnt] = id[vcnt + n] = u;
siz[u] = 1;
for (int i = head[u]; ~i; i = e[i].nxt)
{
int v = e[i].to;
if (v == f) continue;
dfs(v, u);
siz[u] += siz[v];
}
}
int lowbit(int x) { return x & -x; }
void update(int p, int v) { for (int i = p; i < maxn; i += lowbit(i)) tree[i] += v; }
int getSum(int p)
{
int res = 0;
for (int i = p; i > 0; i -= lowbit(i)) res += tree[i];
return res;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
while (~scanf("%d", &n))
{
memset(last, 0, sizeof(last));
memset(tree, 0, sizeof(tree));
memset(ans, 0, sizeof(ans));
memset(head, -1, sizeof(head));
vcnt = ecnt = 0;
for (int i = 1; i <= n * 2; i++) vec[i].clear();
for (int u = 2; u <= n; u++)
{
int v = read();
addEdge(u, v);
addEdge(v, u);
}
dfs(1, 0);
for (int i = 1; i <= n; i++) w[i] = read();
for (int i = 1; i <= n; i++)
{
vec[dfn[i] + siz[i] - 1].push_back(p(dfn[i], i));
if (i > 1) vec[dfn[i] + n - 1].push_back(p(dfn[i] + siz[i], i));
}
for (int i = 1; i <= n * 2; i++)
{
if (last[w[id[i]]]) update(last[w[id[i]]], -1);
update(i, 1);
last[w[id[i]]] = i;
for (auto dfn : vec[i]) ans[dfn.second] += getSum(i) - getSum(dfn.first - 1);
}
int res = 0;
for (int i = 1; i <= n; i++) res = max(res, ans[i]);
write(res); putchar(10);
}
return 0;
}