Jerry Mouse
Jerry Mouse
Time Limit: 1000ms Memory limit: 65536K ^_^
题目描述
Kudo and Saya are good friends, and they are always together.
But today, since Saya is not here (Where is Saya? You can get the answer at Problem F), Kudo fells very bored. So Kudo starts to watch TV for fun.
Now, she is watching the famous cartoon “Tom and Jerry”. Jerry’s home have many entrance, he always enter his home in an entrance and get out from another.
Kudo suddenly thought that what will happen if there are many Jerrys?
Kudo finds out a paper box and dig many holes in the side of the box. Then, she marks every hole with an integer, representing its owner. Finally, she signs the box “Kudo’s House” as shown in Figre 1.
![](http://acm.sdut.edu.cn/image/2160_1.png)
![](http://acm.sdut.edu.cn/image/2160_2.png)
She defined mouse A and mouse B is the same mouse if and only if:
a) mouse A and mouse B are in a same house and A equals to B;
b) mouse A and mouse B are in different houses, but they have a hole that combined together, such as mouse 3 in Kudo’s House and mouse 7 in Lin’s House;
c) There exits a lists of mouse M1, M2 … Mp, while A and M1 are the same, M1 and M2 are the same … Mp and B are the same.
But there is a problem: Suppose mouse 3 in Kudo’s House is the same with mouse 7 in Lin’s House, while mouse 7 in
Lin’s House is the same with mouse 4 in Kudo’s House. It means that mouse 3 and 4 in Kudo’s House are the same!
Kudo is very confused, so she comes to you for help.
Given the initial N boxes, can you tell her finally each hole belongs to whom?
Here, N always equals to 1, 4, 9, 16, 25, 36, 49, 64, 81 or 100. Kudo always combines the boxes as shown in Figure 3, while n = √N.
But today, since Saya is not here (Where is Saya? You can get the answer at Problem F), Kudo fells very bored. So Kudo starts to watch TV for fun.
Now, she is watching the famous cartoon “Tom and Jerry”. Jerry’s home have many entrance, he always enter his home in an entrance and get out from another.
Kudo suddenly thought that what will happen if there are many Jerrys?
Kudo finds out a paper box and dig many holes in the side of the box. Then, she marks every hole with an integer, representing its owner. Finally, she signs the box “Kudo’s House” as shown in Figre 1.
![](http://acm.sdut.edu.cn/image/2160_1.png)
Figure 1 The gray part represents a hole, and the numbers means its owner, i.e. the two gray parts in the lower left corner means the two holes belongs to mouse 2.
Kudo think it is very interesting, so she makes a lot of boxes, and sign them “Saya’s House”, “Riki’s House”, “Lin’s House” and so on.
![](http://acm.sdut.edu.cn/image/2160_2.png)
Figure 2 The jointed gray parts represent the same hole.
At last, she combines them together to make a large box as shown in Figure 2.She defined mouse A and mouse B is the same mouse if and only if:
a) mouse A and mouse B are in a same house and A equals to B;
b) mouse A and mouse B are in different houses, but they have a hole that combined together, such as mouse 3 in Kudo’s House and mouse 7 in Lin’s House;
c) There exits a lists of mouse M1, M2 … Mp, while A and M1 are the same, M1 and M2 are the same … Mp and B are the same.
But there is a problem: Suppose mouse 3 in Kudo’s House is the same with mouse 7 in Lin’s House, while mouse 7 in
Lin’s House is the same with mouse 4 in Kudo’s House. It means that mouse 3 and 4 in Kudo’s House are the same!
Kudo is very confused, so she comes to you for help.
Given the initial N boxes, can you tell her finally each hole belongs to whom?
Here, N always equals to 1, 4, 9, 16, 25, 36, 49, 64, 81 or 100. Kudo always combines the boxes as shown in Figure 3, while n = √N.
Figure 3 How Kudo combines the boxes.
输入
The input consists of several test cases.
The first line of input in each test case contains two integers N (0<N≤100) and M (0<M≤1000), which represent the number of boxes and the number of holes in each side of the box. In every side of the N boxes,You can assume that there are always M holes, and the M holes are arranged with equal spacing.
Each of the next N lines containing 4M integers representing the holes on the boxes. The first M integers represent the holes on the upside, from left to right; the second M integers represent the holes on the downside, from left to right; the third M integers represent the holes on the leftward, from up to down; the forth M integers represent the holes on the rightward, from up to down. You can assume that the hole number is not greater than M*2.
The last case is followed by a line containing two zeros.
The first line of input in each test case contains two integers N (0<N≤100) and M (0<M≤1000), which represent the number of boxes and the number of holes in each side of the box. In every side of the N boxes,You can assume that there are always M holes, and the M holes are arranged with equal spacing.
Each of the next N lines containing 4M integers representing the holes on the boxes. The first M integers represent the holes on the upside, from left to right; the second M integers represent the holes on the downside, from left to right; the third M integers represent the holes on the leftward, from up to down; the forth M integers represent the holes on the rightward, from up to down. You can assume that the hole number is not greater than M*2.
The last case is followed by a line containing two zeros.
输出
For each case, print the case number (1, 2 …) and 4*n*M integers (n=√N) represents the holes on the upside, downside, leftward and rightward side of the large box, using the same format as the input file.
In your answer, the holes signed by the same numbers should belong to the same mouse, while the holes signed by different numbers should belong to different mouse. The number should be an integer, starting from 1. Since there are multiply solutions, please print the one whose first number is the least. If there are still multiply solutions, print the one whose second number is the least, and so on.
Your output format should imitate the sample output. Print a blank line after each test case.
In your answer, the holes signed by the same numbers should belong to the same mouse, while the holes signed by different numbers should belong to different mouse. The number should be an integer, starting from 1. Since there are multiply solutions, please print the one whose first number is the least. If there are still multiply solutions, print the one whose second number is the least, and so on.
Your output format should imitate the sample output. Print a blank line after each test case.
示例输入
4 1 2 2 1 1 2 2 1 1 1 1 2 2 1 1 2 2 0 0
示例输出
Case 1: 1 2 1 2 3 4 3 4
提示
并查集
来源
2010年山东省第一届ACM大学生程序设计竞赛
代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<string> 6 #include<cmath> 7 #define N 104 8 #define M 1000 9 using namespace std; 10 int n,d[4][N][M+5],fa[M*M+5],num[M*M+5];//u,d,l,r 11 int find(int x){ 12 if(x==fa[x]) 13 return x; 14 return fa[x]=find(fa[x]); 15 } 16 void merge(int x,int y){ 17 int fx=find(x); 18 int fy=find(y); 19 if(fx==fy) 20 return ; 21 fa[fx]=fy; 22 return ; 23 } 24 int cal(int k,int i){ 25 if(k==0) 26 return i; 27 if(k==1) 28 return n*n-n+i; 29 if(k==2) 30 return i*n; 31 return i*n+n-1; 32 } 33 int main() 34 { 35 int m,i,j,k,cnt; 36 int ics=0; 37 while(~scanf("%d%d",&n,&m)){ 38 if(!n&&!m) 39 break; 40 for(i=0;i<n;i++){ 41 for(k=0;k<4;k++){ 42 for(j=0;j<m;j++){ 43 scanf("%d",&d[k][i][j]); 44 } 45 } 46 } 47 cnt=1; 48 for(i=0;i<n;i++){ 49 memset(num,0,sizeof(num)); 50 for(k=0;k<4;k++){ 51 for(j=0;j<m;j++){ 52 if(!num[d[k][i][j]]){ 53 num[d[k][i][j]]=cnt++; 54 } 55 d[k][i][j]=num[d[k][i][j]]; 56 } 57 } 58 } 59 for(i=0;i<=cnt;i++) 60 fa[i]=i; 61 n=sqrt((double)(n+0.5)); 62 for(i=0;i<n;i++){ 63 for(j=0;j<n-1;j++){ 64 for(k=0;k<m;k++){ 65 merge(d[3][i*n+j][k],d[2][i*n+j+1][k]); 66 } 67 } 68 } 69 for(i=0;i<n-1;i++){ 70 for(j=0;j<n;j++){ 71 for(k=0;k<m;k++){ 72 merge(d[1][i*n+j][k],d[0][i*n+j+n][k]); 73 } 74 } 75 } 76 printf("Case %d:\n",++ics); 77 cnt=1; 78 memset(num,0,sizeof(num)); 79 for(k=0;k<4;k++) 80 for(i=0;i<n;i++){ 81 for(j=0;j<m;j++){ 82 int t=cal(k,i); 83 d[k][t][j]=find(d[k][t][j]); 84 if(!num[d[k][t][j]]) 85 num[d[k][t][j]]=cnt++; 86 printf("%d ",num[d[k][t][j]]); 87 } 88 } 89 printf("\n\n"); 90 } 91 return 0; 92 }