SDKD TeamContest A-K - 1011

来源:互联网 发布:linux如何关闭echo 编辑:程序博客网 时间:2024/06/05 19:57

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 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

3121 231 21 32 3

Sample Output

11 21 2 3



#include <stdio.h>#include <string>#include <stdlib.h>#include <iostream>using namespace std;int map[105][105];int num[105] ;bool vis[105];int flag ;int t, n;void dfs(int v) {num[t ++] = v;if(t == n) {flag = 1; return;}for(int u = 1; u <= n; u ++) {if(!vis[u] && map[v][u] == 1) {vis[u] = true; dfs(u);if(flag == 1) return;t --;vis[u] = false;}}}int main() {int cas;int a, b, i;scanf("%d", &cas);while(cas --) {scanf("%d", &n);for (i = 0; i < 105; i ++)for (int j = 0; j < 105; j ++)map[i][j] = 0;for (int k = 0; k < 105; k ++)vis[k] = 0;for(i = 0; i < n * (n - 1) / 2; i ++) {scanf("%d %d", &a, &b) ;map[a][b] = 1;}flag = 0;for(i = 1; i <= n; i ++) {vis[i] = true;t = 0;dfs(i);if(flag) break;vis[i] = false;}if(flag) {for(i = 0; i < n; i ++) {if(i != n - 1)printf("%d ", num[i]);elseprintf("%d\n", num[i]);}}else  printf("Impossible\n");}return 0;}


0 0
原创粉丝点击