zoukankan      html  css  js  c++  java
  • CS Academy Round 44 Check DFS

    题目链接https://csacademy.com/contest/round-44/task/check-dfs/

    题目大意:给出一个无向简单连通图,顶点从1~n标号,给出一个1开头的1~n的排列,问这个排列是否可以为该图dfs一次的访问顺序。

    解题思路:将给出的排列放入队列q中,pop第一个元素后dfs,对于每个节点,如果其孩子中有队首元素,那么标记该元素,队列pop,并以其为新节点搜索,搜索完毕后重复该过程;如果不包含但是孩子中有未访问的,则不可行;如果不包含但孩子没有未访问的,则返回。

    注意前向星表示中边数*2!!!

    代码:

     1 const int maxn = 1e5 + 5;
     2 struct Edge{
     3     int to, next;
     4 };
     5 Edge edges[maxn * 3];
     6 int tot, head[maxn * 3];
     7 queue<int> q;
     8 bool vis[maxn], flag = true;
     9 int n, m;
    10 
    11 void init(){
    12     tot = 0;
    13     flag = true;
    14     memset(vis, 0, sizeof(vis));
    15     memset(head, -1, sizeof(head));
    16 }
    17 void addEdge(int u, int v){
    18     edges[tot].to = v;
    19     edges[tot].next = head[u];
    20     head[u] = tot++;
    21 }
    22 
    23 void dfs(int x){
    24     if(!flag) return;
    25     vis[x] = 1;
    26     set<int> s;
    27     vector<int> vec;
    28     for(int i = head[x]; i != -1; i = edges[i].next){
    29         int v = edges[i].to;
    30         if(!vis[v]) {
    31             s.insert(v);
    32             vec.push_back(v);
    33         }
    34     }
    35     if(vec.size() == 0) return;
    36     while(s.size() > 0 && q.size() > 0){
    37         int tmp = q.front();
    38         set<int>::iterator it = s.find(tmp);
    39         if(it == s.end()){
    40             for(it = s.begin(); it != s.end(); it++){
    41                 if(!vis[*it]) {
    42                     flag = false;
    43                     return;
    44                 }
    45             }
    46             return;
    47         }
    48         s.erase(it);
    49         q.pop();
    50         dfs(tmp);
    51         if(!flag) return;
    52     }
    53 }
    54 void solve(){
    55     q.pop();
    56     dfs(1);
    57     if(q.size() > 0) flag = false;
    58     if(flag) puts("1");
    59     else puts("0");
    60 }
    61 int main(){
    62     init();
    63     scanf("%d %d", &n, &m);
    64     for(int i = 1; i <= n; i++) {
    65         int tmp;
    66         scanf("%d", &tmp);
    67         q.push(tmp);
    68     }
    69     for(int i = 1; i <= m; i++){
    70         int u, v;
    71         scanf("%d %d", &u, &v);
    72         addEdge(u, v);
    73         addEdge(v, u);
    74     }
    75     solve();
    76 }

    题目:

    Check DFS

    Time limit: 1000 ms
    Memory limit: 128 MB

     

    One of the most popular algorithms for traversing a graph is the Depth First Search (DFS).

    The DFS is usually implemented as a recursive function. First we call the function for the starting node. For each call, we go through the adjacency list of the current node, and recursively call the function for the unvisited neighbours.

    Notice that if we permute the adjacency lists of the nodes, the order in which we visit them might differ.

    In this problem you are given a graph with NN nodes and MM edges and a permutation PP of size NN. If you are allowed to shuffle the adjacency lists, is it possible to visit the nodes during a DFS starting in node 11 in the order given by PP?

    Standard input

    The first line contains two integers NN and MM.

    The second line contains a permutation of size NN. The first elements is always equal to 11.

    Each of the next MM lines contains two integer aa and bb representing two nodes that share an edge.

    Standard output

    If it's possible to visit the nodes in the given order print 11, otherwise print 00.

    Constraints and notes

    • 1 leq N, M leq 10^51N,M105​​ 
    • The graph is connected, simple and undirected
    InputOutputExplanation
    4 3
    1 4 2 3
    1 2
    1 3
    1 4
    
    1
    

    1234

    4 4
    1 2 4 3
    1 2
    2 3
    3 4
    2 4
    
    1
    

    1234

    4 3
    1 2 4 3
    1 2
    2 3
    3 4
    
    0
    

    1234

    4 6
    1 4 2 3
    1 2
    1 3
    1 4
    3 2
    4 2
    3 4
    
    1
    

    1234

  • 相关阅读:
    新能源汽车三大核心技术
    新能源汽车分类解读
    hadoop(四):配置参数
    HDP2.4安装(六):小结与回顾
    【Web】Nginx配置开机启动
    【Linux】ODBC安装
    【Erlang】源码安装
    【Linux】 Ncures库的介绍与安装
    【RabbitMQ】 RabbitMQ安装
    《MinDoc 接口文档在线管理系统》
  • 原文地址:https://www.cnblogs.com/bolderic/p/7465919.html
Copyright © 2011-2022 走看看