• Strange Country II 暴力dfs


    这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <string>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    #define MOD 1000000007
    #define pi acos(-1.0)
    
    using namespace std;
    
    typedef long long           ll      ;
    typedef unsigned long long  ull     ;
    typedef unsigned int        uint    ;
    typedef unsigned char       uchar   ;
    
    template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
    template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}
    
    const double eps = 1e-7      ;
    const int N = 210            ;
    const int M = 1100011*2      ;
    const ll P = 10000000097ll   ;
    const int MAXN = 10900000    ;
    
    int n;
    int g[110][110], vis[110], ans[110];
    
    void init(){
        memset(g, 0, sizeof(g));
    }
    
    bool dfs(int pos, int level){
        ans[level] = pos;
        if(level >= n){
            return true;
        }
        for(int i = 1; i <= n; ++i){
            if(i == pos)    continue;
            if(!vis[i] && g[pos][i]){
                vis[i] = 1;
                if(dfs(i, level + 1)){
                    return true;
                }
                vis[i] = 0;
            }
        }
    }
    
    int main(){
        std::ios::sync_with_stdio(false);
        int i, j, t, k, u, v, numCase = 0;
        cin >> t;
        while(t--){
            init();
            cin >> n;
            for(i = 1; i <= n * (n - 1) / 2; ++i){
                cin >> u >> v;
                g[u][v] = 1;
            }
            for(i = 1; i <= n; ++i){
                memset(vis, 0, sizeof(vis));
                memset(ans, 0, sizeof(ans));
                vis[i] = 1;
                if(dfs(i, 1))   break;
                vis[i] = 0;
            }
            if(i != n + 1){
                for(i = 1; i < n; ++i){
                    cout << ans[i] << ' ';
                }
                cout << ans[n] << endl;
            } else{
                cout << "Impossible" << endl;
            }
        }
    
        return 0;
    }
  • 相关阅读:
    【转】以太坊分片:Overview and Finality
    Raiden Network — Ethereum 区块链支付通道
    ERC 和 EIP 代表什么呢?
    【转】什么是加密经济学
    Ethereum Probabilistic Micropayments
    【转】以太坊钱包分析与介绍
    【转】用Python从零开始创建区块链
    【转】用 Go 构建一个区块链
    通用权限管理系统组件 (GPM
    通用权限管理系统组件 (GPM
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4339691.html
Copyright © 2020-2023  润新知