1. 程式人生 > >poj1041 John's trip (無向圖求歐拉回路方案)

poj1041 John's trip (無向圖求歐拉回路方案)

John's trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5950 Accepted: 1946 Special Judge

Description

Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible. Very soon he realized that the best way to do it was to travel through each street of town only once. Naturally, he wanted to finish his trip at the same place he started, at his parents' house. 

The streets in Johnny's town were named by integer numbers from 1 to n, n < 1995. The junctions were independently named by integer numbers from 1 to m, m <= 44. No junction connects more than 44 streets. All junctions in the town had different numbers. Each street was connecting exactly two junctions. No two streets in the town had the same number. He immediately started to plan his round trip. If there was more than one such round trip, he would have chosen the one which, when written down as a sequence of street numbers is lexicographically the smallest. But Johnny was not able to find even one such round trip. 

Help Johnny and write a program which finds the desired shortest round trip. If the round trip does not exist the program should write a message. Assume that Johnny lives at the junction ending the street appears first in the input with smaller number. All streets in the town are two way. There exists a way from each street to another street in the town. The streets in the town are very narrow and there is no possibility to turn back the car once he is in the street 

Input

Input file consists of several blocks. Each block describes one town. Each line in the block contains three integers x; y; z, where x > 0 and y > 0 are the numbers of junctions which are connected by the street number z. The end of the block is marked by the line containing x = y = 0. At the end of the input file there is an empty block, x = y = 0.

Output

Output one line of each block contains the sequence of street numbers (single members of the sequence are separated by space) describing Johnny's round trip. If the round trip cannot be found the corresponding output block contains the message "Round trip does not exist."

Sample Input

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

Sample Output

1 2 3 5 4 6 

此題目從半上午做到這時候,首先把演算法弄懂了,然後就是找bug將近三個小時!!bug?判斷相等的時候要用兩個等號 活生生少寫一個!!!!醉了~。核心思想是在判斷完有歐拉回路的情況下,這個可以從所有點度數都是偶數的充分條件來判定,然後從標號最小的邊開始dfs,因為程式裡有個對鄰接表排序的過程你,那麼必然之後沿著邊的標號上升的dfs過程。遇到走不通就回溯,那麼此時必然還要存在環,否則不會出現走不通,那時應該全圖是一個環才對。遇到最小序環(假設存在)且無法繼續dfs回溯的時候,回溯出來的那條邊必然到最後是歐拉回路最後一條邊,直接push,否則就會出現沿著歐拉回路走的時候“過快”的回到原點了。遇到最小序環且無法走通的情況下,此時就要從最小序環遍歷各點dfs並按照邊標號上升的順序深搜其他環,那麼path裡面到最後就儲存的是尤拉路徑的 逆序,reverse一下就麼麼噠了~
<pre name="code" class="cpp">#include<iostream>
 #include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;

//input: adj 全域性變數,adj【i】表示從結點i連出的所有邊
//判斷是否有解。有向無向皆可,path(全域性)儲存歐拉回路。
const int maxn=2000;
const int maxm=1000000;
int father[maxn];
vector< pair<int,int > > adj[maxn];
bool vis[maxm];

int getFather(int x)
{
     return x==father[x]?x:father[x]=getFather(father[x]);
//    int root=a;
//    int temp;
//    while(father[root]!=root)
//        root=father[root];
//    while(father[a]!=root)
//    {
//        temp=a;
//        a=father[a];
//        father[temp]=root;
//    }
}

void add (int x,int y,int z)
{
    adj[x].push_back(make_pair(z,y));
    adj[y].push_back(make_pair(z,x));
}

vector<int >path;

void dfs(int u)
{
    for(int it=0; it<adj[u].size(); it++)
        if(!vis[adj[u][it].first])
        {
            vis[adj[u][it].first]=1;
            dfs(adj[u][it].second);
            path.push_back(adj[u][it].first);
        }
}

bool solve()
{
    for(int i=0; i<maxn; i++)
        father[i]=i;
    for(int i=0; i<maxn; i++)
    {
        for(int j =0; j < adj[i].size() ; ++j)
        {
            father[getFather(i)]=getFather(adj[i][j].second);
        }
    }
    int origin=-1;
    for(int i=0; i<maxn; i++)
        if(adj[i].size())
        {
            if(adj[i].size()%2==1)return 0;
            if(origin==-1)origin=i;
            if(getFather(i)!=getFather(origin))return 0;
            sort(adj[i].begin(),adj[i].end());
        }
    path.clear();
    memset(vis,0,sizeof(vis));
    if(origin!=-1)dfs(origin);
    reverse(path.begin(),path.end());
    return 1;
}

int main()
{
    int x,y,z;
    while(scanf("%d%d",&x,&y)!=EOF)
    {
        if(x==0&&y==0)
            break;
        scanf("%d",&z);
        for(int i=0; i<maxn; i++)
            adj[i].clear();
        add(x,y,z);
        while(scanf("%d%d",&x,&y)!=EOF)
        {
            if(y==0&&x==0)
                break;
            scanf("%d",&z);
            add(x,y,z);
        }

        if(solve())
        {
            int i;
            for( i=0; i<path.size()-1; i++)
                printf("%d ",path[i]);
            printf("%d\n",path[i]);
        }
        else
            printf("Round trip does not exist.\n");
    }
    return 0;
}