首页 > 其他 > 详细

ZOJ 3332 Strange Country II

时间:2015-04-18 10:07:12      阅读:159      评论:0      收藏:0      [点我收藏+]

K - Strange Country II
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 to n. The unique way to travel in the country is taking planes. Strangely, in this strange country, for every two cities A and B, there is a flight from A to B or from Bto A, but not both. You can start at any city and you can finish your visit in any city you want. You want to visit each city exactly once. Is it possible?

Input

There are multiple test cases. The first line of input is an integer T (0 < T <= 100) indicating the number of test cases. Then T test cases follow. Each test case starts with a line containing an integer n (0 < n <= 100), which is the number of cities. Each of the next n * (n - 1) / 2 lines contains 2 numbers A, B (0 < A, B <= n, A != B), meaning that there is a flight from city A to city B.

Output

For each test case:

  • If you can visit each city exactly once, output the possible visiting order in a single line please. Separate the city numbers by spaces. If there are more than one orders, you can output any one.
  • Otherwise, output "Impossible" (without quotes) in a single line.

Sample Input

3
1
2
1 2
3
1 2
1 3
2 3

Sample Output

1
1 2
1 2 3


题目大意:给你n*(n-1)条路线,代表可以从a走到b。

问你遍历所有点的路径输出。(可以输出任意解,每个城市只能走一次)

因为可以输出任意解,直接暴力搜索就好了。

#include <cstdio>
#include <algorithm>
#include <map>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL __int64
typedef long long ll;
#define PI 3.1415926
int mat[150][150];
int vis[150];
int b[150];
int n,l,flag;
void dfs(int x)
{
    int i;
    if(l==n)
    {
        flag=1;
        return;
    }
    for(i=1; i<=n; i++)
    {
        if(!vis[i]&&mat[x][i])
        {
             b[l++]=i;
            vis[i]=1;
            dfs(i);
            if(flag==1)
                return;
            vis[i]=0;
            l--;
        }
    }
    return;
}
int main()
{
    int t,i;
    cin>>t;
    while(t--)
    {
        int a,c;
        cin>>n;
        if(n==1)
        {
            cout<<1<<endl;
            continue;
        }
        memset(b,0,sizeof(b));
        memset(mat,0,sizeof(mat));
        memset(vis,0,sizeof(vis));
        for(i=1; i<=n*(n-1)/2; i++)
        {
            cin>>a>>c;
            mat[a][c]=1;
        }
        flag=0;
        for(i=1; i<=n; i++)
        {
            l=0;
            b[l++]=i;
            vis[i]=1;
            dfs(i);
            l--;
            if(flag)
                break;
                vis[i]=0;
        }
        if(flag)
        {
            for(i=0; i<n; i++)
            {
                if(i!=n-1)
                    cout<<b[i]<<" ";
                else
                    cout<<b[i];
            }
            cout<<endl;
        }
        else
            cout<<"Impossible"<<endl;
    }
    return 0;
}


ZOJ 3332 Strange Country II

原文:http://blog.csdn.net/sky_miange/article/details/45110411

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!