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]T [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;
}

  

A - Tree的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  10. Tree树节点选中及取消和指定节点的隐藏

    指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...

随机推荐

  1. centos8安装部署RADIUS+MySQLPGSQL高可用架构实现

    以下是针对中大型网络的 RADIUS+MySQL/PGSQL高可用方案 的完整实现,包含数据库集成.主备集群部署和Keepalived配置: 一.MySQL/PGSQL数据库集成(以MySQL为例) ...

  2. IntelliJ IDEA 源文件提示 cannot resolve method 或者 Cannot find declaration to go to

    问题描述:IntelliJ IDEA 在源文件中提示 Cannot resolve method,但是项目可以正常编译运行,提示异常的类明明存在且没有任何异常.尝试使用ctrl+鼠标左键进入该类时,提 ...

  3. NSDictionary 内存布局

    NSDictionary是iOS开发中经常用到的数据结构. 熟悉NSDictionary的内部实现,有助于我们更好的使用它们. 同时,在遇到相关崩溃,也能帮助我们更好的分析问题. 1 类簇 非可变字典 ...

  4. Hexo博客Next主题更换cdn加速访问

    有时候访问我的博客时,总是会出现cdn.jsdelivr.net无法访问或者访问速度过慢的情况.我的博客园使用的是BNDong/Cnblogs-Theme-SimpleMemory主题,也遇到的这样的 ...

  5. git创建远程分支

    如果有个叫 serverfix 的分支需要和他人一起开发,有两种方式可以创建远程分支origin/serverfix 1.git push origin serverfix 2.git push or ...

  6. Hexo-butterfly 接入腾讯混元大模型自动生成文章摘要(保姆教程)

    1.注册腾讯云账号 首先必须要有一个腾讯云的账号,没有的话在官网注册一个,注册完成之后,进行实名操作. 接下来,在产品中心搜索腾讯混元大模型,点击产品控制台进入,在模型广场选择自己想要的大模型 (复制 ...

  7. 基于 C\# 和 .NET 的 Spread.NET 数据处理实战

    引言 在当今数字化的时代,数据处理和分析在各个领域都扮演着至关重要的角色.对于开发者而言,选择一款功能强大且易于集成的表格控件来处理数据是提高开发效率和质量的关键.Spread.NET 作为 Grap ...

  8. 从数组和List中随机抽取若干不重复的元素

    一.从数组中随机抽取若干不重复元素 /** * @function:从数组中随机抽取若干不重复元素 * * @param paramArray:被抽取数组 * @param count:抽取元素的个数 ...

  9. 2025国内五大MES系统排名探秘:从核心架构到选型指南,解锁智造升级最优解

    在智能制造浪潮席卷全球的今天,MES系统(制造执行系统)作为连接企业管理层与车间生产层的"神经中枢",其重要性日益凸显.它能有效打通信息孤岛,实现生产全流程透明化.可控化与智能化, ...

  10. 【闲话 No.5】 FFT 与 NTT 基础

    门的另一端 可爱得很有力量的歌,丘丘人的部分感觉好听炸了. 偶然推开的那一扇门 从何时起变得不再陌生 对面有人 可爱地等 在遇见你的这个世界 时针似乎转得要比平时快些 天空 更清澈了一些 相遇的理由 ...