hdu1289-A Bug's Life

A Bug's Life


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#define maxn 2005

int father[maxn],sex[maxn]; 
using namespace std;

int find(int x) //查找根
{
	if(father[x]==x) return x;
	else
		return father[x] = find(father[x]);
}

void Union(int x, int y)  //合并
{
	int f1 = find(x);
	int f2 = find(y);
	if(f1 != f2)
	father[f1] = f2;
}
int main(int argc, char *argv[])
{
	int T,n,m,x,y,Case = 1;
	scanf("%d",&T);
	while(T--)
	{
		int flag = 0;
		scanf("%d%d",&n,&m);
		memset(sex,0,sizeof(sex));
		for(int i = 1; i <= n; i++)
			father[i] = i;
		for(int i = 1; i <= m; i++)
		{
			scanf("%d%d",&x,&y);
			if(flag)
				continue;
			if(find(x)==find(y))  
			{
				flag = 1;
			}
			else
			{
				if(sex[x] == 0)
					sex[x] = y;
				else
					Union(sex[x],y);
				
				if(sex[y] == 0)
					sex[y] = x;
				else
					Union(sex[y],x);
			}                 
		}
		if(!flag)
		{
			printf("Scenario #%d:\n",Case++);
			puts("No suspicious bugs found!");
			printf("\n");	
		}
		else
		{
			printf("Scenario #%d:\n",Case++);
			puts("Suspicious bugs found!");
			printf("\n");
		}
	}
	return 0;
}






View Code 
 #define _CRT_SECURE_NO_WARNINGS
 #include<iostream>
 #include<cstdio>
 #include<cstring>
 using namespace std;
 const int MAXN=2000+20;
 int parent[MAXN];
 int mark[MAXN];
 int n,m;
 
 void Initiate(){
     memset(mark,0,sizeof(mark));
     for(int i=1;i<=n;i++){
         parent[i]=i;
     }
 }
 
 int Find(int x){
     int s=x;
     while(s!=parent[s]){
         s=parent[s];
     }
     return s;
     /*
     int s;
     for(s=x;parent[s]>=0;s=parent[s]);
     while(s!=x){
         int tmp=parent[x];
         parent[x]=s;
         x=tmp;
     }
     return s;
     */
 }
 
 void Union(int R1,int R2){
     int r1=Find(R1);
     int r2=Find(R2);
     if(r1<r2){
         parent[r2]=r1;
     }else
         parent[r1]=r2;
 }
 
 
 int main(){
     int _case,t=1;
     scanf("%d",&_case);
     while(_case--){
         scanf("%d%d",&n,&m);
         Initiate();
         bool flag=true;
         for(int i=1;i<=m;i++){
             int u,v;
             scanf("%d%d",&u,&v);
             if(!flag)continue;
             if(Find(u)==Find(v)){
                 //找到同性的了。。。
                 flag=false;
                 continue;
             }else {
                 if(mark[u]==0)mark[u]=v;
                 else Union(mark[u],v);//同性的并入一个集合
                 if(mark[v]==0)mark[v]=u;
                 else Union(mark[v],u);
             }
         }
         printf("Scenario #%d:\n",t++);
         if(!flag){
             printf("Suspicious bugs found!\n");
         }else {
             printf("No suspicious bugs found!\n");
         }
         puts("");
     }
     return 0;
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值