#include<stdio.h>
#ifdef _WIN32
	#include<conio.h>
	#include<Windows.h>
	#include<process.h>
#else
	char getch() {
		return getchar();
	}
#endif

void main()
{
	int i,j,k,m,n,s,t,domain,**Mrs;

	printf("\n		### How many domain : ");
	scanf("%d",&domain);
	Mrs = (int**)malloc(sizeof(int*)*domain);
	for(i=0;i<domain;i++)
	{
		*(Mrs+i) = (int*)malloc(sizeof(int)*domain);
	}
	for(i=0;i<domain;i++)
	{
		for(j=0;j<domain;j++)
		{
			(*(*(Mrs+i)+j)) = 0;
		}
	}

	printf("\n## Input Relation R(1..n) ##\n");
	printf("ex) 1 2 (to end : -1 –1)\n");
	do
	{
		scanf("%d %d",&i,&j);
		if(i == -1)
		{
			break;
		}else
		{
			(*(*(Mrs+i-1)+j-1)) = 1;
		}
	}while(1);

	printf("\n#### Relation Mr ####\n");
	for(i=0;i<domain;i++)
	{
		for(j=0;j<domain;j++)
		{
			printf("%d ",*(*(Mrs+i)+j));
		}
		printf("\n");
	}

	printf("\t\t\t<<Press Any Key>>");
	getch();
	printf("\n");

	for(i=0;i<domain;i++)
	{
		for(j=i+1;j<i+domain;j++)
		{
			for(k=i+1;k<i+domain;k++)
			{
				m = j%domain;
				n = k%domain;
				if(Mrs[i][n] == 1 && Mrs[m][i] == 1)
				{
					Mrs[m][n] = 1;
				}
			}
		}

		printf("#### W%d #### \n",i+1);
		for(s=0;s<domain;s++)
		{
			for(t=0;t<domain;t++)
			{
				printf("%d ",*(*(Mrs+s)+t));
			}
			printf("\n");
		}
		printf("\n");
		for(s=0;s<domain;s++)
		{
			for(t=0;t<domain;t++)
			{
				if(*(*(Mrs+s)+t) == 1)
				{
					printf("(%d, %d) ",s+1,t+1);
 				}
			}
		}
		printf("\n");
		printf("\t\t\t<<Press Any Key>>");
		getch();
		printf("\n\n");
	}

	for(i=0;i<domain;i++)
	{
		free(*(Mrs+i));
	}
	free(Mrs);
	system("PAUSE");
}