ZOJ 3332 Strange Country II

Source: Internet
Author: User

Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3757


Surface:

Strange Country II Time limit: 1 Second Memory Limit: 32768 KB Special Judge

You want to visit a strange country. There is N cities in the country. Cities is numbered from 1 to N. The unique country is taking planes. Strangely, in this strange country, for every, cities a and B, there are a flight from a to C5>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 the city exactly once. Is it possible?

Input

There is multiple test cases. The first line of input was an integer T (0 < T <=) indicating the number of test cases. Then T test Cases follow. Each test case starts with a line containing an integer n (0 < n <=), which is the number of C Ities. Each of the next N * (n -1)/2 lines contains 2 numbers a, B (0 < a, b <= n, A ! = B), meaning that there are 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. Separate the city numbers by spaces. If there is 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

Solving:

A small amount of data, direct DFS can be, plus a flag flag in time to return. Because accidentally ignored each one only access once this condition, WA once.


Code:

#include <iostream> #include <cstdio> #include <cstring> using namespace Std;bool map[105][105];bool Vis[105],flag;int n,t,x,a,b;int road[105],cnt=0;void dfs (int x) {//Timely return if (flag) Return;road[cnt++]=x;vis[x]=1;bool sign=true;//Check if find for (int i=1;i<=n;i++) {if (vis[i]==0) {sign=false;break;}} Already found if (sign) {Flag=true;return;} else{for (int i=1;i<=n;i++) {//Note that the point should not be accessed if (Map[x][i]&&!vis[i]) {  DFS (i);  Pay attention to restore   vis[i]=0;  cnt--;}}}} int main () {scanf ("%d", &t), while (t--) {Flag=false;memset (map,0,sizeof (map)); scanf ("%d", &n); x=n* (n-1)/2;// Read-in operation for (int i=0;i<x;i++) {  scanf ("%d%d", &a,&b);  


ZOJ 3332 Strange Country II

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.