noi2015程序自动分析

并查集

只是要离散化或者hash(这个比较慢)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;
#define MAXN 2000007
struct point{
int x,y,z;
}a[100100];

int t,n,flag,u,v;
int father[MAXN];
int hashnum[MAXN];
int gethash(int x)
{
    int t=x % MAXN;
    while (hashnum[t]!=0 && hashnum[t]!=x && t<=MAXN)
        t=(t+1) % MAXN;
    if (hashnum[t]==x) return t; else {hashnum[t]=x;return t;}
}
int find1(int x)
{
   int i=x;
   while (father[i]!=i)
        i=father[i];
   int t;
   while (father[x]!=x)
   {
       t=father[x];
       father[x]=i;
       x=t;
   }
   return i;
}
int main()
{
    cin>>t;
    while (t--)
    {
        cin>>n;
        for (int i=1;i<=MAXN;i++) father[i]=i;
        memset(hashnum,0,sizeof(hashnum));
        for (int i=1;i<=n;i++)
        {
            cin>>a[i].x>>a[i].y>>a[i].z;
            a[i].x=gethash(a[i].x);
            a[i].y=gethash(a[i].y);
            if (a[i].z==1)
            {
                u=find1(a[i].x);
                v=find1(a[i].y);
                if (u!=v) father[u]=father[v];
            }
        }
        flag=1;
        for (int i=1;i<=n;i++)
        if (a[i].z==0)
        {
            u=find1(a[i].x);
            v=find1(a[i].y);
            if (u==v) {flag=0;break;}
        }
        if (flag==1) cout<<"YES"; else cout<<"NO";
        if (t>0) cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值