Three countries, Red, Yellow, and Blue are in war. The map of battlefield is a tree, which means that
there are N nodes and (N −1) edges that connect all the nodes. Each country has a base station located
in one node. All three countries will not place their station in the same node. And each country will
start from its base station to occupy other nodes. For each node, country A will occupy it iff other two
country’s base stations have larger distances to that node compared to country A. Note that each edge
is of the same length.
Given three country’s base station, you task is to calculate the number of nodes each country
occupies (the base station is counted).
Input
The input starts with a single integer T (1 ≤ T ≤ 10), the number of test cases.
Each test cases starts with a single integer N (3 ≤ N ≤ 105
), which means there are N nodes in
the tree.
Then N − 1 lines follow, each containing two integers u and v (1 ≤ u, v ≤ N, u ̸= v), which means
that there is an edge between node u and node v.
Then a single integer M (1 ≤ M ≤ 105
) follows, indicating the number of queries.
Each the next M lines contains a query of three integers a, b, c (1 ≤ a, b, c ≤ N, a, b, c are distinct),
which indicates the base stations of the three countries respectively.
Output
For each query, you should output three integers in a single line, separated by white spaces, indicating
the number of nodes that each country occupies. Note that the order is the same as the country’s base
station input.
Sample Input
1
9
1 2
1 3
1 4
2 5
2 6
2 7
6 8
6 9
2
1 2 8
2 1 4
Sample Output
3 3 1
there are N nodes and (N −1) edges that connect all the nodes. Each country has a base station located
in one node. All three countries will not place their station in the same node. And each country will
start from its base station to occupy other nodes. For each node, country A will occupy it iff other two
country’s base stations have larger distances to that node compared to country A. Note that each edge
is of the same length.
Given three country’s base station, you task is to calculate the number of nodes each country
occupies (the base station is counted).
Input
The input starts with a single integer T (1 ≤ T ≤ 10), the number of test cases.
Each test cases starts with a single integer N (3 ≤ N ≤ 105
), which means there are N nodes in
the tree.
Then N − 1 lines follow, each containing two integers u and v (1 ≤ u, v ≤ N, u ̸= v), which means
that there is an edge between node u and node v.
Then a single integer M (1 ≤ M ≤ 105
) follows, indicating the number of queries.
Each the next M lines contains a query of three integers a, b, c (1 ≤ a, b, c ≤ N, a, b, c are distinct),
which indicates the base stations of the three countries respectively.
Output
For each query, you should output three integers in a single line, separated by white spaces, indicating
the number of nodes that each country occupies. Note that the order is the same as the country’s base
station input.
Sample Input
1
9
1 2
1 3
1 4
2 5
2 6
2 7
6 8
6 9
2
1 2 8
2 1 4
Sample Output
3 3 1
6 2 1
第一次写解题报告,希望大家能够喜欢。
#include
#include
#include
#include
#include
using namespace std; #define maxn 100100 int n,d; int fa[maxn][20]; int head[maxn]; int next[maxn<<1]; int edge[maxn<<1]; int pre[maxn]; int nn; int l[maxn]; int r[maxn]; int stop; int ll1[2][maxn]; int rr1[2][maxn]; int vis[2][maxn]; void add(int u,int v) { edge[d]=v; next[d]=head[u]; head[u]=d++; } void dfs(int u,int cent) { stop++; l[u]=stop; pre[u]=cent; int i; for(i=head[u];i!=-1;i=next[i])if(!pre[edge[i]]){ fa[edge[i]][0]=u; dfs(edge[i],cent+1); } r[u]=stop; } void init() { int i,j; for(j=1;(1<
<=n;j++) for(i=1;i<=n;i++)fa[i][j]=fa[fa[i][j-1]][j-1]; } void Swap(int &u,int &v) { int temp=u; u=v; v=temp; } int lca(int u,int v) { if(pre[u]
=0;i--) if(fa[u][i]!=fa[v][i]){ u=fa[u][i]; v=fa[v][i]; } return fa[u][0]; } void solve(int u,int v1,int v,int v2) { int i; int f=lca(u,v); if(pre[u]
>=1; int zhong=u; for(i=0;(1<
<=ll;i++)if((1<