hdu 1272 判断所给的图是不是生成树 (并查集)

时间:2022-04-03 01:49:40

判断所给的图是不是生成树,如果有环就不是,如果没环但连通分量大于1也不是

find函数 用递归写的话 会无限栈溢出 Orz
要加上那一串 手动扩栈

Sample Input
6 8 5 3 5 2 6 4
5 6 0 0

8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0

3 8 6 8 6 4
5 3 5 6 5 2 0 0

-1 -1

Sample Output
Yes
Yes
No

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <queue>
# define LL long long
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std ; const int MAXN=;
int F[MAXN];
bool vis[MAXN] ;
int save[MAXN] ;
bool flag ;
int find(int x)//找x的祖先结点
{
if(F[x]==x) return x;
return F[x]=find(F[x]);
}
void bing(int u,int v)
{
int t1=find(u);
int t2=find(v);
if(t1!=t2) F[t1]=t2;
else flag = ; //有环
}
int main()
{
//freopen("in.txt","r",stdin) ;
int u , v ;
while(scanf("%d %d" , &u , &v) != EOF)
{
if (u == - && v == -)
break ;
if (u == && v == )
{
printf("Yes\n") ;
continue ;
}
int i ;
for(i=;i<MAXN;i++)
{
F[i]=i;
}
memset(vis , , sizeof(vis)) ;
F[u] = v ;
int l = ;
flag = ;
if (!vis[u])
{
vis[u] = ;
save[l++] = u ;
}
if (!vis[v])
{
vis[v] = ;
save[l++] = v ;
}
while(scanf("%d %d" , &u , &v))
{
if (u == && v == )
break ;
if (flag)
continue ;
if (!vis[u])
{
vis[u] = ;
save[l++] = u ;
}
if (!vis[v])
{
vis[v] = ;
save[l++] = v ;
}
bing(u,v) ;
}
int res = ; //连通分量
for (i = ; i < l ; i++)
if (F[save[i]] == save[i])
res++ ;
if (res >= || flag == )
printf("No\n") ;
else
printf("Yes\n") ;
}
return ;
}