1. 程式人生 > >並查集 食物鏈 模板

並查集 食物鏈 模板

pre pac bitset for time sin .org union color

https://www.luogu.org/problemnew/show/P2024

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <ctime>
 5 #include <cstring>
 6 #include <string>
 7 #include <map>
 8 #include <set>
 9 #include <list>
10 #include <queue>
11
#include <stack> 12 #include <vector> 13 #include <bitset> 14 #include <algorithm> 15 #include <iostream> 16 using namespace std; 17 #define ll long long 18 const int maxn=5e4+10; 19 20 int fa[maxn*3]; 21 22 int getf(int d) 23 { 24 if (fa[d]==d) 25 return d;
26 fa[d]=getf(fa[d]); 27 return fa[d]; 28 } 29 30 void _union(int x,int y) 31 { 32 int xx=getf(x); 33 int yy=getf(y); 34 fa[xx]=yy; 35 } 36 37 int main() 38 { 39 int n,t,mode,x,y,x1,y1,y2,lie=0,i; 40 scanf("%d%d",&n,&t); 41 for (i=1;i<=3*n;i++) 42 fa[i]=i;
43 ///x eat y k -> (k+n) mod (3*n) 44 ///judge:use any(both ok) ; handle:all 45 while (t--) 46 { 47 scanf("%d%d%d",&mode,&x,&y); 48 if (x>n || y>n) 49 { 50 lie++; 51 continue; 52 } 53 if (mode==1) 54 { 55 ///x,y same 56 x1=getf(x); 57 y1=getf(y+n); 58 y2=getf(y+n+n); 59 if (x1==y1 || x1==y2) 60 lie++; 61 else 62 { 63 _union(x,y); 64 _union(x+n,y+n); 65 _union(x+n+n,y+n+n); 66 } 67 } 68 else 69 { 70 ///x eat y 71 x1=getf(x); 72 y1=getf(y); 73 y2=getf(y+n+n); 74 if (x1==y1 || x1==y2) 75 lie++; 76 else 77 { 78 _union(x,y+n); 79 _union(x+n,y+n+n); 80 _union(x+n+n,y); 81 } 82 } 83 } 84 printf("%d",lie); 85 return 0; 86 }

並查集 食物鏈 模板