D. MEX Tree

题目链接
outputstandard output
You are given a tree with n nodes, numerated from 0 to n−1. For each k between 0 and n, inclusive, you have to count the number of unordered pairs (u,v), u≠v, such that the MEX of all the node labels in the shortest path from u to v (including end points) is k.

The MEX of a sequence of integers is the smallest non-negative integer that does not belong to the sequence.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains a single integer n (2≤n≤2⋅105).

The next n−1 lines of each test case describe the tree that has to be constructed. These lines contain two integers u and v (0≤u,v≤n−1) denoting an edge between u and v (u≠v).

It is guaranteed that the given edges form a tree.

It is also guaranteed that the sum of n for all test cases does not exceed 2⋅105.

Output
For each test case, print n+1 integers: the number of paths in the tree, such that the MEX of all the node labels in that path is k for each k from 0 to n.

Example
inputCopy
2
4
0 1
0 2
2 3
2
1 0
outputCopy
1 2 1 1 1
0 0 1
Note
In example case 1,
在这里插入图片描述

For k=0, there is 1 path that is from 2 to 3 as MEX([2,3])=0.
For k=1, there are 2 paths that is from 0 to 2 as MEX([0,2])=1 and 0 to 3 as MEX([0,2,3])=1.
For k=2, there is 1 path that is from 0 to 1 as MEX([0,1])=2.
For k=3, there is 1 path that is from 1 to 2 as MEX([1,0,2])=3
For k=4, there is 1 path that is from 1 to 3 as MEX([1,0,2,3])=4.
In example case 2,
在这里插入图片描述

For k=0, there are no such paths.
For k=1, there are no such paths.
For k=2, there is 1 path that is from 0 to 1 as MEX([0,1])=2.

分析:

有一颗有n个点的树,顶点编号为 0 — n-1 。MEX是指除了u-v的路径上的点之外的编号最小值。题目求当MEX=0,1…n时的所有u-v的路径 ,即不包含0的所有路径,不包含1但包含0的所有路径,不包含2但包含0和1的所有路径…不包含n但包含0、1、2…n-1的所有路径。
令 ans[0] = n*(n-1)/2,即一棵树所有的路径数。
ans[1]表示包含0的所有路径数,则(ans[0]-ans[1])是MEX=0时的答案,即不包含0的所有路径。
abs[2]表示包含0和1的所有路径,则(ans[1]-ans[2])是MEX=1时的答案,即不包含1但包含0的所有路径。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+10;
const ll mod = 998244353;
int n;
int h[N],e[2*N],ne[2*N],idx,f[N],c[N];
ll size[N],ans[N];
void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u) 
{
	size[u]=1;
	for(int i=h[u];i!=-1;i=ne[i])
	{
		int v=e[i];
		if(f[u]==v) continue;
		f[v]=u;
		dfs(v);
		size[u]+=size[v];
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(h,-1,sizeof(h));
		idx=0;
		scanf("%d",&n);
		int a,b;
		for(int i=1;i<n;i++)
		{
			scanf("%d%d",&a,&b);
			add(a,b),add(b,a);//连边 
		}
		dfs(0);//以 0为根,求得子树的顶点数 size 
		ans[0]=1LL*n*(n-1)/2;//所有路径 
		ll sum=1;
		for(int i=h[0];i!=-1;i=ne[i])
		{
			int v=e[i];
			ans[1]+=sum*size[v];//ans[1]是包含0的所有路径 
			sum+=size[v];
		}
		a=0,b=0,c[0]=1;//当前a-b表示一条包含0的路径 ,c[i]=1表示i实在a-b这条路径上 
		int p,x;
		for(int i=1;i<n;i++)
		{
			if(!c[i])//如果当前i点不在a-b这条路径 
			{
				x=p=i;
				while(!c[x]) c[x]=1,p=x,x=f[x];//从i点往上找,找到与a-b这条路径连接的点x 
				if(x==a)
				{
					size[a]-=size[p];
					a=i;
				}
				else if(x==b)
				{
					size[b]-=size[p];
					b=i;
				}
				else break;//表明当前已经无法找到一条包含0、1、2...i的路径 
			}
			ans[i+1]=size[a]*size[b];//包含0、1、2...i的所有路径 
		}
		for(int i=0;i<=n;i++)
		{
			printf("%lld ",ans[i]-ans[i+1]);
		}
		printf("\n");
		for(int i=0;i<=n;i++)
		{
			size[i]=ans[i]=f[i]=c[i]=0;
		}
	} 
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值