994D - Open Communication - CodeForces Solution


bitmasks *1900

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <algorithm>
using namespace std;

struct pp
{
    int x, y, cnt, graph[10];
} p[15], q[15];

int ans, n, m, i, j, a, b, cnt, graph[10];

int main()
{
    cin >> n >> m;

    for (i = 1; i <= n; i++)
        cin >> p[i].x >> p[i].y;

    for (i = 1; i <= m; i++)
        cin >> q[i].x >> q[i].y;

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            if ((p[i].x == q[j].x || p[i].x == q[j].y || p[i].y == q[j].x || p[i].y == q[j].y) && !(p[i].x == q[j].x && p[i].y == q[j].y) && !(p[i].x == q[j].y && p[i].y == q[j].x))
            {
                ans = (p[i].x == q[j].x || p[i].x == q[j].y) ? p[i].x : p[i].y;
                if (graph[ans] == 0)
                {
                    graph[ans] = 1;
                    cnt++;
                }
                if (p[i].graph[ans] == 0)
                {
                    p[i].graph[ans] = 1;
                    p[i].cnt++;
                }
                if (q[j].graph[ans] == 0)
                {
                    q[j].graph[ans] = 1;
                    q[j].cnt++;
                }
            }
        }
    }

    if (cnt == 1)
    {
        cout << ans << endl;
    }
    else
    {
        for (i = 1; i <= n; i++)
        {
            if (p[i].cnt > 1)
            {
                cout << "-1" << endl;
                return 0;
            }
        }
        for (i = 1; i <= m; i++)
        {
            if (q[i].cnt > 1)
            {
                cout << "-1" << endl;
                return 0;
            }
        }
        cout << "0" << endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad