吝啬的国度

来源:互联网 发布:java开发pc客户端 编辑:程序博客网 时间:2024/04/27 21:56


http://acm.nyist.net/JudgeOnline/problem.php?pid=20

#include <stdio.h>
#include <string.h>
int map[100005];
void DFS(int start)
{
 int pre = map[start];
 if (pre != 0)
 {
  DFS(pre);
  map[pre] = start;
 }
}
int main()
{
 int m, n, start, a, b,i;
 scanf("%d",&m);
 while (m--)
 {
  scanf("%d%d",&n,&start);;
  memset(map, 0, sizeof(map));
  for (i = 1; i < n; i++)
  {
   scanf("%d%d",&a,&b);
   if (map[b] == 0)
       map[b] = a;  
   else
   {
    DFS(a);
    map[a] = b;
   }  
  }
  DFS(start);
  map[start] = -1;
  for (i = 1; i < n; i++)
   printf("%d ",map[i]);
  printf("%d\n",map[n]);
 }
 return 0;
}

0 0
原创粉丝点击