• 双联通分量复习


    1,边双联通(桥)
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 17989   Accepted: 7470

    Description

    In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another. 

    Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way. 

    There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

    Input

    Line 1: Two space-separated integers: F and R 

    Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

    Output

    Line 1: A single integer that is the number of new paths that must be built.

    Sample Input

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

    Sample Output

    2

    Hint

    Explanation of the sample: 

    One visualization of the paths is: 
       1   2   3
    +---+---+
    | |
    | |
    6 +---+---+ 4
    / 5
    /
    /
    7 +
    Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 
       1   2   3
    +---+---+
    : | |
    : | |
    6 +---+---+ 4
    / 5 :
    / :
    / :
    7 + - - - -
    Check some of the routes: 
    1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
    1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
    3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7 
    Every pair of fields is, in fact, connected by two routes. 

    It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.
     
      1 /**
      2 在树中至少添加多少条边能使图变为双连通图。
      3 结论:添加边数=(树中度为1的节点数+1)/2
      4 
      5 具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,
      6 这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的
      7 环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样
      8 一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。
      9 
     10 */
     11 #include <cstdio>
     12 #include <iostream>
     13 #include <cstring>
     14 #include <algorithm>
     15 #include <cmath>
     16 #include <stack>
     17 
     18 using namespace std;
     19 const int MAXN = 5e3 + 7;
     20 const int MAXM = 1e4 + 7;
     21 
     22 struct Edge {
     23     int to, w, next;
     24     int cut;
     25 } edge[MAXM * 2];
     26 
     27 int first[MAXN], dfn[MAXN], low[MAXN], belong[MAXN], degree[MAXN], ins[MAXM];
     28 
     29 stack<int>st;
     30 
     31 int bridge; ///桥的数目
     32 
     33 int sign, n, m;
     34 
     35 int indx, scc;
     36 
     37 inline void init() {
     38     memset(first, -1, sizeof(first));
     39     sign = 0;
     40 }
     41 
     42 inline void add_edge(int u, int v, int w) {
     43     edge[sign].to = v;
     44     edge[sign].w = w;
     45     edge[sign].cut = 0;
     46     edge[sign].next = first[u];
     47     first[u] = sign++;
     48 }
     49 
     50 void tarjan(int now, int pre) {
     51     dfn[now] = low[now] = ++indx;
     52     st.push(now);
     53     ins[now] = 1;
     54     for(int i = first[now]; ~i; i = edge[i].next) {
     55         int to = edge[i].to;
     56         if(to == pre) {
     57             continue;
     58         }
     59         if(!dfn[to]) {
     60             tarjan(to, now);
     61             low[now] = min(low[now], low[to]);
     62             if(low[to] > dfn[now]) {
     63                 bridge++;
     64                 edge[i].cut = 1;
     65                 edge[i^1].cut = 1;
     66             }
     67         } else if(ins[to]) {
     68             low[now] = min(low[now], dfn[to]);
     69         }
     70     }
     71     if(dfn[now] == low[now]) {
     72         scc++;
     73         int top;
     74         do {
     75             top = st.top();
     76             st.pop();
     77             ins[top] = 0;
     78             belong[top] = scc;
     79         } while(top != now);
     80     }
     81 }
     82 
     83 int main()
     84 {
     85     int u, v;
     86     while(~scanf("%d %d", &n, &m)) {
     87         init();
     88         memset(dfn, 0, sizeof(dfn));
     89         memset(ins, 0, sizeof(ins));
     90         memset(belong, 0, sizeof(belong));
     91         memset(degree, 0, sizeof(degree));
     92         indx = scc = 0;
     93         while(!st.empty()) {
     94             st.pop();
     95         }
     96         for(int i = 1; i<= m; i++ ) {
     97             scanf("%d %d", &u, &v);
     98             add_edge(u, v, 1);
     99             add_edge(v, u, 1);
    100         }
    101 
    102         tarjan(1, -1);
    103 
    104         for(int i = 1; i <= n; i++ ) {
    105             for(int j = first[i]; ~j; j = edge[j].next) {
    106                 int from = i, to = edge[j].to;
    107                 if(edge[j].cut) {
    108                     degree[ belong[i] ]++;
    109                 }
    110             }
    111         }
    112         int ans = 0;
    113         for(int i = 1; i <= scc; i++ ) {
    114             if(degree[i] == 1) {
    115                 ans ++;
    116             }
    117         }
    118         ans = (ans + 1) / 2;
    119         printf("%d
    ", ans);
    120     }
    121 
    122     return 0;
    123 }
    View Code

    2,点双联通

    题目背景

    割点

    题目描述

    给出一个n个点,m条边的无向图,求图的割点。

    输入输出格式

    输入格式:

    第一行输入n,m

    下面m行每行输入x,y表示x到y有一条边

    输出格式:

    第一行输出割点个数

    第二行按照节点编号从小到大输出节点,用空格隔开

    输入输出样例

    输入样例#1: 
    6 7
    1 2
    1 3
    1 4
    2 5
    3 5
    4 5
    5 6
    输出样例#1: 
    1 
    5

    说明

    n,m均为100000

    tarjan 图不一定联通!!!

    注意这题实际数据范围比题目描述要大。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 const int MAXN = 1e5 + 7;
     5 const int MAXM = 2e5 + 7;
     6 
     7 int n, m, first[MAXN], sign, indx, scc;
     8 
     9 int dfn[MAXN], low[MAXN], ins[MAXN], cut[MAXN];
    10 
    11 //stack<int>stk;
    12 
    13 struct Edge {
    14     int to, w, next;
    15 } edge[MAXM];
    16 
    17 inline void init() {
    18 //    for(int i = 0; i <= n; i++ ) {
    19 //        first[i] = -1;
    20 //    }
    21     memset(first, -1, sizeof(first));
    22     sign = 0;
    23 }
    24 
    25 inline void add_edge(int u, int v, int w) {
    26     edge[sign].to = v;
    27     edge[sign].w = w;
    28     edge[sign].next = first[u];
    29     first[u] = sign++;
    30 }
    31 
    32 inline void init_tarjan() {
    33     for(int i = 0; i <= n; i++ ) {
    34         dfn[i] = low[i] = ins[i] = cut[i] = 0;
    35     }
    36     indx = scc = 0;
    37     //while(!stk.empty()) { stk.pop(); }
    38 }
    39 
    40 void tarjan(int now, int root) {
    41     low[now] = dfn[now] = ++indx;
    42     int child = 0;
    43     for(int i = first[now]; ~i; i = edge[i].next) {
    44         int to = edge[i].to;
    45         if(!dfn[to]) {
    46             tarjan(to, root);
    47             low[now] = min(low[now], low[to]);
    48             ///非根节点,如果dfn[to] <= low[to]那么它是割点
    49             if(low[to] >= dfn[now] && now != root) {
    50                 cut[now] = 1;
    51             }
    52             if(now == root) {
    53                 child++;
    54             }
    55         }
    56         low[now] = min(low[now], dfn[to]);
    57     }
    58     if(child >= 2 && now == root) { ///根节点有两个以上子树的是割点
    59         cut[now] = 1;
    60     }
    61 }
    62 
    63 int main()
    64 {
    65     //freopen("testdata.in", "r", stdin);
    66     while(~scanf("%d %d", &n, &m)) {
    67         init();
    68         init_tarjan();
    69         for(int i = 1; i <= m; i++ ) {
    70             int u, v;
    71             scanf("%d %d", &u, &v);
    72             add_edge(u, v, 1);
    73             add_edge(v, u, 1);
    74         }
    75         for(int i = 1; i <= n; i++ ) {
    76             if(!dfn[i]) {
    77                 tarjan(i, i);
    78             }
    79         }
    80         vector<int>vec;
    81         for(int i = 1; i <= n; i++ ) {
    82             if(cut[i]) {
    83                 vec.push_back(i);
    84             }
    85         }
    86         printf("%d
    ", vec.size());
    87         for(int i = 0; i < vec.size(); i++ ) {
    88             printf("%d ", vec[i]);
    89         }
    90     }
    91     return 0;
    92 }
    View Code
  • 相关阅读:
    Mysql热备份
    win10 上安装虚拟机
    SpringMVC AJAX向后台传递数组参数/实体集合
    解决eclipse中tomcat不加载web项目的问题
    Python 基础第九天
    Python 基础第8天(文件管理)
    Python 基础第七天
    Python 基础第六天
    Python 基础第五天
    Python 基础第四天
  • 原文地址:https://www.cnblogs.com/Q1143316492/p/9195174.html
Copyright © 2020-2023  润新知