AcWing 240. 食物链 边带权并查集

AcWing 240. 食物链

题意

动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。给出他们之间的关系,判断有几句话是假话。每句话的格式如下:

  • 1 X Y ,表示 X和Y是同类
  • 2 X Y ,表示X吃Y

当一句话满足下列三条之一时,这句话就是假话,否则就是真话。

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

输出有几句假话。

解法

考虑边带权并查集。

  • X吃Y可以转化为 X − Y = 1 X-Y=1 XY=1 ,X和Y同类可以转换为 X = Y X=Y X=Y
  • 如果X和Y是同类,则需要判断在之前的话中X和Y有没有关系,如果有关系且 X ! = Y ( m o d 3 ) X!=Y(mod3) X!=Y(mod3),则这句话是假话。
  • 如果X吃Y,且X和Y在之前已经有关系,需要判断 X − Y X-Y XY 是否等于1,即 d [ x ] − d [ y ] = 1 ( m o d 3 ) d[x]-d[y]=1(mod3) d[x]d[y]=1(mod3) ,不成立则这句话是假话。
  • 不能判断就把这句话的关系记录下来。
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
    static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
    if (p1 == pend) {
        p1 = buf;
        pend = buf + fread(buf, 1, BUF_SIZE, stdin);
        if (pend == p1) {
            IOerror = 1;
            return -1;
        }
    }
    return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror) return false;
    if (ch == '-') sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
    if (sign) x = -x;
    return true;
}
inline bool R(double &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror) return false;
    if (ch == '-') sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
    if (ch == '.') {
        double tmp = 1;
        ch = nc();
        for (; ch >= '0' && ch <= '9'; ch = nc())
            tmp /= 10.0, x += tmp * (ch - '0');
    }
    if (sign)
        x = -x;
    return true;
}
inline bool R(char *s) {
    char ch = nc();
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    for (; !blank(ch) && !IOerror; ch = nc())
        *s++ = ch;
    *s = 0;
    return true;
}
inline bool R(char &c) {
    c = nc();
    if (IOerror) {
        c = -1;
        return false;
    }
    return true;
}
template <class T, class... U>
bool R(T &h, U &... t) { return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
};  // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
    for (auto i = x.begin(); i != x.end(); _W(*i++))
        if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 5e4 + 5;
int fa[maxn], d[maxn];
int findroot(int x) {
    if (x == fa[x]) return x;
    int root = findroot(fa[x]);
    d[x] += d[fa[x]];
    return fa[x] = root;
}
void link(int x, int y, int s) {
    int xx = findroot(x), yy = findroot(y);
    if (xx == yy) return;
    fa[xx] = yy;
    d[xx] = s - d[x] + d[y];
}
int get3(int x) { return (x % 3 + 3) % 3; }
int main() {
    int n, k;
    R(n, k);
    rep(i, 1, n) fa[i] = i;
    int ans = 0;
    while (k--) {
        int op, x, y;
        R(op, x, y);
        if (x > n || y > n) {
            ++ans;
            continue;
        }
        int xx = findroot(x), yy = findroot(y);
        if (op == 1 && xx == yy && get3(d[x]) != get3(d[y]))
            ++ans;
        else if (op == 2 && xx == yy && get3(d[x] - d[y]) != 1)
            ++ans;
        else
            link(x, y, (op == 2 ? 1 : 0));
    }
    W(ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值