poj 1308 Is It A Tree?

  • #include <iostream>
    using namespace std;
    
    struct node
    {
    	int x,y;
    }t;
    int tt,case_t;
    int parent[105];
    bool flag[105];
    void makeset()
    {
    	int xx;
    	for(xx=1;xx<100;xx++)
    	{
    		parent[xx]=xx;
    		flag[xx]=0;
    	}
    }
    int findset(int xx)
    {
    	if(xx!=parent[xx])
    		parent[xx]=findset(parent[xx]);
    	return parent[xx];
    }
    void unionset(int xx,int yy)
    {
    	xx=findset(xx);
    	yy=findset(yy);
    	if(xx==yy)
    		return ;
    	parent[yy]=xx;
    }
    int main()
    {
    	int fir;
    	while(scanf("%d%d",&t.x,&t.y)!=EOF)
    	{
    		if(t.x==-1 && t.y==-1)
    			break;
    		if(t.x==0 && t.y==0)
    		{
    			printf("Case %d is a tree./n", ++case_t);
    			continue;
    		}
    		makeset();
    		flag[t.x]=flag[t.y]=1;
    		fir=t.x;
    		bool tree=1;
    		if(t.x==t.y)
    			tree=0;
    		else
    			unionset(t.x,t.y);
    		while(scanf("%d%d",&t.x,&t.y) &&t.x!=0)
    		{
    			flag[t.x]=flag[t.y]=1;
    			if(findset(t.x)==findset(t.y))
    				tree=0;
    			unionset(t.x,t.y);
    		}
    		for(int i=1;i<100;i++)
    			if(flag[i] && findset(i)!=findset(fir))
    				tree=0;
    		if(tree)
    			printf("Case %d is a tree./n", ++case_t);
    		else
    			printf("Case %d is not a tree./n", ++case_t);
    	}
    	return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值