Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 58561 | Accepted: 17111 |
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
Source
#include<stdio.h> #define N 150005//开成3倍大 int par[N],ran[N]; void init(int n) { for(int i=0;i<N;i++) { par[i]=i; ran[i]=0; } } int find(int x) { if(par[x]==x)return x; return par[x]=find(par[x]); } void unite(int x,int y) { x=find(x); y=find(y); if(x==y)return; if(ran[x]<ran[y]) par[x]=y; else par[y]=x; if(ran[x]==ran[y])ran[x]++; } bool same(int x,int y) { return find(x)==find(y); } int main() { int n,k,cnt=0; scanf("%d%d",&n,&k); init(n);//别忘初始化还有初始化的时候有3n个 while(k--) { int d,x,y; scanf("%d%d%d",&d,&x,&y); if(x<1||x>n||y<1||y>n)cnt++; else if(d==1) { if(same(x,y+n)||same(x,y+n*2))cnt++; else { unite(x,y); unite(x+n,y+n); unite(x+n*2,y+n*2); } } else { int x1=find(x); int y1=find(y+n*2); if(same(x,y)||same(x,y+n*2))cnt++; else { unite(x,y+n); unite(x+n,y+2*n); unite(x+2*n,y); } } } printf("%d\n",cnt); }