【UVA-10763】Foreign Exchange【紫书】

【UVA-10763】Foreign Exchange【紫书】

问题链接:https://vjudge.net/problem/UVA-10763

Solution:

排序,常见的无序变有序解决问题的方法。

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct Node {
    int a, b;
    bool operator < (const Node & n) const {
        if (a != n.a) return a < n.a;
        return b < n.b;
    }
    bool operator != (const Node &n) const {
        return a != n.a || b != n.b;
    }
};
vector<Node> data;

inline void swap(int &a, int &b) {
    int tmp = a;
    a = b;
    b = tmp;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n, a, b;
    while (cin >> n && n) {
        data.clear();
        while (n--) {
            cin >> a >> b;
            if (a > b) swap(a, b);
            data.push_back({a, b});
        }
        if (data.size() & 1) {
            cout << "NO\n";
            continue;
        }
        sort(data.begin(), data.end());
        int i;
        for (i = 1; i < data.size(); i += 2)
            if (data[i] != data[i - 1]) break;
        if (i >= data.size()) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}