• 【HDOJ4612】【双连通分量缩点+找树的直径】


    http://acm.hdu.edu.cn/showproblem.php?pid=4612

    Warm up

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 8309    Accepted Submission(s): 1905

    Problem Description
      N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
      If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
    People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
      Note that there could be more than one channel between two planets.
     
    Input
      The input contains multiple cases.
      Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
      (2<=N<=200000, 1<=M<=1000000)
      Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
      A line with two integers '0' terminates the input.
     
    Output
      For each case, output the minimal number of bridges after building a new channel in a line.
     
    Sample Input
    4 4 1 2 1 3 1 4 2 3 0 0
     
    Sample Output
    0
    题目大意:给一个连通图,求加一条边之后最少还有多少个桥。
    题目分析:首先看到要求是"改造桥",则先进行缩点,由于是改造桥,所以进行边双连通的缩点,然后求树的直径【树上桥最多的一条路】,则最后结果ans=原来桥数-直径。
    题解:Tarjan缩点求桥数+两次bfs求树的直径
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<queue>
      6 using namespace std;
      7 queue<int>pq;
      8 struct edge {
      9     int fr;
     10     int to;
     11     int next;
     12 }e[2000002], e2[2000002];
     13 int cnt, scc_cnt, ccnt, tim;
     14 int head[200002], head2[200002], dfn[200002], low[200002], stk[200002], col[200002],d[200002], tp,dis,node;
     15 bool in_stk[200002], vis[200002];
     16 int ans = 0;
     17 void dfs(int x,  int fa) {
     18     d[x] = dis++;
     19     for (int i = head2[x]; i != -1; i = e2[i].next) {
     20         int v = e2[i].to;
     21         if (v == fa)continue;
     22         if (!vis[v]) {
     23             vis[v] = 1;
     24             dfs(v,  x);
     25         }
     26     }
     27     return;
     28 }
     29 void bfs(int x) {
     30     while (!pq.empty())pq.pop();
     31     pq.push(x);
     32     memset(d, 0, sizeof(d));
     33     memset(vis, 0, sizeof(vis));
     34     while (!pq.empty()) {
     35         int s = pq.front(); pq.pop();
     36         for (int i = head2[s]; i != -1; i = e2[i].next) {
     37             int v = e2[i].to;
     38             if (!vis[v]) {
     39                 vis[v] = 1;
     40                 d[v] = d[s] + 1;
     41                 pq.push(v);
     42                 if (d[v] > ans) {
     43                     node = v;
     44                     ans = d[v];
     45                 }
     46             }
     47         }
     48     }
     49 }
     50 void Tarjan(int u, int fa, int id) {
     51     dfn[u] = low[u] = ++tim;
     52     in_stk[u] = 1;
     53     stk[tp++] = u;
     54     for (int i = head[u]; i != -1; i = e[i].next) {
     55         int v = e[i].to;
     56         if (!dfn[v]) {
     57             Tarjan(v, u, i);
     58             if (low[v] < low[u])low[u] = low[v];
     59         }
     60         else if (in_stk[v] && ((i ^ 1) != id)) {
     61             if (dfn[v] < low[u])low[u] = dfn[v];//这里使用dfn[v]和low[v]都可以
     62         }
     63     }
     64     if (dfn[u] == low[u]) {
     65         scc_cnt++;
     66         do {
     67             tp--;
     68             int tt = stk[tp];
     69             col[tt] = scc_cnt;
     70             in_stk[tt] = 0;
     71         } while (stk[tp] != u);
     72     }
     73 }
     74 void add(int x, int y) {
     75     e[cnt].fr = x;
     76     e[cnt].to = y;
     77     e[cnt].next = head[x];
     78     head[x] = cnt++;
     79 }
     80 void add2(int x, int y) {
     81     e2[ccnt].fr = x;
     82     e2[ccnt].to = y;
     83     e2[ccnt].next = head2[x];
     84     head2[x] = ccnt++;
     85 }
     86 int main() {
     87     int n, m;
     88     scanf("%d%d", &n, &m);
     89     while (n || m) {
     90         cnt = 0;
     91         tp = 0;
     92         scc_cnt = 0;
     93         ccnt = 0;
     94         ans = 0;
     95         tim = 0;
     96         memset(vis, 0, sizeof(vis));
     97         memset(in_stk, 0, sizeof(in_stk));
     98         memset(col, 0, sizeof(col));
     99         memset(head, -1, sizeof(head));
    100         memset(head2, -1, sizeof(head2));
    101         memset(dfn, 0, sizeof(dfn));
    102         memset(low, 0, sizeof(low));
    103         while (m--) {
    104             int a, b;
    105             scanf("%d%d", &a, &b);
    106             add(a, b);
    107             add(b, a);
    108         }
    109         for (int i = 1; i <= n; i++) {
    110             if (!dfn[i]) {
    111                 Tarjan(i, -1, -1);
    112             }
    113         }
    114         for (int i = 0; i < cnt; i += 2) {
    115             if (col[e[i].fr] != col[e[i].to]) {
    116                 add2(col[e[i].fr], col[e[i].to]);
    117                 add2(col[e[i].to], col[e[i].fr]);
    118             }
    119         }
    120         bfs(1);
    121         bfs(node);
    122         cout << ccnt / 2 - ans << endl;
    123         scanf("%d%d", &n, &m);
    124     }
    125     return 0;
    126 }
    View Code
     
  • 相关阅读:
    Gradle Gretty进行runAppDebug的Listening for transport dt_socket at address: 5005 的后续配置
    Oracle :value too large for column "SCHEMA"."TABLE"."COLUMN" (actual: 519, maximum: 500)的解决方案
    js file对象 文件大小转换可视容易阅读的单位
    JS的Event各种属性级target/currentTarget/relatedTarget各种目录的解释
    浏览器控制台是否打开的一些措施的讨论
    eclipse启动指定jvm的版本
    IDEA terminal无法从vim的编辑模式转换为命令模式
    win7 64位系统在IronPython2.7 rc安装后运行出现"ipy64/ipy.exe"does not exist解决办法
    VS2010 插件 CSS3 IS 2.1.1 在win7 64位机子上安装小记
    Asp.net ajax 1.0 绑定drowdownlist时取值问题
  • 原文地址:https://www.cnblogs.com/MekakuCityActor/p/9038071.html
Copyright © 2020-2023  润新知