POJ1208(模拟)

题意有些晦涩难懂。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
	int A[26];
	int n;
	scanf("%d",&n);
	char str1[100],str2[100];
	int head[26];
	int pre[26];
	int a,b;
	int i;

	for (i=0;i<=n-1;i++)
		head[i]=i;
	memset(A,-1,sizeof(A));
	memset(pre,-1,sizeof(pre));
	while (1)
	{
		scanf("%s",str1);
		if (!strcmp(str1,"quit")) break;
		scanf("%d %s %d",&a,str2,&b);
		if (head[a]==head[b]) continue;

		int temp,t;
		if (str1[0]=='m' && str2[1]=='n')
		{
			temp=b;
			while (1)
			{
				t=temp;
				temp=A[temp];
				if (temp==-1)
					break;
				head[temp]=temp;
				pre[temp]=-1;
				A[t]=-1;
			}
			temp=a;
			while (1)
			{
				t=temp;
				temp=A[temp];
				if (temp==-1)
					break;
				head[temp]=temp;
				pre[temp]=-1;
				A[t]=-1;
			}
			A[b]=a;
			if (pre[a]!=-1)
			  A[pre[a]]=-1;
			pre[a]=b;
			head[a]=head[b];
		}
		else if (str1[0]=='m' && str2[1]=='v')
		{
			temp=a;
			while (1)
			{
				t=temp;
				temp=A[temp];
				if (temp==-1)
					break;
				head[temp]=temp;
				pre[temp]=-1;
				A[t]=-1;
			}
			temp=b;
			while (temp!=-1)
			{
				t=temp;
				temp=A[temp];
			}
			A[t]=a;
			if (pre[a]!=-1)
			  A[pre[a]]=-1;
			pre[a]=t;
			head[a]=head[b];
		}
		else if (str1[0]=='p' && str2[1]=='n')
		{
			temp=b;
			while (1)
			{
				t=temp;
				temp=A[temp];
				if (temp==-1)
					break;
				head[temp]=temp;
				pre[temp]=-1;
				A[t]=-1;
			}
			A[b]=a;

			if (pre[a]!=-1)
			  A[pre[a]]=-1;
			pre[a]=b;
			//printf("%d xxx\n",pre[a]);
			head[a]=head[b];
		}
		else
		{
			temp=b;
			while (temp!=-1)
			{
				t=temp;
				temp=A[temp];
			}
			A[t]=a;
			if (pre[a]!=-1)
			  A[pre[a]]=-1;

			pre[a]=t;

			head[a]=head[b];
		}
	}

	for (i=0;i<=n-1;i++)
	{
		printf("%d:",i);
		int x=i;
		if (head[i]==i)
		{
			while (x!=-1)
			{
				printf(" %d",x);
				x=A[x];
			}
		}
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(POJ1208(模拟))