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