zoukankan      html  css  js  c++  java
  • 1137. Bus Routes

    1137. Bus Routes

    Time limit: 1.0 second Memory limit: 64 MB
    Several bus routes were in the city of Fishburg. None of the routes shared the same section of road, though common stops and intersections were possible. Fishburg old residents stated that it was possible to move from any stop to any other stop (probably making several transfers). The new mayor of the city decided to reform the city transportation system. He offered that there would be only one route going through all the sections where buses moved in the past. The direction of movement along the sections must be the same and no additional sections should be used.
    Write a program, which creates one of the possible new routes or finds out that it is impossible.

    Input

    The first line of the input contains the number of old routes n. Each of the following n lines contains the description of one route: the number of stops m and the list of that stops. Bus stops are identified by positive integers not exceeding 10000. A route is represented as a sequence of m + 1 bus stop identifiers: l1, l2, …,lm, lm+1 = l1 that are sequentially visited by a bus moving along this route. A route may be self-intersected. A route always ends at the same stop where it starts (all the routes are circular).
    The number of old routes: 1 ≤ n ≤ 100. The number of stops: 1 ≤ m ≤ 1000. The number-identifier of the stop: 1 ≤ l ≤ 10000.

    Output

    The output contains the number of stops in the new route k and the new route itself in the same format as in the input. The last (k+1)-th stop must be the same as the first. If it is impossible to make a new route according to the problem statement then write 0 (zero) to the output.

    Sample

    inputoutput
    3
    6 1 2 5 7 5 2 1
    4 1 4 7 4 1
    5 2 3 6 5 4 2
    
    15 2 5 4 2 3 6 5 7 4 1 2 1 4 7 5 2
    

    Hint

    Here is a picture for the example:
     
    Problem illustration
    Problem Source: Quarterfinal, Central region of Russia, Rybinsk, October 17-18 2001 
    ***************************************************************************************
    深搜,存储结构很重要。用vector存储有利于清空。
    ***************************************************************************************
     1 #include<iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<vector>
     7 using namespace std;
     8 vector<int>adj[10003];
     9 vector<int>vis[10003];
    10 vector<int>ans;
    11 int a[10003];
    12 int n,m,i,j,st;
    13 void  dfs(int x,int k)//深搜
    14   {
    15      for(int i=0;i<adj[x].size();i++)
    16        if(!vis[x][i])
    17         {
    18             ans.insert(ans.begin()+k,adj[x][i]);//压入值
    19             vis[x][i]=true;
    20             dfs(adj[x][i],k+1);
    21         }
    22   }
    23   int main()
    24   {
    25       cin>>n;
    26       int num=0;
    27       int gs=-1;
    28       while(n--)
    29        {
    30            cin>>m;
    31            num+=m;
    32            for(i=0;i<=m;i++)
    33             {
    34                 cin>>a[i];
    35                 if(gs==-1)
    36                   gs=a[i];
    37                 if(i)
    38                  {
    39                      adj[a[i-1]].push_back(a[i]);//标记相邻的点
    40                      vis[a[i-1]].push_back(false);//初始化
    41                  }
    42             }
    43        }
    44      ans.clear();//清空值
    45      ans.push_back(gs);
    46      dfs(gs,1);
    47      if(num!=ans.size()-1)//欧拉回路不存在
    48      {
    49          cout<<'0'<<endl;
    50          return 0;
    51      }
    52      cout<<num<<' ';
    53      for(i=0;i<ans.size();i++)//输出深搜值
    54       cout<<ans[i]<<' ';
    55      cout<<endl;
    56      return 0;
    57   }
    View Code
  • 相关阅读:
    Android见招拆招十:Migrate Android Code
    Android转载三:(布局)ImageView中src与background的区别
    Android见招拆招九:字符编码问题导入项目报错
    Android见招拆招八:多次遇到的R.java编译问题
    Android学习笔记五:(布局)Layout_margin和Layout_padding的区别
    Android见招拆招七:Error parsing XML: no element
    Window10系统修改hosts文件的方法
    Foxmail:‘错误信息:由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败’的解决办法
    Oracle 查询NULL字段/空字符串
    Python 安装第三方模块时 报Retrying(Retry(total=4, connect=None, read=None, redirect=None, status=None))...[WinError 10061]由于目标计算机积极拒绝,无法连接 错误
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3254690.html
Copyright © 2011-2022 走看看