poj1308 hdu1325! 并查集*3

11007421asia25622191308Accepted1028K0MSC++1441B2012-11-11 20:08:11

hdu WA;………… 受不了这题,hdu 案例都过不了的代码能AC——!

View Code
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 111111
int deg[MAXN],tot,father[MAXN],totnum,tote;
bool flag=true;
int find(int x)
{
    int temp=x;
    while(temp!=father[temp])
    temp=father[temp];
    int i=x,j;
    while(i!=temp)
    {
        j=father[i];
        father[i]=temp;
        i=j;
    }
    return temp;
}
bool judge()
{
    if(!flag)return false;
    int odd=0,rot;
    if(tote==0&&totnum==0)return true;
    if(tote!=totnum-1)return false;
    rot=find(tot);
    for(int i=1;i<=tot;i++)
        if(deg[i]!=0&&rot!=find(i))return false;
    return true;
}
int main()
{
    int x,y,i;
    int cases=1;
    tot=0;memset(deg,0,sizeof(deg));
    for(i=0;i<=MAXN;i++)father[i]=i;
    totnum=tote=0;
    while(scanf("%d%d",&x,&y),x>=0)
    {
        if(x==0&&y==0)
        {if(judge())printf("Case %d is a tree.\n",cases);
            else printf("Case %d is not a tree.\n",cases);
            cases++;
        tot=0;memset(deg,0,sizeof(deg));
        flag=true;
        for(i=0;i<=MAXN;i++)father[i]=i;
        totnum=tote=0;continue;}
        tot=max(max(tot,x),y);
        if(deg[x]==0)totnum++;
        if(deg[y]==0)totnum++;
        tote++;
        deg[x]++;deg[y]++;
        if(find(x)!=find(y))
        father[find(x)]=find(y);
        else flag=false;
    }
    return 0;
}

坑爹的hdu 巨坑也不填……

转载于:https://www.cnblogs.com/TO-Asia/archive/2012/11/11/2765382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值