| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 53651 | Accepted: 15730 |
Description
Input
Output
Sample Input
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
题目大意:
中文题。。。怪我咯?合并前要先检查是否矛盾,如对应a与b同一类,如果a-1和b-2在同一集合中,则为假话。
参考代码:
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma commment(linker,"/STACK: 102400000 102400000")
using namespace std;
const double eps=1e-6;
const int INF=0x3f3f3f3f;
const int MAXN=50000+1000;
int n,k,par[MAXN*3],t[100050],a[100050],b[100050];
int find(int x)
{
return x==par[x]?x:par[x]=find(par[x]);
}
bool unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return false;
par[x]=y;
return true;
}
bool same(int x,int y)
{
return find(x)==find(y);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
scanf("%d%d",&n,&k);
for(int i=1; i<=n*3; i++)
par[i]=i;
int ans=0;
for(int i=1; i<=k; i++)
scanf("%d%d%d",&t[i],&a[i],&b[i]);
for(int i=1; i<=k; i++)
{
if(a[i]<1||a[i]>n||b[i]<1||b[i]>n)
{
ans++;
continue;
}
if(t[i]==1)
{
if(same(a[i],b[i]+n)||same(a[i],b[i]+2*n))
{
ans++;
continue;
}
unite(a[i],b[i]);
unite(a[i]+n,b[i]+n);
unite(a[i]+2*n,b[i]+2*n);
}
else if(t[i]==2)
{
if(same(a[i],b[i])||same(a[i],b[i]+2*n))
{
ans++;
continue;
}
unite(a[i],b[i]+n);
unite(a[i]+n,b[i]+2*n);
unite(a[i]+2*n,b[i]);
}
else
ans++;
}
printf("%d\n",ans);
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/noooooorth/article/details/47689541