# 食物链 洛谷2024 并查集

2017-02-06 07:52:25来源:CSDN作者:jpwang8人点击

## 题目描述

• 当前的话与前面的某些真的话冲突，就是假话
• 当前的话中 X 或 Y 比 N 大，就是假话
• 当前的话表示 X 吃 X，就是假话

100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5

3

1 ≤ N ≤ 5 ∗ 10^4
1 ≤ K ≤ 10^5

## Analysis

switch要用break啊记住记住要死要死

## Code

``#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <numeric>#include <iomanip>#include <bitset>#include <sstream>#include <fstream>#define debug puts("-----")#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)#define fill(x, t) memset(x, t, sizeof(x))#define min(x, y) x<y?x:y#define max(x, y) x>y?x:y#define PI (acos(-1.0))#define EPS (1e-8)#define INF (1<<30)#define ll long long#define db double#define ld long double#define N 100001#define E N * 8 + 1#define MOD 100000007#define L 255using namespace std;int fa[N * 3 + 1];inline int read(){    int x = 0, v = 1;    char ch = getchar();    while (ch < '0' || ch > '9'){        if (ch == '-'){            v = -1;        }        ch = getchar();    }    while (ch <= '9' && ch >= '0'){        x = (x << 1) + (x << 3) + ch - '0';        ch = getchar();    }    return x * v;}inline int getFather(const int &now){    return now == fa[now]? now: fa[now] = getFather(fa[now]);}inline int merge(const int &x, const int &y){    int fx = getFather(x), fy = getFather(y);    if (fx ^ fy){        fa[fx] = fy;        return 1;    }    return 0;}int main(void){    int n = read(), k = read();    int ans = 0;    rep(i, 1, n + n + n){        fa[i] = i;    }    rep(i, 1, k){        int opt = read(), x = read(), y = read();        if (x > n || y > n){            ans += 1;            // printf("in %d/n", i);        }else{            switch (opt){                case 1:{                    if (x ^ y){                        if (getFather(x) == getFather(y + n) || getFather(x) == getFather(y + n + n)){                            ans += 1;                            // printf("in %d/n", i);                        }else{                            merge(x, y);                            merge(x + n, y + n);                            merge(x + n + n, y + n + n);                        }                    }                    break;                }                case 2:{                    if (getFather(x) == getFather(y) || getFather(x + n + n) == getFather(y)){                        ans += 1;                        // printf("in %d/n", i);                    }else{                        merge(x, y + n + n);                        merge(x + n, y);                        merge(x + n + n, y + n);                    }                    break;                }            }        }    }    printf("%d/n", ans);    return 0;}``