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;
}
时间: 2024-11-08 19:23:56

Strange Country II 暴力dfs的相关文章

Strange Country II ( 简单的dfs,但是要注意细节)

Strange Country II 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, th

zoj 3332 Strange Country II

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3332 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 trav

ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)

链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 题意: 给你一个N,代表含有N个点的竞赛图,接着的N * (N- 1) / 2行两个点u, v,代表存在有向边<u,v>,问是否能构造出来一个哈密顿通路. 思路: 竞赛图一定含有哈密顿通路,不一定含有哈密顿回路.则不可能出现不存在的情况,直接构造就可以,至于方法可看我的另外一篇文章:http://www.cnblogs.com/Ash-ly/p/5452580.

A. The Fault in Our Cubes 暴力dfs

http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行,就会早早退出. 这样写起来比较好写. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

Codeforces Round #359 (Div. 2) C. Robbers&#39; watch (暴力DFS)

题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b < m 且a的7进制和n-1的7进制位数相同 且b的7进制和m-1的7进制位数相同,还有a和b的7进制上的每位上的数各不相同. 看懂题目,就很简单了,先判断a和b的7进制位数是否超过7,不超过的话就dfs暴力枚举计算就可以了. 1 //#pragma comment(linker, "/STACK

hihoCoder 1185 连通性&#183;三(Tarjan缩点+暴力DFS)

#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出去,就拜托小Hi和小Ho忙帮放牧. 约翰家一共有N个草场,每个草场有容量为W[i]的牧草,N个草场之间有M条单向的路径. 小Hi和小Ho需要将牛羊群赶到草场上,当他们吃完一个草场牧草后,继续前往其他草场.当没有可以到达的草场或是能够到达的草场都已经被吃光了之后,小hi和小Ho就把牛羊群赶回家. 一开

Zoj3332-Strange Country II(有向竞赛图)

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 fro

ZOJ Seven-Segment Display 暴力dfs + 剪枝

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on     1 = off A seven segment code of permutation p is a set of seven segment code derived from the standard code by rearranging the bits into the order indicated by p. For exampl