90pts求助
查看原帖
90pts求助
152652
AndyChen2005121楼主2020/11/26 18:00
#include <iostream>
// #include <vector>
#include <set>
#include <unordered_map>
using namespace std;
// vector<int> G[250000];
int fa[500000], degree[500000];
int n, m, f, cnt, ptr;
set<string> st;
unordered_map<string, int> mp;
void init() {
    for (int i = 1; i <= 300000; i++) {
        fa[i] = i;
    }
}
int get(int x) {
    if (fa[x] == x) {
        return x;
    }
    return fa[x] = get(fa[x]);
}
void merge(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
        fa[y] = x;
        f++; 
    }
}

int main() {
    init();
    string a, b;
    while(cin >> a >> b){
        if(!st.count(a)){
            mp[a]=ptr++;
            st.insert(a);
        }
        if(!st.count(b)){
            mp[b]=ptr++;
            st.insert(b);
        }
        // G[mp[a]].push_back(mp[b]);
        // G[mp[b]].push_back(mp[a]);
        merge(mp[a], mp[b]);
        degree[mp[a]]++;
        degree[mp[b]]++;

    }
    if((st.size()-f)!=1){
        cout << "Impossible" << endl;
        return 0;
    }
    for(int i = 0; i < st.size(); i++){
        if(degree[i] % 2 == 1){
            cnt++; 
        }
    }
    if(cnt == 0 || cnt==2){ 
        cout << "Possible" << endl;
    } else { 
        cout << "Impossible" << endl;
    }
    return 0;
}
2020/11/26 18:00
加载中...