A - Tree

http://acm.hdu.edu.cn/showproblem.php?pid=6867

You are given a tree consisting of  [Math Processing Error]n vertices numbered [Math Processing Error]1 to [Math Processing Error]n rooted at node [Math Processing Error]1. The parent of the [Math Processing Error]i-th vertices is [Math Processing Error]pi. You can move from a vertex to any of its children. What's more, you can add one directed edge between any two different vertices, and you can move through this edge too. You need to maximize the number of pairs [Math Processing Error](x,y) such that [Math Processing Error]x can move to [Math Processing Error]y through the edges after adding the edge. Note that [Math Processing Error]x can also move to [Math Processing Error]x.

InputThe first line contains one integer [Math Processing Error][Math Processing Error](1≤T≤100000) — the number of test cases.

The first line of each test case contains only one integer [Math Processing Error]n(1≤n≤5×105) — the number of vertices in the tree.

The second line of each test case contains [Math Processing Error]n−1 integers [Math Processing Error]p2,p3,…,pn(1≤pi<i) — the parent of each non-root node.

The sum of [Math Processing Error]n over all test cases does not exceed [Math Processing Error]106.
OutputPrint [Math Processing Error]T integers — for each test case output the maximum number of pairs [Math Processing Error](x,y) that vertices [Math Processing Error]x can move to [Math Processing Error]y after adding one edge.Sample Input

2
5
1 1 2 2
6
1 2 3 1 3

Sample Output

17
26

Sponsor

 

题意:

给定一棵树,边为父结点指向子结点,添加一条有向边,

使得从某一点A出发能到达一点B的产生的组合<A,B>尽量多

 

DFS连线求最优

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;

int fa[N] = {0} , deep[N] = {0};
int Q[N] = {0};
int ans = -1;
int min_ = 0 , n = 0;
vector<int> e[N];

void TreeSplitDfs1(int x)
{
	Q[x] = 1;
	deep[x] = deep[fa[x]] + 1;
	for(int i = 0;i<N;i++)
	{
		int next = e[x][i];
		Q[x] += Q[next];
		TreeSplitDfs1(next);
	}
	min_ += Q[x];
}
void TreeSplitDfs2(int x,int dep)
{
	ans = max(ans , dep);
	for(int i = 0;i<N;i++)
	{
		int next = e[x][i];
		TreeSplitDfs2(next , dep - Q[next] + n);
	}
}
int main()
{
	int i = 0;
	int t = 0;
	cin >> t;
	while(t--)
	{
		cin >> n;
		min_ = 0;
		ans = 0;
		i = 0;
		while(i < n)
		{
		 	i += 1;
            e[i].clear();
        }
		i = 0;
		while(i < n)
		{
			i += 1;
			cin >> fa[i+1];
			e[fa[i+1]].push_back(i+1);
		}
		TreeSplitDfs1(1);
		TreeSplitDfs2(1 , min_);
		cout << ans << endl;
	}
	return 0;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值