P2024 [NOI2001] 食物链

108 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 16 天,点击查看活动详情

[NOI2001] 食物链

题目描述

动物王国中有三类动物 A,B,CA,B,C,这三类动物的食物链构成了有趣的环形。AABBBBCCCCAA

现有 NN 个动物,以 1N1 \sim N 编号。每个动物都是 A,B,CA,B,C 中的一种,但是我们并不知道它到底是哪一种。

有人用两种说法对这 NN 个动物所构成的食物链关系进行描述:

  • 第一种说法是 1 X Y,表示 XXYY 是同类。
  • 第二种说法是2 X Y,表示 XXYY

此人对 NN 个动物,用上述两种说法,一句接一句地说出 KK 句话,这 KK 句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。

  • 当前的话与前面的某些真的话冲突,就是假话;
  • 当前的话中 XXYYNN 大,就是假话;
  • 当前的话表示 XXXX,就是假话。

你的任务是根据给定的 NNKK 句话,输出假话的总数。

输入格式

第一行两个整数,N,KN,K,表示有 NN 个动物,KK 句话。

第二行开始每行一句话(按照题目要求,见样例)

输出格式

一行,一个整数,表示假话的总数。

样例 #1

样例输入 #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

提示

对于全部数据,1N5×1041\le N\le 5 \times 10^41K1051\le K \le 10^5

分析

这题是个很难的题但是我不知道为什么出现在基础课里面,就很离谱,这就是一个带权并查集的一个典题,很难很难,然后就是要一个d数组记录一下。

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
#include <cmath>
#include <unordered_map>
#include <stack>
#include <queue>
#define ll long long
#define lowbit(x) x&(-x)
using namespace std;
typedef pair<int,int> PII;
typedef pair<string,int> PSI;
typedef stack<int> stk;
int gcd(int x,int y){
    return y?gcd(y,x%y):x;
}
ll qmi(ll x,ll y,int mod){
    ll res=1;
    while(y){
        if(y&1) res=res*x%mod;
        y>>=1;
        x=x*x%mod;
    }
    return res;
}
const int N=50010;
int fa[N],d[N],n,k,ans=0;
int find(int x){
    if(fa[x]!=x){
        int t=find(fa[x]);
        d[x]+=d[fa[x]];
        fa[x]=t;
    }
    return fa[x];
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++) fa[i]=i;
    memset(d,0,sizeof d);
    while(k--){
        int op,x,y;
        cin>>op>>x>>y;
        if(x>n || y>n) ans++;
        else{
            int px=find(x),py=find(y);
            if(op==1){
                if(px==py){
                    if((d[x]-d[y])%3!=0) ans++;
                }
                else{
                    fa[px]=py;
                    d[px]=d[y]-d[x];
                }
            }
            else{
                if(px==py){
                    if((d[x]-d[y]-1)%3) ans++;
                }
                else{
                    fa[px]=py;
                    d[px]=d[y]+1-d[x];
                }
            }
        }
    }
    cout<<ans<<"\n";
    return 0;
}

希望能帮助到大家。