題目描述
動物王國中有三類動物 A,B,C,這三類動物的食物鏈構成了有趣的環形。A 吃 B,B 吃 C,C 吃 A。 現有 N 個動物,以 1 - N 編號。每個動物都是 A,B,C 中的一種,但是我們并不知道 它到底是哪一種。 有人用兩種說法對這 N 個動物所構成的食物鏈關系進行描述: 第一種說法是“1 X Y”,表示 X 和 Y 是同類。 第二種說法是“2 X Y”,表示 X 吃 Y 。 此人對 N 個動物,用上述兩種說法,一句接一句地說出 K 句話,這 K 句話有的是真 的,有的是假的。當一句話滿足下列三條之一時,這句話就是假話,否則就是真話。 ? 當前的話與前面的某些真的話沖突,就是假話 ? 當前的話中 X 或 Y 比 N 大,就是假話 ? 當前的話表示 X 吃 X,就是假話 你的任務是根據給定的 N 和 K 句話,輸出假話的總數。
輸入輸出格式
輸入格式:
從 eat.in 中輸入數據 第一行兩個整數,N,K,表示有 N 個動物,K 句話。 第二行開始每行一句話(按照題目要求,見樣例)
輸出格式:
輸出到 eat.out 中 一行,一個整數,表示假話的總數。
輸入輸出樣例
輸入樣例#1:
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
輸出樣例#1:
3
說明
1 ≤ N ≤ 5 ? 10^4 1 ≤ K ≤ 10^5
Analysis
把動物關系分成三類,a是它本身,a+n是它吃什么,a+n+n是什么吃它 然后就各種判斷啊,同一類的合并,例如a吃b,那么a+n和b實際上是同一類動物,合并,以此類推 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; //