这题点的个数(<=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