Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
新思维#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <sstream> #include <iomanip> using namespace std; const int INF=0x4fffffff; const int EXP=1e-6; const int MS=50005; const int MS2=100005; int op[MS2],X[MS2],Y[MS2]; int fa[3*MS]; int N,K; void input() { scanf("%d%d",&N,&K); for(int i=0;i<K;i++) scanf("%d%d%d",&op[i],&X[i],&Y[i]); for(int i=0;i<3*N;i++) fa[i]=i; } int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } void merge(int x,int y) { int f1=find(x); int f2=find(y); if(f1!=f2) fa[f1]=f2; } bool same(int x,int y) { return find(x)==find(y); } void solve() { int ans=0; for(int i=0;i<K;i++) { int t=op[i]; int x=X[i]-1; int y=Y[i]-1; if(x<0||x>=N||y<0||y>=N) { ans++; continue; } if(t==1) { if(same(x,y+N)||same(x,y+2*N)) ans++; else { merge(x,y); merge(x+N,y+N); merge(x+2*N,y+2*N); } } else { if(same(x,y)||same(x,y+2*N)) ans++; else { merge(x,y+N); merge(x+N,y+2*N); merge(x+2*N,y); } } } printf("%d\n",ans); } int main() { input(); solve(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator