• hdu 1269 迷宫城堡 (tarjan)


    迷宫城堡
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 21627    Accepted Submission(s): 9417

    Problem Description
    为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间。Gardon需要请你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间i。
     
    Input
    输入包含多组数据,输入的第一行有两个数:N和M,接下来的M行每行有两个数a和b,表示了一条通道可以从A房间来到B房间。文件最后以两个0结束。
     
    Output
    对于输入的每组数据,如果任意两个房间都是相互连接的,输出"Yes",否则输出"No"。
     
    Sample Input
    3 3
    1 2
    2 3
    3 1
    3 3
    1 2
    2 3
    3 2
    0 0
     
    Sample Output
    Yes
    No

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <vector>
     5 #include <string>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <climits>
     9 #include <iostream>
    10 #include <algorithm>
    11 #define INF 0x3f3f3f3f
    12 using namespace std;
    13 const int my_max = 10010;
    14 
    15 int n, m, a, b, scc_cnt, my_dfn[my_max], my_low[my_max], my_belong[my_max],
    16     my_stack[my_max], my_top, my_index, is_stack[my_max];
    17 vector <int> my_map[my_max];
    18 
    19 void my_tarjan(int u)
    20 {
    21     my_stack[my_top ++] = u;
    22     my_dfn[u] = my_low[u] = my_index ++;
    23     is_stack[u] = 1;
    24     for (int i = 0; i < my_map[u].size(); ++ i)
    25     {
    26         int temp = my_map[u][i];
    27         if (!my_dfn[temp])
    28         {
    29             my_tarjan(temp);
    30             my_low[u] = min(my_low[u], my_low[temp]);
    31         }
    32         else if (is_stack[temp])
    33             my_low[u] = min(my_low[u], my_dfn[temp]);
    34     }
    35     if (my_dfn[u] == my_low[u])
    36     {
    37         scc_cnt ++;
    38         int temp;
    39         do{
    40             temp = my_stack[-- my_top];
    41             is_stack[temp] = 0;
    42             my_belong[temp] = scc_cnt;
    43         } while (temp != u);
    44     }
    45 }
    46 
    47 int main()
    48 {
    49     while (scanf("%d%d", &n, &m), n || m)
    50     {
    51         for (int i = 1; i <= n; ++ i) my_map[i].clear();
    52         scc_cnt = my_top = my_index = 0;
    53         for (int i = 1; i <= n; ++ i)
    54             my_dfn[i] = my_low[i] = is_stack[i] = 0;
    55         while (m --)
    56         {
    57             scanf("%d%d", &a, &b);
    58             my_map[a].push_back(b);
    59         }
    60 
    61         for (int i = 1; i <= n; ++ i)
    62             if (!my_dfn[i])
    63                 my_tarjan(i);
    64         printf("%s
    ", scc_cnt == 1 ? "Yes" : "No");
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    面试题58:翻转字符串
    面试题57_2:和为S的连续正数序列
    面试题57:和为S的数字
    面试题56:数组中数字出现的次数
    面试题55_2:平衡二叉树
    面试题54:二叉搜索树的第k大节点
    面试题55_1:二叉树的深度
    tortoisegit 本地创建的分支 push github
    TortoiseGit 拉取分支
    tortoiseGIT 本地分支创建合并
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9496207.html
Copyright © 2020-2023  润新知