URAL 1137Bus Routes (dfs)

Z - Bus Routes

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice URAL 1137

Description

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 Input

input output
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

Notes

Here is a picture for the example:

题意就是给你几条旧的路线,让你求能否设计一条新的路线,可以包含这里面的所有路线,dfs变换一下就可以了,但是注意一点就是再回塑的时候不要把标记vis即系清空

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define N 10010
int n,m;
vector<int>ed[N];
bool vis[N][N];
int pa[100010],t;
void dfs(int u){
     int i;
    for(i = 0 ;i < (int)ed[u].size() ; i++){
        int v = ed[u][i];
        if(!vis[u][v]){
            vis[u][v] = 1;
            dfs(v);
        }
    }
    t++;
    pa[t] = u;
    return ;
}
int main(){
    int i,j,u,v;
    scanf("%d",&n);
    int sum=0;
    for(i = 1; i <= n ; i++){
        scanf("%d",&m);
        sum+=m;
        scanf("%d",&u);
        for(j = 1 ; j <= m ; j++)
        {
            scanf("%d",&v);
            ed[u].push_back(v);
            u = v;
        }
    }
    dfs(1);
    if(sum!=t-1)
        printf("0\n");
    else{
    printf("%d\n",t-1);
    for(i = t; i > 1 ; i--)
    printf("%d ",pa[i]);
    printf("%d\n",pa[1]);
    }
    return 0;
}
时间: 2024-10-12 08:15:01

URAL 1137Bus Routes (dfs)的相关文章

URAL 1242 Werewolf(DFS)

Werewolf Time limit: 1.0 secondMemory limit: 64 MB Knife. Moonlit night. Rotten stump with a short black-handled knife in it. Those who know will understand. Disaster in the village. Werewolf. There are no so many residents in the village. Many of th

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in

11218 - KTV(dfs)

问题 C: Repeat Number 时间限制: 1 Sec  内存限制: 128 MB 提交: 23  解决: 7 [提交][状态][论坛] 题目描述 Definition: a+b = c, if all the digits of c are same ( c is more than ten),then we call a and b are Repeat Number. My question is How many Repeat Numbers in [x,y]. 输入 There

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

poj3187Backward Digit Sums(DFS)

题目链接: huangjing 思路: 这个题目想到dfs很容易,但是纠结在这么像杨辉三角一样计算那些值,这个我看的队友的,简直厉害,用递归计算出杨辉三角顶端的值....具体实现详见代码... 题目: Language: Default Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4285   Accepted: 2474 Description FJ and his cows