首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一道ACM的题,已知数据测试都对,但下传都是异常,求解

2013-03-12 
一道ACM的题,已知数据测试都对,但上传都是错误,求解希望有人能帮我把我没想到的可能举例一下,或者告诉我代

一道ACM的题,已知数据测试都对,但上传都是错误,求解
希望有人能帮我把我没想到的可能举例一下,或者告诉我代码怎么样改进,谢谢了!



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:

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

3
1
2
1 2
3
1 2
1 3
2 3
Sample Output

1
1 2
1 2 3





#include<stdio.h>
#include<string.h>
int path[110][110];
int a[110],vid[110],p;
void s(int n,int v)
{
     int i,j;
     if (p) return ;
     if(v==n) 
     {
          for(i=0;i<n;i++)
          {
                 printf("%d ",a[i]+1);
          }
          printf("\n");
          p=1; 
     }
     else
     {
           for(i=0;i<n;i++)
           {
    if(path[a[v-1]][i] && !vid[i])
{
vid[i]=1;
a[v]=i;
s(n,v+1);
vid[i]=0;
}   
           


           }
     
     }
     return ;

}
int main()
{
       int n,k,m,x,y,i;
       
       
       scanf("%d",&n);
       
       while(n--)
       {
       memset(vid,0,sizeof(vid));
       memset(path,0,sizeof(path));
             scanf("%d",&m);
             k=m*(m-1)/2;
 p=0;
             while(k--)
             {
              
                   scanf("%d%d",&x,&y);
                   path[x-1][y-1]=1;

             }
for(i=0;i<m;i++)
{
a[0]=i;
vid[i]=1;
s(m,1);
vid[i]=0;
if(p) break;
}

  if(!p) printf("Impossible\n");
       }
       return 0;

}


[解决办法]
首先tournament graph是保证存在hamilton path的。原证明用的归纳法直接就能给出平方的算法,够这题用了。你这个dfs最后肯定会tle的。

至于你现在的代码,我能看出来的问题就只有行末多余的空格

热点排行