Пример #1
0
int main()
{
    Trie trie;
    DisjointSet djset;
    int degree[500001] = {0};
    string word1, word2;
    char w1[15], w2[15];
    int hash1, hash2;
    while (scanf("%s %s", w1, w2) != EOF) {
        hash1 = trie.addWord(w1);
        hash2 = trie.addWord(w2);
        degree[hash1]++;
        degree[hash2]++;
        djset.union_set(hash1, hash2);
    }
    int max_count = trie.max_count();
    int sum = 0;
    for (int i = 0; i < max_count; i++) {
        if (degree[i] & 1)
            sum++;
        if (sum > 2) {
            cout << "Impossible" << endl;
            return 0;
        }
    }

    for (int i = 0; i < max_count; i++) {
        if (djset.find_set(i) != djset.find_set(0)) {
            cout << "Impossible" << endl;
            return 0;
        }
    }
    cout << "Possible" << endl;
    return 0;
}
Пример #2
0
 inline bool
 same_component(Vertex u, Vertex v, DisjointSet& ds) {
   return ds.find_set(u) == ds.find_set(v);
 }