UVA 10763 Foreign Exchange

来源:互联网 发布:icc 文件 mac 编辑:程序博客网 时间:2024/06/18 03:39

题目大意:数字a->b需要b->a来相消,输入n个配对,看是否能全部相消
解题思路:用map记录a->b出现的次数 遇到相同的就+1 遇到b->a就-1, 用cou来记录剩余未相消的配对

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <map>using namespace std;int n;int cou;struct con {    int x, y;    bool operator <(const con &c)const {        if(x != c.x) {            return x < c.x;        }        if(y != c.y) {            return y < c.y;        }        return false;    }};map<con, int> Ma;void jud(int a, int b) {    con con1;    con1.x = b;    con1.y = a;    if(Ma[con1] > 0) {        Ma[con1]--;        cou = cou - 2;    }    else {        con con2;        con2.x = a;        con2.y = b;        Ma[con2]++;    }}int main() {    while(cin >> n) {        if(n == 0)            break;        cou = n;        for(int i = 0; i < n; i++) {            int a, b;            cin >> a >> b;            jud(a, b);        }        if(cou == 0) {            cout << "YES" << endl;        }        else {            cout << "NO" << endl;        }    }    return 0;}
原创粉丝点击