标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 51148 | Accepted: 14950 |
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~n 自己
n+1~2n 被吃
2n+1~3n 吃
#include <stdio.h> int n, k, ans, fa[200000], r, x, y; int getfather(int x){fa[x] == x ? x : fa[x] = getfather(fa[x]);} int judge(int x, int y){return getfather(x) == getfather(y);} void unite(int x,int y) { int u = getfather(x); int v = getfather(y); if(u != v) fa[u] = v; } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= 3*n; i++)fa[i] = i; for (int i = 1 ; i <= k; i++) { scanf("%d%d%d", &r, &x, &y); if (x > n || y > n){ans++;continue ;} if (r == 1) { if (judge(x, y + n)||judge(x, y + 2*n))ans++; else { unite(x,y); unite(x+n,y+n); unite(x+2*n,y+2*n); } } if (r == 2) { if (judge(x, y)||judge(x, y + 2*n))ans++; else { unite(x,y+n); unite(x+n,y+2*n); unite(x+2*n,y); } } } printf ("%d", ans); }
标签:
原文地址:http://www.cnblogs.com/z52527/p/4596612.html