zoukankan      html  css  js  c++  java
  • 蓝桥---危险系数(关键点+路径)

    Description

    抗日战争时期,冀中平原的地道战曾发挥重要作用。
    地道的多个站点间有通道连接,形成了庞大的网络。但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系。
    我们来定义一个危险系数DF(x,y):
    对于两个站点x和y (x != y), 如果能找到一个站点z,当z被敌人破坏后,x和y不连通,那么我们称z为关于x,y的关键点。相应的,对于任意一对站点x和y,危险系数DF(x,y)就表示为这两点之间的关键点个数。

    本题的任务是:已知网络结构,求两站点之间的危险系数。

    Input

    输入数据第一行包含2个整数n(2 <= n <= 1000), m(0 <= m <= 2000),分别代表站点数,通道数;
    接下来m行,每行两个整数 u,v (1 <= u, v <= n; u != v)代表一条通道;
    最后1行,两个数u,v,代表询问两点之间的危险系数DF(u, v)。

    Output

    一个整数,如果询问的两点不连通则输出-1.

    Sample Input

    7 6
    1 3
    2 3
    3 4
    3 5
    4 5
    5 6
    1 6

    Sample Output

    2

    给了起点st终点ed ,我们从起点往终点搜, 得到cnt条路径, 要从st->ed ,只有这cnt条路径。

    如果这cnt条路径都有点a , 也就是从st->ed,都要通过点a ,那么显然删除点a,就不能st->ed ,则点a是割点 。

    同时我们用vis[]来记录路径 。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const double eps =1e-8;
    16 const int mod=1e9+7;
    17 const int maxn=1e6+10;
    18 using namespace std;
    19 
    20 vector<int> vt[1005];
    21 int num[1005];//记录路径中每个点经过次数 
    22 int vis[1005];//记录路径 
    23 int cnt;//路径数 
    24 int n,m,st,ed;
    25 
    26 void DFS(int u)
    27 {
    28     if(u==ed)//到达终点
    29     {
    30         cnt++;
    31         for(int i=1;i<=n;i++)
    32         {
    33             if(vis[i]) num[i]++; 
    34         }
    35         return;
    36     }
    37     for(int i=0;i<vt[u].size();i++)
    38     {
    39         int v=vt[u][i];
    40         if(vis[v]==0)
    41         {
    42             vis[v]=1;
    43             DFS(v);
    44             vis[v]=0;
    45         }
    46     }
    47     return ;
    48 }
    49 
    50 int main()
    51 {
    52     #ifdef DEBUG
    53     freopen("sample.txt","r",stdin);
    54     #endif
    55     
    56     scanf("%d %d",&n,&m);
    57     for(int i=1;i<=m;i++)
    58     {
    59         int u,v;
    60         scanf("%d %d",&u,&v);
    61         vt[u].push_back(v);
    62         vt[v].push_back(u);
    63     }
    64     scanf("%d %d",&st,&ed);
    65     vis[st]=1;
    66     DFS(st); 
    67     int ans=0;
    68     for(int i=1;i<=n;i++)
    69     {
    70         if(num[i]==cnt) ans++;
    71     }
    72     if(cnt>0) printf("%d
    ",ans-2);//起点和终点不算割点,减去 
    73     else printf("-1
    ");
    74     
    75     return 0;
    76 }

    -

  • 相关阅读:
    LeetCode 189. Rotate Array
    LeetCode 965. Univalued Binary Tree
    LeetCode 111. Minimum Depth of Binary Tree
    LeetCode 104. Maximum Depth of Binary Tree
    Windows下MySQL的安装与配置
    LeetCode 58. Length of Last Word
    LeetCode 41. First Missing Positive
    LeetCode 283. Move Zeroes
    《蚂蚁金服11.11:支付宝和蚂蚁花呗的技术架构及实践》读后感
    删除docker下的镜像
  • 原文地址:https://www.cnblogs.com/jiamian/p/12319134.html
Copyright © 2011-2022 走看看