原题链接:
https://vjudge.net/problem/POJ-1182
AC代码:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int INF = 0x3fffffff;
void __init__()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
}
int fa[150050];
int find(int x)
{
if (x == fa[x])
return x;
return fa[x] = find(fa[x]);
}
void merge(int x, int y)
{
fa[find(x)] = find(y);
}
int main()
{
// __init__();
int n, k;
int ans = 0;
// cin >> n >> k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= 3*n; i++)
{
fa[i] = i;
}
for (int i = 1; i <= k; i++)
{
int op, x, y;
// cin >> op >> x >> y;
scanf("%d %d %d", &op, &x, &y);
if (x > n || y > n)
ans++;
else
{
if(op==1){
if(find(x+n)==find(y)||find(x+2*n)==find(y)){
ans++;
}
else{
merge(x,y);
merge(x + n, y + n);
merge(x + 2 * n, y + 2 * n);
}
}
else {
if(x==y||find(x)==find(y)||find(x)==find(y+n)){
ans++;
}
else {
merge(x, y + 2 * n);
merge(x + n, y);
merge(x + 2 * n, y + n);
}
}
}
}
// cout<<ans;
printf("%d\n", ans);
}
这题不用scanf会TLE,我明明加__init__了还是不行???所以__init__到底加速了个锤子??