Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 60276 | Accepted: 17671 |
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
Source
思路:
因为b的权值是1,所以b被他的根节点a吃, c的权值是2,所以c吃它的根节点a, d的权值是0,所以d跟它的根节点
同类
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string> #include <cmath> #include <set> #include <queue> #include <algorithm> #include <vector> #include <stack> #include <map> using namespace std; #define esp 1e-8 const double PI = acos(-1.0); const long long inf = 1000000000; const long long mod = 10000007; typedef long long LL; #pragma comment(linker, "/STACK:1024000000,1024000000") //freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取 //freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中 int f[50005], v[50005]; int find(int x) { if (x != f[x]) { int u = f[x]; f[x] = find(f[x]); v[x] = (v[x] + v[u]) % 3; } return f[x]; } int main() { int n, m, i, j; scanf("%d%d", &n, &m);//注意不要多组输入否则会wa { for (i = 0; i <= n; ++i) f[i] = i; memset(v, 0, sizeof(v)); int ans = 0; while (m--) { int d, a, b; scanf("%d%d%d", &d, &a, &b); int fa = find(a); int fb = find(b); if (a > n || b > n || (a == b && d == 2)) { ans++; continue; } if (fa == fb) { if ((v[a] != (v[b] + d- 1) % 3)) { ans++; //continue; } } else { f[fa] = fb;//如果这里是f[fb] = fa的话,那么下面应该是v[fb] = (v[a] - v[b] + d - 1 + 3) % 3,上面应该是(v[b] != (v[a] + d- 1) % 3) //反正就是根节点的权值-子节点的权值 v[fa] = (v[b] - v[a] + d - 1 + 3) % 3; } } printf("%d\n", ans); } }