ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 1182 食物链

2022-02-02 19:02:28  阅读:142  来源: 互联网

标签:食物链 int 1182 ++ else fa POJ ans find


题目链接:POJ 1182 食物链

题目大意:

题解:
正解是带权并查集,这里选用了一种开三倍并查集的思想。
开了三倍大小的标记数组来表示三个物种,\(1\)到\(n\)为\(A\)物种,\(n+1\)到\(2 \times n\)为\(B\)物种,\(2 \times n + 1\)到\(3 \times n\)为\(C\)物种。
如果\(u\)吃\(v\),则相对的\(u+n\)与\(v\)为一个物种,\(u+2\times n\)与\(v + n\)为一个物种,\(u\)与\(v + 2\times n\)为一个物种。
通过并查集关联同一物种。

#include <iostream>
#include <cstdio>
#include <ctype.h>
using namespace std;
#define MAXN 100010
#define INF 100000000
#define ll long long

ll read() {     // fast read
    ll ans = 0; int f = 1;  char x = getchar();
    while (!isdigit(x)) {
        if (x == '-')
            f = -1;
        x = getchar();
    }
    while (isdigit(x)) {
        ans = (ans << 3) + (ans << 1) + (x ^ 48);
        x = getchar();
    }
    return ans * f;
}

int fa[150050], n, k, act, u, v, ans;

int find(int x) {   // find father
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

int main() {
    n = read(), k = read();
    for (int i = 1; i <= n; ++i) {
        fa[i] = i;
        fa[i + n] = i + n;
        fa[i + n * 2] = i + n * 2;
    }
    while (k--) {
        act = read(), u = read(), v = read();
        if (u > n || v > n) {
            ans++;
            continue;
        }
        else if (act == 1) {
            if (find(u) == find(v + n) || find(v) == find(u + n)) {
                ans++;
            }
            else {  // same father
                fa[find(u)] = find(v);
                fa[find(u + n)] = find(v + n);
                fa[find(u + n * 2)] = find(v + n * 2);
            }
        }
        else {
            if (find(u) == find(v + n) || find(u) == find(v)) {
                ans++;
            }
            else {  // u -> v
                fa[find(u + n)] = find(v);
                fa[find(u + n * 2)] = find(v + n);
                fa[find(u)] = find(v + n * 2);
            }
        }
    }
    cout << ans;
    return 0;
}

正解如下:

// 带权并查集
#include <cstdio>
#include <iostream>
using namespace std;

int f[50005], d[50005], n, k, d1, x, y, ans;

int find(int x) {
    if (x != f[x]) {
        int xx = f[x];
        f[x] = find(f[x]);
        d[x] = (d[x] + d[xx]) % 3;
    }
    return f[x];
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) {
        f[i] = i;
        d[i] = 0;
    }
    for (int i = 1; i <= k; i++) {
        scanf("%d%d%d", &d1, &x, &y);
        if ((d1 == 2 && x == y) || (x > n || y > n)) {
            ans++;
            continue;
        }
        if (d1 == 1) {
            if (find(x) == find(y)) {
                if (d[x] != d[y]) ans++;
            } else {
                d[f[x]] = (d[y] - d[x] + 3) % 3;
                f[f[x]] = f[y];
            }
        }
        if (d1 == 2) {
            if (find(x) == find(y)) {
                if (d[x] != (d[y] + 1) % 3) ans++;
            } else {
                d[f[x]] = (d[y] - d[x] + 4) % 3;
                f[f[x]] = f[y];
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

标签:食物链,int,1182,++,else,fa,POJ,ans,find
来源: https://www.cnblogs.com/IzumiSagiri/p/15860615.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有