//#pragma warning (disable:4996)
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
const int maxVertexCount = 250000 * 2 + 1;
int ancestor[maxVertexCount];
int rank[maxVertexCount];
int colorID;
int degree[maxVertexCount + 1];
void InitializeSet()
{
for (int i = 0; i < maxVertexCount; ++i)
{
ancestor[i] = i;
}
}
int FindSet(int x)
{
if (ancestor[x] != x)
ancestor[x] = FindSet(ancestor[x]);
return ancestor[x];
}
void UnionSet(int lhs, int rhs)
{
int lhsParent = FindSet(lhs);
int rhsParent = FindSet(rhs);
if (rank[lhsParent] > rank[rhsParent])
{
ancestor[rhsParent] = lhsParent;
}
else
{
ancestor[lhsParent] = rhsParent;
if (rank[lhsParent] == rank[rhsParent])
{
++rank[rhsParent];
}
}
}
struct TrieNode
{
bool flag;
int id;
TrieNode* next[27];
TrieNode()
{
flag = false;
id = 0;
std::memset(static_cast<void*>(next), 0, sizeof(next));
}
};
TrieNode* root = new TrieNode();
int GetIndex(const char* str)
{
TrieNode* nodePtr = root;
for (int i = 0; str[i]; ++i)
{
int index = str[i] - 'a';
if (!nodePtr->next[index])
{
nodePtr->next[index] = new TrieNode();
}
nodePtr = nodePtr->next[index];
}
if(!nodePtr->flag)
{
nodePtr->flag = true;
nodePtr->id = ++colorID;
}
return nodePtr->id;
}
int main()
{
InitializeSet();
char firstColor[11] = { 0 }, secondColor[11] = { 0 };
std::ios::ios_base::sync_with_stdio(false);
while (std::cin >> firstColor >> secondColor)
{
int firstColorIndex = GetIndex(firstColor);
int secondColorIndex = GetIndex(secondColor);
++degree[firstColorIndex];
++degree[secondColorIndex];
UnionSet(firstColorIndex, secondColorIndex);
}
int s = FindSet(1);
int oddDegreeCount = 0;
bool isPossible = true;
for (int i = 1; i < colorID; ++i)
{
if (degree[i] & 1) ++oddDegreeCount;
if (oddDegreeCount > 2)
{
isPossible = false;
break;
}
if (FindSet(i) != s)
{
isPossible = false;
break;
}
}
if (oddDegreeCount == 1) isPossible = false;
if (isPossible)
{
std::printf("%s\n", "Possible");
}
else
{
std::printf("%s\n", "Impossible");
}
return 0;
}
POJ 2513
最新推荐文章于 2018-01-30 17:48:06 发布