[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图

简介: DescriptionLittle Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible.

网址链接


Description


Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible. Very soon he realized that the best way to do it was to travel through each street of town only once. Naturally, he wanted to finish his trip at the same place he started, at his parents’ house.


The streets in Johnny’s town were named by integer numbers from 1 to n, n < 1995 n < 1995n<1995. The junctions were independently named by integer numbers from 1 to m, m < = 44 m <= 44m<=44. No junction connects more than 44 streets. All junctions in the town had different numbers. Each street was connecting exactly two junctions. No two streets in the town had the same number. He immediately started to plan his round trip. If there was more than one such round trip, he would have chosen the one which, when written down as a sequence of street numbers is lexicographically the smallest. But Johnny was not able to find even one such round trip.


Help Johnny and write a program which finds the desired shortest round trip. If the round trip does not exist the program should write a message. Assume that Johnny lives at the junction ending the street appears first in the input with smaller number. All streets in the town are two way. There exists a way from each street to another street in the town. The streets in the town are very narrow and there is no possibility to turn back the car once he is in the street


Input


Input file consists of several blocks. Each block describes one town. Each line in the block contains three integers x; y; z, where x > 0 and y > 0 are the numbers of junctions which are connected by the street number z. The end of the block is marked by the line containing x = y = 0. At the end of the input file there is an empty block, x = y = 0.


Output


Output one line of each block contains the sequence of street numbers (single members of the sequence are separated by space) describing Johnny’s round trip. If the round trip cannot be found the corresponding output block contains the message “Round trip does not exist.”


Sample Input


1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0


Sample Output


1 2 3 5 4 6 
Round trip does not exist.


题意:


给出m个点m<=44,n条边,n<1995的图,问是否有欧拉回路{

1. 有 则输出字典序最小的那条

2. 无 则输出"Round trip does not exist."

}


欧拉回路基本概念+判断+求解

总结


089ab08677424bf297f03addad8700fd.png


因为在输入的过程中,边的数量和点的数量都不是确定的,所以说我们必须要在输入的过程中记录点的数量以及边的数量,因为点的编号以及边的编号都是连续的,所以说我们可以直接记录点的编号的最大值以及边的编号最大值即可

假如统计出来的边的的最大编号为max_edge,那么说边的最大编号为max_edge,我们从1号点开始,从1 11向m a x _ e d g e max\_edgemax_edge便利这个点连向的所有边,如果当前点联想的边没有走过,就标记这条边,然后继续以这条边的另一端为起点继续深搜,中间过程中记录答案,因为每条边最只会被访问一次,所以说复杂度不会很高


ac_code:


int n;
int cnt,head[50];
int deg[50];
int gra[50][2007];
int u,v,id;
int start,max_edge;
bool vis[2007];
void _Init(){
  for(int i=1;i<=2000;i++) vis[i] = false,deg[i] = 0;
  memset(gra,0,sizeof gra);
}
vector<int> ans;
void dfs(int u){
  for(int i=1;i<=max_edge;i++){
    if(!vis[i] && gra[u][i]){
      vis[i] = 1;
      dfs(gra[u][i]);
      ans.push_back(i);
    }
  }
}
int main() {
  while(true){
    start = 0x3f3f3f3f,max_edge = 0, n = 0;
    ans.clear();
    _Init();
    u = read,v =read;
    if(u == 0 && v == 0) break;
    id = read;
    start = min(u, v);
    n = max(u, v);
    max_edge = max(max_edge,id);
    gra[u][id] = v;
    gra[v][id] = u;
    deg[u] ++;
    deg[v] ++;
    while(true){
      u = read,v = read;
      if(!(u + v)) break;
      id = read;
      start = min(start,min(u,v));
      n = max(n,max(u, v));
      max_edge = max(max_edge, id);
      gra[u][id] = v;
      gra[v][id] = u;
      deg[u] ++;
      deg[v] ++;
    }
    int flag = 0;
//    debug(n);
//    debug(max_edge);
//    debug(start);
    for(int i=1;i<=n;i++){
      if(deg[i] % 2) {
        puts("Round trip does not exist.");
        flag = 1;
        break;
      }
    }
    if(flag) continue;
    assert(start == 1);
    dfs(start);
    for(int i=1;i<=max_edge;i++){
      printf("%d%c",ans.back(),(i!=max_edge?' ':'\n'));
      ans.pop_back();
    }
  }
  return 0;
}
/**
1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0
**/




目录
相关文章
|
算法 测试技术 C++
C++二分算法习题:判断是否是完全平方数[容易]和排列箱子[容易]
C++二分算法习题:判断是否是完全平方数[容易]和排列箱子[容易]
|
6月前
|
人工智能 BI
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
52 0
|
6月前
|
算法
|
3月前
|
算法 Java
LeetCode初级算法题:子数组最大平均数+二叉树的最小深度+最长连续递增序列+柠檬水找零
LeetCode初级算法题:子数组最大平均数+二叉树的最小深度+最长连续递增序列+柠檬水找零
40 0
|
6月前
|
算法 测试技术 C#
【数位dp】【数论】【动态规划】2999. 统计强大整数的数目
【数位dp】【数论】【动态规划】2999. 统计强大整数的数目
|
6月前
【每日一题Day122】LC1237找出给定方程的正整数解 | 双指针 二分查找
【每日一题Day122】LC1237找出给定方程的正整数解 | 双指针 二分查找
40 0
|
算法 C++
剑指offer(C++)-JZ11:旋转数组的最小数字(算法-搜索算法)
剑指offer(C++)-JZ11:旋转数组的最小数字(算法-搜索算法)
|
算法 C++
每日算法系列【LeetCode 329】矩阵中的最长递增路径
每日算法系列【LeetCode 329】矩阵中的最长递增路径
|
Go vr&ar
【每日一题Day17】LC754到达终点数字|数学 等差数列
在一根无限长的数轴上,你站在0的位置。终点在target的位置。
111 0
【每日一题Day17】LC754到达终点数字|数学 等差数列