Zoj 3332 strange country II

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3332.

Description

You want to visit a strange country. There areNCities in the country. Cities are numbered from 1N. The unique way to travel in the country is taking planes. Strangely, in this strange country, for every two citiesAAndB, There is a flight fromAToBOr fromBToA, 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 integerT(0 <T<= 100) indicating the number of test cases. ThenTTest Cases Follow. Each test case starts with a line containing an integerN(0 <N<= 100), which is the number of cities. Each of the nextN*(N-1)/2 lines contains 2 numbersA,B(0 <A,B<=N,A! =B), Meaning that there is a flight from CityATo CityB.

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


Let's talk about the meaning of the question first:


When a person arrives in a strange city, the characteristic of this city is that every two cities have planes arriving directly, but not necessarily two-way, that is, a-> B! = B->,


The number of cities N and N * (N-1)/The number of two routes from city X to city y (indicating that there are planes between city X and city y) are given ), can you pass through every city and only go through once?


If yes, output the passing city at a time; otherwise, output impossible.




Analysis:Obviously, first create an adjacent matrix map [a] [B], which indicates the route between A and B. If (a-> B ), map [a] [B] = 1; else map [a] [B] = 0;


Then, how can we use num [N] to record the cities that have passed? Put it in DFS, indicating that there is a path between them, so we can record it.


It is critical to use VIS [N] to query whether access has been made.


T is used to determine whether all the cities have been to the same city. t is used for subtraction ..

For details, see the code: # include <cstdio> # include <cstring> int map [105] [105]; int num [105]; bool vis [105]; int flag; int t, n; void DFS (INT v) {num [t ++] = V; // record node if (t = N) // found, exit {flag = 1; return;} For (INT u = 1; U <= N; U ++) {If (! Vis [u] & map [v] [u] = 1) // unmarked, with paths {vis [u] = true; DFS (U ); // continue to search for if (flag = 1) // exit return if it is found; t --; // note that if it is not found, subtract 1 vis [u] = false; // trace back to the original status }}int main () {int T, A, B, I; scanf ("% d", & T); While (t --) {scanf ("% d", & N); memset (MAP, 0, sizeof (MAP); memset (VIS, 0, sizeof (VIS )); for (I = 0; I <n * (N-1)/2; I ++) {scanf ("% d", & A, & B ); map [a] [B] = 1;} flag = 0; for (I = 1; I <= N; I ++) {vis [I] = true; T = 0; // The initial value is zero DFS (I); If (FLAG) // exit break if it is found; vis [I] = false; // trace back to the original status} If (FLAG) // If {for (I = 0; I <n; I ++) {if (I = 0) exists) printf ("% d", num [I]); elseprintf ("% d", num [I]);} printf ("\ n ");} else printf ("impossible \ n");} return 0 ;} /* 551 33 52 52 41 31 31 31 31 31 361 21 61 45 65 43 21 21 21 21 21 21 21 21 2 */



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.