zoukankan      html  css  js  c++  java
  • wenbao与搜索

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    http://agc013.contest.atcoder.jp/tasks/agc013_b

    给n个点,m条边,找一条路径,要求与首尾相连的所有点必须在路径里面

    两次dfs搞定(仔细体会为什么?)

    很好的一个题

     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 const int maxn = 1e5+10;
     5 vector<int> v[maxn];
     6 bool vis[maxn];
     7 int a[maxn], num;
     8 void d(int x){
     9     for(int i = 0; i < v[x].size(); ++i){
    10         int xx = v[x][i];
    11         if(vis[xx]) continue;
    12         vis[xx] = true, a[++num] = xx;
    13         d(xx); return ;
    14     }
    15 }
    16 int main(){
    17     int n, m, x, y, num2;
    18     scanf("%d%d", &n, &m);
    19     for(int i = 0; i < m; ++i){
    20         scanf("%d%d", &x, &y);
    21         v[x].push_back(y), v[y].push_back(x);
    22     }
    23     vis[1] = true;
    24     a[num = 1] =  1;
    25     d(1),  num2 = num, d(1);
    26     printf("%d
    ", num);
    27     for(int i = num2; i >= 1; --i){
    28         if(i == num2) printf("%d", a[i]);
    29         else printf(" %d", a[i]);
    30     }
    31     for(int i = num2+1; i <= num; ++i){
    32         printf(" %d", a[i]);
    33     }
    34     printf("
    ");
    35     return 0;
    36 }

    -----------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------

    只有不断学习才能进步!

  • 相关阅读:
    指针数组与数组指针
    209. 长度最小的子数组
    面试题 05.08. 绘制直线(位运算)
    1160. 拼写单词
    88. 合并两个有序数组
    80. 删除排序数组中的重复项 II(On)
    python自定义异常和主动抛出异常
    类的构造方法1(类中的特殊方法)
    python之判断一个值是不是可以被调用
    主动调用其他类的成员(普通调用和super方法调用)
  • 原文地址:https://www.cnblogs.com/wenbao/p/6739182.html
Copyright © 2011-2022 走看看