uva 10158 War (并查集)

uva 10158 War

四个操作:1)使AB成为朋友。2)使AB成为敌人。3)询问AB是不是朋友。4)询问AB是不是敌人。PS:敌人的敌人是朋友。朋友的朋友是朋友。

开一个2 * n的数组,0~n - 1表示的是朋友,n~2 * n - 1表示的是n号国家的敌人。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N = 100005;
typedef long long ll;
int n, f[N * 2];

void init() {
    for (int i = 0; i < n; i++) {
        f[i] = i;
        f[i + n] = i + n;
    }
}

int find(int x) {
    return x == f[x] ? x : f[x] = find(f[x]);
}   

void setFriends(int x, int y) {
    int px = find(x), py = find(y);
    int qx = find(x + n), qy = find(y + n);
    if (px == qy || qx == py) {
        printf("-1\n");
        return;
    }
    f[px] = py;
    f[qx] = qy;
}

void setEnemies(int x, int y) {
    int px = find(x), py = find(y);
    int qx = find(x + n), qy = find(y + n);
    if (px == py || qx == qy) {
        printf("-1\n");
        return;
    }
    f[px] = qy;
    f[qx] = py;
}

void areFriends(int x, int y) {
    int px = find(x), py = find(y);
    if (px == py) printf("1\n");
    else printf("0\n");
}

void areEnemies(int x, int y) {
    int px = find(x), py = find(y + n);
    if (px == py) printf("1\n");
    else printf("0\n");
}

int main() {
    scanf("%d", &n);
    int a, b, c;
    init();
    while (scanf("%d %d %d", &a, &b, &c) == 3) {
        if (!a && !b && !c) break;
        switch(a) {
            case 1: setFriends(b, c);
                    break;
            case 2: setEnemies(b, c);
                    break;
            case 3: areFriends(b, c);
                    break;
            case 4: areEnemies(b, c);
                    break;
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。

时间: 2024-10-25 05:35:06

uva 10158 War (并查集)的相关文章

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

UVA 10158 War(并查集)

War A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country's espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including y

UVA - 12232 Exclusive-OR (并查集扩展偏离向量)

Description You are not given n non-negative integersX0,X1,..., Xn-1 less than220, but they do exist, and their values never change. I'll gradually provide you some facts about them, and ask you some questions. There are two kinds of facts, plus one

uva 1455 - Kingdom(并查集+线段树)

题目链接:uva 1455 - Kingdom 题目大意:平面上又n个城市,初始时城市之间没有任何双向道路相连,要求一次执行指令. road A B :在城市A和城市B之间连接一条双向道路 line C:询问一条y=C的水平线上穿过多少州和这些州总共有多少城市. 一个联通分量算一个州,C保证为小数部分为0.5的实数. 解题思路:线段树维护每个位置上州和城市的个数,并查集维护哪些城市属于同一个州,并且要记录这些州上下范围.每次新建一条道路,要相应根据两个州的y坐标范围对线段树进行维护. #incl

uva 1160 - X-Plosives(并查集)

题目链接:uva 1160 - X-Plosives 题目大意:每一种化合物由两种简单的元素组成,现在有n种化合物要装车,如果出现说有k种化合物刚好由k种元组组成,就会发生化学发应.工人在每次装车的时候会检查是否有可能发生发应,有的话将放弃装车.问说最后有几个化合物没有装车. 解题思路:并查集,将每个元组视为一个节点,一种化合物视为边,如果新增一条边形成环则不能加入,统计没有添加的边数即可. #include <cstdio> #include <cstring> #include

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

Graph Connectivity UVA, 459(并查集)

Graph Connectivity UVA, 459 Time Limit: 3000 MS  Graph Connectivity  Consider a graph G formed from a large number of nodes connected by edges. G is said to be connected if a path can be found in 0 or more steps between any pair of nodes in G. For ex

UVA 10608 Friends 并查集

并查集水题 有n个人,m队朋友,朋友的朋友,也是朋友,A与B是朋友,B与C是朋友,那么A与C也是朋友,即A,B,C在同一个并查集里,合并即可: 最后会有几个"朋友圈子",求最大的朋友圈的人数. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int r[30005]; int x[30010]; i

Uva 10158 War

并查集的应用 直接阔成2倍.后N项为对应的敌人 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #inc