有根数问题(daan)

zhangyihao 2022-02-08 20:19:51 2022-02-09 14:16:40 0
#include<bits/stdc++.h>
using namespace std;
const int maxn=50005;
int N,np=0,first[maxn]={0},x,y,z,root=1;
int dep[maxn],fa[maxn],chd[maxn],sz[maxn];
struct edge
{
	int to,next;
}E[maxn];
void add(int u,int v)
{
	E[++np]=(edge){v,first[u]};
	first[u]=np;
}
void DFS(int i,int f,int d)
{
	fa[i]=f;
	dep[i]=d;
	chd[i]=0;
	sz[i]=1;
	for(int p=first[i];p;p=E[p].next)
	{
		chd[i]++;
		DFS(E[p].to,i,d+1);
		sz[i]+=sz[E[p].to]; 
	}
}
void solve1()
{
	int ans1=0,ans2=0;
	for(int i=1;i<=N;i++)
	{
		ans1=max(ans1,dep[i]);
		ans2=max(ans2,chd[i]);
	}
	printf("%d %d\n",ans1,ans2);
}
void solve2()
{
	printf("%d ",dep[x]);
	if(chd[x]==0)printf("Yes\n");
	 else printf("No\n");
}
void solve3()
{
	printf("%d %d\n",chd[y],sz[y]-1);
}
void solve4(int u)
{
	if(fa[u]!=0)
		solve4(fa[u]);
	printf("%d ",u);
}
int main()
{
	scanf("%d",&N);
	for(int i=1;i<N;i++)
	{
	    int u,v;
		scanf("%d%d",&u,&v);
		
		add(u,v);
	}
	scanf("%d%d%d",&x,&y,&z);
	DFS(root,0,1);
	solve1();
	solve2(); 
	solve3(); 
	solve4(fa[z]); 
	return 0;
}~


{{ vote && vote.total.up }}

共 2 条回复

zhangyihao

好的呢

root 站长

不要 ####,直接 ```c就行了