题目描述
动物王国中有三类动物 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 255
using 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;
}