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

  • 相关阅读:
    form表单提交中文乱码(前台中文到JAVA后台乱码)问题及解决
    JSON工具类库: alibaba/fastjson 使用记录
    前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 "
    AJAX与后台交互传参的两种方式
    小米平板充不上电解决(非硬件问题+系统升级原因)
    对于异常的理解
    solr如何进行搜索实战,关键字全库搜索
    solr5.5.4 添加mysql数据,实现同步更新
    solr5.5.4 tomcat8部署
    log4j通过配置文件配置即,即可完成系统报错想向指定邮箱发送提醒消息,网上的很多方法都又问题
  • 原文地址:https://www.cnblogs.com/bolderic/p/7465919.html
Copyright © 2011-2022 走看看