ZOJ 3332 (哈密顿路竞赛图)

 

参考:http://www.cnblogs.com/zhixingqiezhixing/archive/2012/04/21/2462351.html

 

View Code
 1 /*
 2 ZOJ  :3332 
 3 思路:
 4  这是个竞赛图问题
 5  寻找图中的哈密顿路
 6  竞赛图中一定有哈密顿路,
 7  竞赛图去掉一个顶点和其关联的边仍是竞赛图
 8  参考文献  哈密顿路 及 竞赛图 
 9 */ 
10 #include<iostream>
11 #include<cstdio>
12 #include<cstring>
13 #include<list>
14 using namespace std;
15 
16 const int con=110;
17 
18 bool map[con][con];
19 list<int> path;
20 list<int>::iterator cur,pre;
21 
22 int main()
23 {
24   int t,n,i,x,y;
25   bool f;
26   //freopen("in.txt","r",stdin);
27   //freopen("out.txt","w",stdout);
28   scanf("%d",&t);
29   while(t--)
30    {
31      memset(map,0,sizeof(map));
32      scanf("%d",&n);
33      int ct=n*(n-1)/2;
34      for(i=0;i!=ct;++i)
35       {
36         scanf("%d%d",&x,&y);
37         map[x][y]=true;
38       }
39      path.clear();
40      path.push_back(1);
41      
42      for(i=2;i<=n;++i)
43       {
44         f=0;
45         cur=path.begin();
46         if(map[i][*cur])
47          {
48            path.push_front(i);
49            f=1;
50            continue;
51          }
52         cur=path.end();
53         cur--;
54         if(map[*cur][i])
55         {
56           path.push_back(i);
57           f=1;
58           continue;
59         }
60         cur=pre=path.begin();
61         cur++;
62         
63         while(cur!=path.end())
64          {
65             if(map[*pre][i]&&map[i][*cur])
66              {
67                path.insert(cur,i);
68                f=1;
69                break;
70              }
71             pre++;
72             cur++;
73          }
74         if(!f)break;
75       }
76       
77       if(i<=n)printf("Impossible\n");
78       else
79        {
80          for(cur=path.begin();cur!=path.end();cur++)
81           {
82             if(cur != path.begin())//注意输出时的空格最后一个数字后不能有空格 
83                 printf(" ");
84             printf("%d", *cur);
85           }
86           printf("\n");
87        }
88    }
89   system("pause");
90   return 0;
91 }

 

转载于:https://www.cnblogs.com/zhixingqiezhixing/archive/2012/04/21/2462355.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值