程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1776 競賽圖的哈密爾頓回路

POJ 1776 競賽圖的哈密爾頓回路

編輯:C++入門知識

Task Sequences Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2062 Accepted: 583 Special Judge

Description

Tom has received a lot of tasks from his boss, which are boring to deal with by hand. Fortunately,Tom got a special machine - Advanced Computing Machine (ACM) to help him.
ACM works in a really special way. The machine can finish one task in a short time, after it's finishing one task, it should smoothly move to the next one, otherwise the machine will stop automatically. You must start it up again to make it continue working. Of course, the machine cannot move arbitrarily from one task to another. So each time before it starts up, one task sequence should be well scheduled. Specially, a single task also can be regarded as a sequence. In the sequence, the machine should be able to smoothly move from one task to its successor (if exists). After started up, the machine always works according to the task sequence, and stops automatically when it finishes the last one. If not all the tasks have been finished, the machine has to start up again and works according to a new sequence. Of course, the finished tasks can't be scheduled again.
For some unknown reasons, it was guaranteed that for any two tasks i and j, the machine can smoothly move from i to j or from j to i or both. Because the startup process is quite slow, Tom would like to schedule the task sequences properly, so that all the tasks can be completed with minimal number of startup times. It is your task to help him achieve this goal.

Input

Input contains several testcases. For each testcase, the first line contains only one integer n, (0 < n <= 1,000), representing the number of tasks Tom has received. Then n lines follow. Each line contains n integers, 0 or 1, separated by white spaces. If the jth integer in the ith line is 1, then the machine can smoothly move from task i to task j, otherwise the machine can not smoothly move from task i to task j. The tasks are numbered from 1 to n.
Input is terminated by end of file.

Output

For each testcase, the first line of output is only one integer k, the minimal number of startup times needed. And 2k lines follow, to describe the k task sequences. For each task sequence, the first line should contain one integer m, representing the number of tasks in the sequence. And the second line should contain m integers, representing the order of the m tasks in the sequence. Two consecutive integers in the same line should be separated by just one white space. Extra spaces are not allowed. There may be several solutions, any appropriate one is accepted.

Sample Input

3
0 1 1
1 0 1
0 0 0

Sample Output

1
3
2 1 3

Source


競賽圖:圖中的任意兩點間有且僅有一條有向弧連接

求競賽圖中的哈密頓路的算法:

首先,由數學歸納法可證競賽圖在n>=2時必存在哈密頓路;

(1)n=2時顯然;

(2)假設n=k時,結論成立,哈密頓路為V1,V2,...,Vi,...,Vk;

現添加第k+1個結點,若存在弧和弧,則可得哈密頓回路V1,V2,...,Vi,Vk+1,Vi+1,...,Vk;

若不存在上述的vi,考慮到Vk+1與v1~vk的連通狀況,則只有下面種原哈密頓路的情況:

1.所有的Vi(1,那麼可得哈密頓回路V1,V2,...,Vi,...,Vk,Vk+1;

2.所有的Vi(1,那麼可得哈密頓回路Vk+1,V1,V2,...,Vi,...,Vk;

3.存在一個中間結點m,使得所有的Vi(1<=i<=m)與Vk+1的弧方向為,所有的Vj(m,這時依然可以構造哈密頓路 V1,V2,...,Vi,...,Vk,Vk+1;

代碼:

/* ***********************************************
Author :rabbit
Created Time :2014/3/25 20:14:43
File Name :12.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=1010;
int next[maxn],pic[maxn][maxn];
int n;
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     while(cin>>n){
		 memset(pic,0,sizeof(pic));
		 string str;
		 getline(cin,str);
		 for(int i=1;i<=n;i++){
			 getline(cin,str);
			 for(int j=1;j<=n;j++)
				 pic[i][j]=str[(j-1)*2]-'0';
		 }
		 memset(next,0,sizeof(next));
		 int head=1,t;
		 for(int k=2;k<=n;k++){
			 bool flag=0;
			 for(int i=head;i;i=next[i])
				 if(pic[k][i]){
					 if(i==head)head=k;
					 else next[t]=k;
					 next[k]=i;
					 flag=1;break;
				 }
				 else t=i;
			 if(!flag)next[t]=k;
		 }
		 cout<<1<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved