UVA10158 - War(并查集)

题目链接

题目大意:有n个国家,每个国家有友国和敌国,并且遵守着朋友的朋友是朋友,敌人的敌人是朋友的原则,给你四种操作,题目中有描述,然后让你根据操作进行,对应输出相应的结果。

解题思路:题目里面有敌对关系和朋友关系,朋友的关系可以使用并查集,但是敌对的关系是不行的。转化敌对的关系。假设0~n - 1,代表国家。n~2 * n - 1 代表0~n - 1这些国家的敌国。例如 1 和 4是敌对关系,因为敌人的敌人是朋友,那么1 + n 和 4 与 1 和4 + n就是朋友的关系,将敌对的关系转换成朋友的关系,然后朋友的关系就可以用并差集了(朋友具有传递的关系)。

代码:


#include <cstdio>
#include <cstring>

const int maxn = 10005;
int N;
int p[maxn * 2];

void init () {

    for (int i = 0; i < 2 * N; i++)
        p[i] = i;
}

int getParent(int x) {
    return x == p[x] ? x : p[x] = getParent(p[x]);
}

bool setfriend (int x, int y) {

    int  x1 = getParent(x);
    int  y1 = getParent(y);
    int  x2 = getParent(x + N);
    int  y2 = getParent(y + N);

    if (x1 == y2 || y1 == x2)
        return false;
    else {

        p[x1] = y1;
        p[x2] = y2;
        return true;
    }
}

bool setenemies(int x, int y ) {

    int  x1 = getParent(x);
    int  y1 = getParent(y);
    int  x2 = getParent(x + N);
    int  y2 = getParent(y + N);

    if (x1 == y1 || y2 == x2)
        return false;
    else {

        p[x1] = y2;
        p[y1] = x2;
        return true;
    }
}

bool is_friend(int x, int y) {

    int  x1 = getParent(x);
    int  y1 = getParent(y);
    int  x2 = getParent(x + N);
    int  y2 = getParent(y + N);

    if (x1 == y1)
        return true;
    return false;
}

bool is_enemies(int x, int y) {

    int  x1 = getParent(x);
    int  y1 = getParent(y);
    int  x2 = getParent(x + N);
    int  y2 = getParent(y + N);

    if (x1 == y2 || x2 == y1)
        return true;
    return false;
}

int main () {

    int op, x, y;

    scanf ("%d", &N);
    init();

    while (scanf ("%d%d%d", &op, &x, &y) != EOF && (op || x || y)) {

        if (op == 1) {
            if (setfriend(x, y) == false)
                printf("-1\n");
        } else if (op == 2) {
            if (setenemies(x, y) == false)
                printf ("-1\n");
        } else if (op == 3)
            printf ("%d\n", is_friend(x, y));
        else
            printf ("%d\n", is_enemies(x, y));
    }
    return 0;
}
时间: 2024-10-12 10:54:41

UVA10158 - War(并查集)的相关文章

ZOJ 3261 Connections in Galaxy War (并查集)

Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began

CSU1601: War(并查集)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 82  Solved: 24 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B are

csu 1601: War(并查集求每次去掉一条边后连通分量的个数)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 85  Solved: 25 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B are

UVA 10158 War 并查集

D - War(8.4.3) Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: War A war is being lead between two countries, A and B. As a loyal citizen of C, you dec

csu 1601 1601: War (并查集 kruskal)

题意:有n个村子 由m条路联通 其中q条路会依次被摧毁 问每次摧毁后会有多少片村庄被孤立 思路:首先算出q条路都被摧毁后被孤立的村庄数 然后再逆序把每条路修复上 每修复一条孤立的村庄就减少一片 最后再输出每次记录的结果 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int vis[100000+100]; int f

ZOJ3261 Connections in Galaxy War 并查集

分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; typedef pair<int,int>pii; const int N=1e4+5; pii p[N<<1]; int v[N],fa[N],n,m,q; bo

1601: War(并查集)

Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that the

CSU 1601 War (并查集)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 202  Solved: 58 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B ar

CSUOJ 1601 War (离线并查集求连通块个数)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 130  Solved: 38 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B ar