90pts WA on #9
查看原帖
90pts WA on #9
495469
Amidst楼主2024/8/16 09:40

rt

#include <bits/stdc++.h>
using namespace std;
int n=0,m=0,k;
int ind[2500005],outd[2500005];
int ev=0,od=0;
bool flag=true;
int now[2500005];
vector<int> a[2500005];
stack<int> _St;
map<string,int> _Mp;
string s,st;
int fa[2500005];
int cnt=0;
int find(int x)
{
	return x==fa[x]?fa[x]:fa[x]=find(fa[x]);
}
bool merge(int x,int y)
{
	int fx=find(x),fy=find(y);
	fa[fx]=fy;
	return fx!=fy;
}
int main()
{
	while(cin>>s>>st)
	{
		int x,y;
		if(_Mp[s]!=0)
			x=_Mp[s];
		else
			_Mp[s]=x=++n;
		if(_Mp[st]!=0)
			y=_Mp[st];
		else
			_Mp[st]=y=++n;
		a[x].push_back(y);
		outd[x]++;
		ind[y]++;
		m++;
		if(!fa[x])
			fa[x]=x;
		if(!fa[y])
			fa[y]=y;
		cnt+=merge(x,y);
	}
	if(cnt<n-1)
	{
		printf("Impossible\n");
		return 0;
	}
	k=1;
	for(int i=1;i<=n;i++)
	{
		sort(a[i].begin(),a[i].end());
		int tmp=ind[i]-outd[i];
		if(tmp!=0)
		{
			flag=false;
			if(tmp==1)
				ev++;
			else
				if(tmp==-1)
				{
					od++;
					k=i;
				}
				else
				{
					printf("Impossible\n");
					return 0;
				}
		}
	}
	if(!flag&&(ev!=od||od!=1))
	{
		printf("Impossible\n");
		return 0;
	}
	printf("Possible\n");
	return 0;
}
2024/8/16 09:40
加载中...