思路:拓珀排序

#include<iostream>
#define N 1005
using namespace std;
int flag[N][N]={0},cnt[N]={0};
int main()
{
	int i,j,p,r,e1,e2,n,count=0,t,tt,tp,tr;
		scanf("%d",&t);
	for(tt=1;tt<=t;tt++)
	{
			scanf("%d%d%d%d",&p,&r,&e1,&e2);
			n=p+r;
			for(i=0;i<e1;i++)
			{
				scanf("%d%d",&tp,&tr);
				tr+=p;
				if(flag[tr][tp]==0) 
				{
					cnt[tp]++;
					flag[tr][tp]=1;
				}
			}
			
			for(i=0;i<e2;i++)
			{
				scanf("%d%d",&tr,&tp);
				tr+=p;
				if(flag[tp][tr]==0) 
				{
					cnt[tr]++;
					flag[tp][tr]=1;
				}
			}
			
			count=0;
			for(i=0;i<n;i++)
			{
				if(cnt[i]==0)
				{
					count++;
					for(j=0;j<n;j++)
					{
						cnt[j]-=flag[i][j];
					}
					cnt[i]=-1;
					i=-1;
				}
			}
						//	for(i=0;i<n;i++) cout<<cnt[i]<<endl;
			if(count>=n) printf("Case %d: Impossible/n",tt);
			else                  printf("Case %d: Possible/n",tt);
			for(i=0;i<n;i++)
			{
				cnt[i]=0;
				for(j=0;j<n;j++) flag[i][j]=0;
			}
	}
	return 0;
}