Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 41675 | Accepted: 12120 |
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
思路:
带权并查集啊啊啊,公式还没理解啊啊啊,最近快疯了!!! 先存档,懂后补上!
参考:http://blog.csdn.net/c0de4fun/article/details/7318642/
http://blog.csdn.net/balloons2012/article/details/7871104
代码:
#include <stdio.h> #define N 50050 int n; int fa[N]; int rank[N]; void Init() { for(int i = 0; i < n; i ++){ fa[i] = i; rank[i] = 0; } } int getFather(int x) { if(x == fa[x]) return x; int oldfa = fa[x]; fa[x] = getFather(fa[x]); rank[x] = (rank[x] + rank[oldfa]) % 3; return fa[x]; } void unionset(int d, int x, int y) { int fx = getFather(x); int fy = getFather(y); if(fx == fy) return; fa[fx] = fy; rank[fx] = (rank[y] + d - rank[x] + 3) % 3; } int istrue(int d, int x, int y) { if(x > n || y > n || (x == y && d == 2)) return 0; int fx = getFather(x); int fy = getFather(y); if(fx != fy) // 暂未建立关系 return 1; else{ int p = ((d - 1) + rank[y]) % 3; if(rank[x] == p) return 1; else return 0; } } int main() { int k, d, x, y; scanf("%d%d", &n, &k) ; Init(); int ans = 0; while(k --){ scanf("%d%d%d", &d, &x, &y); if(! istrue(d, x, y)) ans ++; else unionset(d - 1, x, y); } printf("%d\n", ans); return 0; }