CodeForcs 1169B Good Triple

CodeForcs 1169B Good Triple

Topic links: http://codeforces.com/problemset/problem/1169/B

Description Title: digital you m does not exceed n, and finding a pair of x, y, m which meet at least a logarithmic number is equal to x or y.

Thinking: assuming as a first pair (a, b), if x, y exist, one of which is a certain number a, b are, then if present, (c, d), such that a, b are not same, then the x, y, another number must in c, d in, so x, y may be (a, c), (a, d), (b, c), (b, d), if not presence (c, d), the x, y is (a, b), to the number of m to be scanned, as long as when any one of the following fifth to: (a, b), (a, c) , (a, d), (b, c), (b, d). The time complexity of O (5m).

#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 300005;

pair<int, int> pa[maxn];
int n, m;

bool check(int a, int b) {
    for (int i = 0; i < m; i++) {
        if (a != pa[i].first && a != pa[i].second && b != pa[i].first && b != pa[i].second) return false;
    }
    return true;
}

int main() {
    while (cin >> n >> m) {
        if (m == 1) {
            cin >> pa[0].first >> pa[0].second;
            cout << "YES" << endl;
            continue;
        }
        cin >> pa[0].first >> pa[0].second;
        int a = pa[0].first, b = pa[0].second, c, d;
        for (int i = 1; i < m; i++) {
            scanf("%d%d", &pa[i].first, &pa[i].second);
            if (a != pa[i].first && a != pa[i].second && b != pa[i].first && b != pa[i].second) {
                c = pa[i].first;
                d = pa[i].second;
            }
        }
        bool flag = check(a, b) || check(a, c) || check(a, d) || check(b, c) || check(b, d);
        if (flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/youpeng/p/10954770.html