Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 46039 | Accepted: 13400 |
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
i-A,i-B,I-C :表示i在第(A,B,C)类。
同一组:表示若1个组员成立,组员全成立
则可分别维护i在A,B,C组的情况。
i,j 同类:i-A=j-A,i-B=j-B,I-C=j-C
i吃j :i-A=j-B,i-B=j-C,I-C=j-A
推断 :若i-A和j-B同类,则必有i吃j,否则无法确定(或因为其他连线不可能实现)。以此类推。
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (3*50000+10) #define MAXM (100000+10) long long mul(long long a,long long b){return (a*b)%F;} long long add(long long a,long long b){return (a+b)%F;} long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;} typedef long long ll; class bingchaji { public: int father[MAXN],n; void mem(int _n) { n=_n; For(i,n) father[i]=i; } int getfather(int x) { if (father[x]==x) return x; return father[x]=getfather(father[x]); } void unite(int x,int y) { father[x]=getfather(father[x]); father[y]=getfather(father[y]); father[father[x]]=father[father[y]]; } bool same(int x,int y) { return getfather(x)==getfather(y); } }S; int main() { // freopen("poj1182_template.in","r",stdin); // freopen(".out","w",stdout); int n,m; scanf("%d%d",&n,&m); S.mem(n*3); int ans=0; For(i,m) { int d,x,y; scanf("%d%d%d",&d,&x,&y); if (x>n||y>n||x<1||y<1) {ans++;continue;} if (d==1) { if (S.same(x,y+n)||S.same(x,y+2*n)) ans++; else { S.unite(x,y);S.unite(x+n,y+n);S.unite(x+2*n,y+2*n); } } else { if (S.same(x,y)||S.same(x,y+2*n)) ans++; else { S.unite(x,y+n);S.unite(x+n,y+2*n);S.unite(x+2*n,y); } } } cout<<ans<<endl; return 0; }