codeforces 686D D. Kay and Snowflake(dfs)
题目链接:
After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.
Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of n nodes. The root of tree has index 1. Kay is very interested in the structure of this tree.
After doing some research he formed q queries he is interested in. The i-th query asks to find a centroid of the subtree of the node vi. Your goal is to answer all queries.
Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node v is formed by nodes u, such that node v is present on the path from u to root.
Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).
The first line of the input contains two integers n and q (2 ≤ n ≤ 300 000, 1 ≤ q ≤ 300 000) — the size of the initial tree and the number of queries respectively.
The second line contains n - 1 integer p2, p3, ..., pn (1 ≤ pi ≤ n) — the indices of the parents of the nodes from 2 to n. Node 1 is a root of the tree. It's guaranteed that pi define a correct tree.
Each of the following q lines contain a single integer vi (1 ≤ vi ≤ n) — the index of the node, that define the subtree, for which we want to find a centroid.
For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.
7 4
1 1 3 3 5 3
1
2
3
5
3
2
3
6
题意:
给一棵树,有q个询问,询问以x为根的子树的质心是哪个节点;
思路:
son[x]表示根为x的子树的节点个数;dfs的过程中可以计算son;
每次可以发现一个子树的质心要么在根上(x所有子节点的son都小于son[x]/2),要么在节点数最多的子树上;
在子树上时可以按最大的子节点的质心到x找到质心;最后就是O(1)的询问;
给一个关于树的重心的博客:转送门
AC代码:
//#include <bits/stdc++.h> #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e18; const int N=3e5+10; const int maxn=1005; int n,q,son[N],ans[N],root[N]; vector<int>ve[N]; void dfs(int x,int fa) { int len=ve[x].size(),mmax=0,pos=x; son[x]=1; ans[x]=x; for(int i=0;i<len;i++) { int y=ve[x][i]; if(y==fa)continue; dfs(y,x); son[x]+=son[y]; if(son[y]>mmax) { mmax=son[y]; pos=y; } } ans[x]=ans[pos]; while(ans[x]!=x&&son[x]>2*son[ans[x]]) ans[x]=root[ans[x]]; } int main() { read(n);read(q); int x; for(int i=2;i<=n;i++) { read(x); root[i]=x; ve[x].push_back(i); ve[i].push_back(x); } dfs(1,-1); while(q--) { read(x); printf("%d\n",ans[x]); } return 0; }