zoukankan      html  css  js  c++  java
  • K

    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 B to 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 AB (0 < AB <= nA != 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

    这是一个dfs解决的题目,只要搜索下就可以得到想要的结果了

    #include <iostream>
    
    using namespace std;
    
    #include <string.h>
    
    int map[111][111];
    
    int vis[111];
    
    int re[111];
    
    int n,flag;
    
    int lu;
    
    void dfs(int a)
    
    {
    
        if(lu==n)
    
        {
    
            flag=1;
    
            return;
    
        }
    
        for(int i=1;i<=n;i++)
    
        {
    
            if(!vis[i]&&map[a][i])
    
            {
    
                re[lu]=i;
    
                lu++;
    
                vis[i]=1;
    
                dfs(i);
    
                if(flag)return;
    
                vis[i]=0;
    
                lu--;
    
            }
    
        }
    
        return;
    
    }
    
    int main()
    
    {
    
        int t;
    
        cin>>t;
    
        while (t--) {
    
            cin>>n;
    
            if(n==1)
    
                cout<<1<<endl;
    
            else
    
            {
    
                memset(map, 0, sizeof(map));
    
                memset(re, 0, sizeof(re));
    
                memset(vis, 0, sizeof(vis));
    
                flag=0;
    
                for(int i=0;i<n*(n-1)/2;i++)
    
                {
    
                    int s,d;
    
                    cin>>s>>d;
    
                    map[s][d]=1;
    
                }
    
                for(int i=1;i<=n;i++)
    
                {
    
                    lu=0;
    
                    re[lu]=i;
    
                    lu++;
    
                    vis[i]=1;
    
                    dfs(i);
    
                    lu--;
    
                    if(flag)break;
    
                    vis[i]=0;
    
                }
    
                if(flag)
    
                {
    
                    for(int i=0;i<n;i++)
    
                    {
    
                        if(i)
    
                            cout<<" "<<re[i];
    
                        else
    
                            cout<<re[i];
    
                    }
    
                    cout<<endl;
    
                }
    
                else
    
                    cout<<"Impossible"<<endl;
    
            }
    
        }
    
        return 0;
    
    }
  • 相关阅读:
    数字货币量化分析[2018-06-04]
    主流币空转多。数字货币量化分析[2018-05-31]
    草莓糖CMT依旧强势,数字货币量化分析[2018-05-29]
    数字货币量化分析[2018-05-28]
    Python3中json的encode和decode
    如何选择数字货币交易所?
    数字货币量化分析[2018-05-27]
    数字货币量化分析[2018-05-26]
    Python3中的urlencode和urldecode
    Zipline Development Guidelines
  • 原文地址:https://www.cnblogs.com/Annetree/p/6362815.html
Copyright © 2011-2022 走看看