sicily 1021. Couples

这个题就用stack来解决啦,如果一个用例中的couple可以全部移走,则将环拆成链后最后的一对couple依然相邻,可以全部移走,因此将环拆成一个链来解决不影响结果;

 

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

int couple_relation[200005];

int main() {
    int t1, t2, n,i;
    while (scanf("%d", &n) && n!=0) {
        stack<int> s;
        for (i=0; i<n; i++) {
            cin>>t1>>t2;
            couple_relation[t1] = t2;
            couple_relation[t2] = t1;
        }
        for (i=1; i<=2*n; i++) {
            if (s.empty()) {
                s.push(i);
            }else if(couple_relation[i] == s.top())
                s.pop();
            else
                s.push(i);
        }
        if (s.empty())
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}

 

posted @ 2016-05-11 21:44  zhou23  阅读(162)  评论(0编辑  收藏  举报