POJ

来源:互联网 发布:风暴英雄mac版下载 编辑:程序博客网 时间:2024/05/21 17:04
#include<algorithm>#include<cstdio>#include<cstring>#include<climits>#include<queue>#include<stack>#include<set>#include<map>#include<vector>#include<iostream>#include<string>using namespace std;int f[3 * 50000 + 1];int m, n;int getf(int x){    if(f[x] == x)        return x;    else        return f[x] = getf(f[x]);}void unite(int x, int y){    x = getf(x);    y = getf(y);    if(x == y) return ;    f[x] = y;}bool same(int x, int y){    if(getf(x) == getf(y))        return 1;    return 0;}void init(){    for(int i = 1; i <= n*3; i++)        f[i] = i;}void solve(){    int t1, t2, t3;    int ans = 0;     for(int i = 0; i < m; i++)     {        scanf("%d %d %d", &t1, &t2, &t3);        if(t2 < 1 || t2 > n || t3 < 1 || t3 > n)        {            ans++;            continue;// 如果越界直接退出        }        if(t1 == 1)//当为同类的时候        {            if(same(t2,t3+n) || same(t2,t3+2*n) )//判断是否符合条件                ans++;            else            {                unite(t2,t3);                unite(t2+n, t3+n);                unite(t2+2*n, t3+2*n)                ;            }        }else // 为敌人时        {            if(same(t2,t3) || same(t2,t3+2*n)) // 判断是否符合条件                ans++;            else            {                unite(t2,t3+n);                unite(t2+n,t3+2*n);                unite(t2+2*n, t3);            }        }    }    printf("%d\n", ans);}int main(){    scanf("%d %d", &n, &m);    init();    solve();    return 0;}

0 0