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. linux窗口透明(全局透明,进程id查找wid,进程名称查找wid)

    linux窗口透明 使用到了qt xcb-ewmh x11-xcb 效果图 如何实现 控制全部窗口透明 1.遍历WID树,的到全部窗口得wid 2.区别窗口属性,桌面和dock窗口不设置透明,其他窗口 ...

  2. 从零开发Vim-like编辑器(01)起步

    前言 Vim和Neovim因其独特的模态编辑和高度可定制化,被列为程序员常用的文本编辑器选项之一,与Sublime Text.VS Code.Emacs等编辑器共同丰富了开发者工具生态.就目前而言,网 ...

  3. 什么是FIPS 140-3?

    什么是FIPS 140-3? FIPS 140-3是一项由NIST(National Institute of Standards and Technology)发布的针对加密模块安全要求的标准,英文 ...

  4. odoo知识图谱

    最近项目交付后,准备将系统整个知识点整理一下,下面是目录,后面针对目录编写文档--todo

  5. odoo14里面给所有模型添加方法

    给所有的model都添加一个方法, 即所有的model都能调用[类似于create.write.unlink.read]. 方式一: from odoo import api, fields, mod ...

  6. 用curl测网速统计访问耗时

      在<从基础到高级,带你结合案例深入学习curl命令>中,介绍了curl的使用方法,这里介绍一个用于统计响应耗时的最佳实践,助力老铁们合理设置网络超时时间.   下面介绍一个用于统计访问 ...

  7. IDEA jrebel热部署插件破解-最新版

    前言 JRebel插件2022.4.2及之后版本在线地址激活方式已不可用,所以采用本地地址 + 生成的GUID方式 激活 (本文章写的时候,用的JRebel最新版本2023.2.1) 如果需要在线激活 ...

  8. 洛谷 P6006 [USACO20JAN]Farmer John Solves 3SUM G

    洛谷 P6006 [USACO20JAN]Farmer John Solves 3SUM G Problem 什么是3-SUM? 给你一个序列\(a\),求有多少组\((i,j,k)(1\le i&l ...

  9. kubernetes实用插件管理

    插件链接:https://kubernetes.io/zh-cn/docs/tasks/extend-kubectl/kubectl-plugins/

  10. 基于Spring AI服务,开发MCP服务

    大家好,好久没有写博客了,最近突然想写一些新的东西,如何开发mcp服务,本地如何调试. 一.项目概述 Spring AI MCP Demo 是一个基于 Spring 生态的多模块应用工程,主要围绕 A ...