Uva10158 war (and query set)

Source: Internet
Author: User

This question is used to check the application of the set. It is a good question type.

I also refer to other people's ideas, but the idea is very simple.

I have mentioned a lot of complicated things about friends and enemies. You will find that, after reading them thoroughly, it is actually a sentence. If you are a friend of mine, then we have public enemies.

That is to say, there is no intersection between the people in this circle of friends and those in the circle of enemies;

But how do they represent friends or enemies?

N people, represented by 2 * n vertices, the first n represents itself, and the last n represents the separation of corresponding points. If they are friends, they add themselves. If they are enemies, add the separation.

When determining whether it is an enemy, it depends on whether the corresponding separation and the other itself are in the same set; If judging whether it is a friend, it depends on whether the two are in a different set.

The Code is as follows:

#include <cstdio>#include <cstring>const int N = 20020;int fa[N], n;int find ( int a ) {    return fa[a] == a ? a : fa[a] = find(fa[a]);}void setFriend( int a, int b ) {    int x1, x2, y1, y2;    x1 = find(a), y1 = find(b);    x2 = find(a+n), y2 = find(b+n);    if ( x1 == y2 || x2 == y1 ) printf("-1\n");    else {        fa[x1] = y1;        fa[x2] = y2;    }}void setEnemy( int a, int b ) {    int x1, x2, y1, y2;    x1 = find(a), y1 = find(b);    x2 = find(a+n), y2 = find(b+n);    if ( x1 == y1 ) printf("-1\n");    else {        fa[x1] = y2;        fa[y1] = x2;    }}void areFriend( int a, int b ) {    int x, y;    x = find(a);    y = find(b);    if ( x == y ) printf("1\n");    else printf("0\n");}void areEnemy( int a, int b ) {    int x1, x2, y1, y2;    x1 = find(a), y1 = find(b);    x2 = find(a+n), y2 = find(b+n);    if ( x2 == y1 || x1 == y2 ) printf("1\n");    else printf("0\n");}    int main(){    while ( scanf("%d", &n) != EOF ) {        int c, a, b;        for ( int i = 0; i <= n*2; ++i ) fa[i] = i;        while ( 1 ) {            scanf("%d%d%d", &c, &a, &b);            if ( a == 0 && c == 0 && b == 0 ) break;            if ( c == 1 ) setFriend( a, b );            else if ( c == 2 ) setEnemy( a, b );            else if ( c == 3 ) areFriend( a, b );            else if ( c == 4 ) areEnemy( a, b );        }    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.