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 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:
Sample Input
3 1 2 1 2 3 1 2 1 3 2 3
Sample Output
1 1 2 1 2 3Author: CAO, Peng
题意:很简单,从一个点开始走,问能不能走完所有的点,而且只走一次,如果可以请输出任意的一条路径
题解:题目给出n*(n-1)/2条边,那么意味着每个点之间都有路径,因为图是有向图,那么不一定是连通图,这里就需要找起点了,这里我们发现只有100个点,那么直接暴力就好了,枚举起点,DFS在图上跑,如果能跑到深度为n,那么就找到输出就可以啦
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<string> #include<bitset> #include<utility> #include<functional> #include<iomanip> #include<sstream> #include<ctime> using namespace std; #define N int(1e3) #define inf int(0x3f3f3f3f) #define mod int(1e9+7) typedef long long LL; vector<vector<int> >g; vector<int>ans; int vis[N], ok; void dfs(int x, int dep, int n) { if (dep == n - 1) { ans.push_back(x); ok = 1; return; } vis[x] = 1; for (int i = 0; i < g[x].size(); i++) { if (!vis[g[x][i]]) { dfs(g[x][i], dep + 1, n); if (ok) { ans.push_back(x); return; } } } vis[x] = 0; } int main() { #ifdef CDZSC freopen("i.txt", "r", stdin); //freopen("o.txt","w",stdout); int _time_jc = clock(); #endif int t, n, x, y; scanf("%d", &t); while (t--) { ans.clear(); memset(vis, 0, sizeof(vis)); scanf("%d", &n); g.clear(); g.resize(n + 10); for (int i = 0; i < (n*(n - 1)) / 2; i++) { scanf("%d%d", &x, &y); g[x].push_back(y); } int st = 1; for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); ok = 0; dfs(i, 0, n); if (ok)break; } if (!ok) { puts("Impossible"); } else { for (int i = ans.size() - 1; i >= 0; i--) printf("%d%c", ans[i], i == 0 ? '\n' : ' '); } } return 0; }
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 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:
Sample Input
3 1 2 1 2 3 1 2 1 3 2 3
Sample Output
1 1 2 1 2 3Author: CAO, Peng