poj 2337 && zoj 1919 欧拉回路+连通性判断

 题目要求按字典序排列,而且可能有重边

所以一开始就将数组从大到小排列,那么我将字符串加入链表时就会令小的不断前移,大的被挤到后面

这里有一点问题就是我一开始使用的是qsort:

int cmp(const void *s1 , const void *s2)
{
    return strcmp((char*)s1 , (char*)s2)<0;
}

qsort(str , n , sizeof(str[0]) , cmp)

poj一直wa,试了发zoj却过了,可能是编译器原因吧,然后将字符串放入了结构体重新进行排序,poj才给过

寻找欧拉回路的关键代码

void dfs(int u , int id)
{
    for(int i=first[u] ; i!=-1 ; i=e[i].next){
        if(e[i].flag){
            e[i].flag=false;
            dfs(e[i].y , e[i].id);
            rec[top2++]=e[i].id;
        }
    }
}

将其逆序输出即可

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <algorithm>
  5 using namespace std;
  6 #define N 1010
  7 int n , in[N] , out[N] , cnt , st , la;
  8 int first[N] , k;
  9 int top1 , top2;
 10 int rec[N];
 11 
 12 struct Str{
 13     char str[30];
 14 }str[N];
 15 
 16 bool cmp(Str s1 , Str s2)
 17 {
 18     return strcmp(s1.str , s2.str)>0;
 19 }
 20 
 21 struct Edge{
 22     int y , next , id;
 23     bool flag;
 24 }e[N*2];
 25 
 26 Edge stack[N];
 27 
 28 void add_edge(int x,int y ,int id)
 29 {
 30     e[k].y=y , e[k].id = id , e[k].flag=true , e[k].next=first[x];
 31     first[x]=k++;
 32 }
 33 
 34 void dfs(int u , int id)
 35 {
 36     for(int i=first[u] ; i!=-1 ; i=e[i].next){
 37         if(e[i].flag){
 38             e[i].flag=false;
 39             dfs(e[i].y , e[i].id);
 40             rec[top2++]=e[i].id;
 41         }
 42     }
 43 }
 44 
 45 void Fleury()
 46 {
 47     top1 = top2 = 0;
 48     dfs(st , -1);
 49 }
 50 
 51 int main()
 52 {
 53   //  freopen("a.in" , "r" , stdin);
 54     int T;
 55     scanf("%d" , &T);
 56     while(T--)
 57     {
 58         scanf("%d" , &n);
 59         for(int i=0 ; i<n ; i++) scanf("%s" , str[i].str);
 60         sort(str , str+n , cmp);
 61      //   for(int i=0 ; i<n ; i++) printf("%s
" , str[i]);
 62         memset(first , -1 , sizeof(first));
 63         memset(in , 0 , sizeof(in));
 64         memset(out , 0 , sizeof(out));
 65         k=0;
 66         for(int i=0 ; i<n ; i++){
 67             int len = strlen(str[i].str);
 68             int a = str[i].str[0]-'a' , b = str[i].str[len-1]-'a';
 69             add_edge(a , b , i);
 70             in[b]++ , out[a]++;
 71         }
 72         cnt=0,st=-1 , la=-1;
 73 
 74         bool flag=true;
 75         for(int i=0;i<26;i++){
 76             if(abs(out[i]-in[i])>1){
 77                 flag=false;
 78                 break;
 79             }
 80             if(out[i]!=in[i]){
 81                 cnt++;
 82                 if(st==-1 && out[i]==in[i]+1) st=i;
 83                 else if(la==-1 && in[i]==out[i]+1) la=i;
 84                 else{
 85                     flag=false;
 86                     break;
 87                 }
 88             }
 89         }
 90         if(!flag || (cnt!=0 && cnt!=2)){printf("***
");continue;}
 91 
 92 
 93 
 94         if(st<0){
 95             for(int i=0 ; i<26 ; i++)
 96                 if(out[i]){
 97                     st = i;
 98                     break;
 99                 }
100         }
101         Fleury();
102            // cout<<top2<<endl;
103         if(top2<n){printf("***
");continue;}
104         for(int i=top2-1 ; i>0 ; i--) printf("%s." , str[rec[i]].str);
105         printf("%s
" , str[rec[0]].str);
106     }
107     return 0;
108 }
原文地址:https://www.cnblogs.com/CSU3901130321/p/4468298.html