CodeForces 618B-Guess the Permutation【搜索】

港控/mmm° 2022-07-26 00:27 235阅读 0赞

B. Guess the Permutation

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as p**i. For all pairs of distinct integers i, j between 1 and n, he wrote the number a**i, j = min(p**i, p**j). He writes a**i, i = 0 for all integer i from 1 to n.

Bob gave you all the values of a**i, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.

Input

The first line of the input will contain a single integer n (2 ≤ n ≤ 50).

The next n lines will contain the values of a**i, j. The j-th number on the i-th line will represent a**i, j. The i-th number on the i-th line will be 0. It’s guaranteed that a**i, j = a**j, i and there is at least one solution consistent with the information given.

Output

Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.

Examples

Input

  1. 2
  2. 0 1
  3. 1 0

Output

  1. 2 1

Input

  1. 5
  2. 0 2 2 1 2
  3. 2 0 4 1 3
  4. 2 4 0 1 3
  5. 1 1 1 0 1
  6. 2 3 3 1 0

Output

  1. 2 5 4 1 3

Note

In the first case, the answer can be {1, 2} or {2, 1}.

In the second case, another possible answer is {2, 4, 5, 1, 3}.

解题思路:

按列说这一列中最大的数代表着这一位中会出现的最小值,保存可能的值深搜就可以,因为会有多组解输出一个就行。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. bool vis[100];
  6. int ans[100];
  7. struct node
  8. {
  9. int cc[100];
  10. int jj;
  11. }num[100];
  12. void wc(int ii,int xx,int n)
  13. {
  14. int i,j;
  15. num[ii].jj=0;
  16. for(i=xx;i<=n;i++)
  17. {
  18. num[ii].cc[num[ii].jj++]=i;
  19. }
  20. }
  21. int nn;
  22. void DFS(int x,int n)
  23. {
  24. if(nn==n)
  25. {
  26. return ;
  27. }
  28. int i,j;
  29. for(i=0;i<num[x].jj;i++)
  30. {
  31. if(!vis[num[x].cc[i]])
  32. {
  33. ans[nn++]=num[x].cc[i];
  34. vis[num[x].cc[i]]=true;
  35. DFS(x+1,n);
  36. if(nn==n)
  37. return ;
  38. vis[num[x].cc[i]]=false;
  39. nn--;
  40. }
  41. if(nn==n)
  42. return;
  43. }
  44. }
  45. int main()
  46. {
  47. int n;
  48. scanf("%d",&n);
  49. int i,j;
  50. for(i=1;i<=n;i++)
  51. {
  52. int mm=-1;
  53. for(j=0;j<n;j++)
  54. {
  55. int xx;
  56. scanf("%d",&xx);
  57. mm=max(mm,xx);
  58. }
  59. wc(i,mm,n);
  60. }
  61. nn=0;
  62. memset(vis,false,sizeof(vis));
  63. DFS(1,n);
  64. for(i=0;i<n;i++)
  65. {
  66. if(i==0)
  67. printf("%d",ans[i]);
  68. else
  69. printf(" %d",ans[i]);
  70. }
  71. printf("\n");
  72. return 0;
  73. }

发表评论

表情:
评论列表 (有 0 条评论,235人围观)

还没有评论,来说两句吧...

相关阅读