标签:合并 continue width 情况 要求 bool std miss col
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 90935 | Accepted: 27329 |
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
1 #include <cstdio> 2 3 using namespace std; 4 5 int n,k; 6 int *T,*X,*Y,*par,*r; 7 8 void init(int n) 9 { 10 for (int i=0; i<n; i++) 11 { 12 par[i]=i; 13 r[i]=0; 14 } 15 } 16 17 int find(int x) 18 { 19 if (par[x]==x) return x; 20 return par[x]=find(par[x]); 21 } 22 23 void unite(int x, int y) 24 { 25 x=find(x); 26 y=find(y); 27 if (x==y) return; 28 if (r[x]<r[y]) par[x]=y; 29 else 30 { 31 par[y]=x; 32 if(r[x]==r[y]) ++r[x]; 33 } 34 } 35 36 bool same(int x, int y) 37 { 38 return find(x)==find(y); 39 } 40 41 int main() 42 { 43 scanf("%d %d",&n,&k); 44 T=new int[k]; 45 X=new int[k]; 46 Y=new int[k]; 47 par=new int[n*3]; 48 r=new int[n*3]; 49 for (int i=0; i<k; i++) scanf("%d %d %d",&T[i],&X[i],&Y[i]); 50 //元素x,x+n,x+n*2分别表示x-A,X-B,x-C 51 init(n*3); 52 int ans=0; 53 for (int i=0; i<k; i++) 54 { 55 int t=T[i],x=X[i]-1,y=Y[i]-1; 56 if (x<0 || x>=n || y<0 || y>=n) 57 { 58 ++ans; 59 continue; 60 } 61 if (t==1) 62 { 63 if (same(x,y+n) || same(x,y+n*2)) ans++; 64 else for (int j=0; j<3; j++) unite(x+n*j,y+n*j); 65 } 66 else 67 { 68 if (same(x,y) || same(x,y+n*2)) ans++; 69 else for (int j=0; j<3; j++) unite(x+n*j,y+n*((j+1)%3)); 70 } 71 } 72 printf("%d\n",ans); 73 }
标签:合并 continue width 情况 要求 bool std miss col
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9448148.html