• nyoj 题目20 吝啬的国度


    吝啬的国度

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来。现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,必须经过的前一个城市是几号城市(假设你不走重复的路)。
     
    输入
    第一行输入一个整数M表示测试数据共有M(1<=M<=5)组
    每组测试数据的第一行输入一个正整数N(1<=N<=100000)和一个正整数S(1<=S<=100000),N表示城市的总个数,S表示参观者所在城市的编号
    随后的N-1行,每行有两个正整数a,b(1<=a,b<=N),表示第a号城市和第b号城市之间有一条路连通。
    输出
    每组测试数据输N个正整数,其中,第i个数表示从S走到i号城市,必须要经过的上一个城市的编号。(其中i=S时,请输出-1)
    样例输入
    1
    10 1
    1 9
    1 8
    8 10
    10 3
    8 6
    1 2
    10 4
    9 5
    3 7
    
    样例输出
    -1 1 10 10 9 8 3 1 1 8

    此题应该用dfs或bfs
    dfs代码如下
     1 #include <cstdio>
     2 #include <vector>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 vector<int> path[100002];
     8 int pre[100002];
     9 
    10 int N, S;
    11 
    12 void dfs(int start) {
    13     int cnt = path[start].size();
    14     for(int i = 0; i < cnt;i++) {
    15         int p = path[start][i];
    16         if(pre[p] == 0) {
    17             pre[p] = start;
    18             dfs(p);
    19         }
    20         
    21     }
    22 }
    23 int main(int argc, char const *argv[])
    24 {
    25     int M;
    26     //freopen("input.txt","r",stdin);
    27     scanf("%d",&M);
    28     while(M--) {
    29         memset(path, 0, sizeof(path));
    30         memset(pre, 0, sizeof(pre));
    31         scanf("%d %d",&N,&S);
    32         for(int i = 0; i < N-1; i++) {
    33             int x,y;
    34             scanf("%d %d",&x,&y);
    35             path[x].push_back(y);
    36             path[y].push_back(x);
    37         }
    38         pre[S] = -1;
    39         dfs(S);
    40         printf("%d", pre[1]);
    41         for(int i = 2; i <= N; i++) {
    42             printf(" %d",pre[i]);
    43         }
    44         puts("");
    45     }
    46     return 0;
    47 }

    bfs代码如下

     1 #include <cstdio>
     2 #include <vector>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 
     7 vector<int> path[100002];
     8 int pre[100002];
     9 queue <int> que;
    10 int N, S;
    11 
    12 int main(int argc, char const *argv[])
    13 {
    14     int M;
    15     //freopen("input.txt","r",stdin);
    16     scanf("%d",&M);
    17     while(M--) {
    18         memset(path, 0, sizeof(path));
    19         memset(pre, 0, sizeof(pre));
    20         scanf("%d %d",&N,&S);
    21         for(int i = 0; i < N-1; i++) {
    22             int x,y;
    23             scanf("%d %d",&x,&y);
    24             path[x].push_back(y);
    25             path[y].push_back(x);
    26         }
    27         pre[S] = -1;
    28         que.push(S);
    29 
    30         while(!que.empty()) {
    31             int p0 = que.front();que.pop();
    32             int cnt = path[p0].size();
    33             for(int i = 0; i < cnt;i++) {
    34                 int p = path[p0][i];
    35                 if(pre[p] == 0) {
    36                     pre[p] = p0;
    37                     que.push(p);
    38                 }
    39             }
    40         }
    41         printf("%d", pre[1]);
    42         for(int i = 2; i <= N; i++) {
    43             printf(" %d",pre[i]);
    44         }
    45         puts("");
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    JAVAGUI设计步骤
    JAVA接口基础知识总结
    静态关键字static用法。
    JAVA面向对象的多态性
    java封装的概念
    多线程
    关于集合类间的区别
    JAVA——异常
    java——内部类
    Java——接口
  • 原文地址:https://www.cnblogs.com/jasonJie/p/6088409.html
Copyright © 2020-2023  润新知