POJ 3177 Redundant Paths(强连通分量)

题目链接:http://poj.org/problem?id=3177

题目大意是一个无向图给你n个点m条边,让你求出最少加多少条边 可以让任意两个点相通两条及以上的路线(每条路线点可以重复,但是每条路径上不能有重边),简单来说就是让你加最少的边使这个图变成一个双连通图。

首先用tarjan来缩点,可以得到一个新的无环图,要是只有一个强连通分量,那本身就是一个双连通图。要是多个强连通分量,那我们可以考虑缩点后度数为1的点(肯定是由这个点开始连新边最优),那我们假设数出度数为1的点的个数为cnt,可以画几个图观察可得答案就是(cnt + 1) / 2。但是这题有个问题就是要去掉重边(A B和B A不算重边),不然会wa...,所以我用了二维map来去重边。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <map>
 6 using namespace std;
 7 const int MAXN = 5005;
 8 map <int , map<int , int> > mp;
 9 struct data {
10     int next , to;
11 }edge[MAXN * 5];
12 int head[MAXN] , low[MAXN] , dfn[MAXN] , st[MAXN] , block[MAXN] , du[MAXN];
13 int top , ord , sccnum , cont;
14 bool instack[MAXN];
15
16 void init() {
17     memset(head , -1 , sizeof(head));
18     memset(instack , false , sizeof(instack));
19     memset(low , 0 , sizeof(low));
20     memset(dfn , 0 , sizeof(dfn));
21     memset(du , 0 , sizeof(du));
22     top = ord = sccnum = cont = 0;
23 }
24
25 void add(int u , int v) {
26     edge[cont].next = head[u];
27     edge[cont].to = v;
28     head[u] = cont++;
29 }
30
31 void tarjan(int u , int par) {
32     low[u] = dfn[u] = ++ord;
33     st[++top] = u;
34     instack[u] = true;
35     for(int i = head[u] ; ~i ; i = edge[i].next) {
36         int v = edge[i].to;
37         if(v == par)
38             continue;
39         if(!dfn[v]) {
40             tarjan(v , u);
41             low[u] = min(low[u] , low[v]);
42         }
43         else if(instack[v]) {
44             low[u] = min(low[u] , dfn[v]);
45         }
46     }
47     if(low[u] == dfn[u]) {
48         int v;
49         sccnum++;
50         do {
51             v = st[top--];
52             instack[v] = false;
53             block[v] = sccnum;
54         }while(u != v);
55     }
56 }
57
58 int main()
59 {
60     int n , m , u , v;
61     while(~scanf("%d %d" , &n , &m)) {
62         init();
63         mp.clear();
64         while(m--) {
65             scanf("%d %d" , &u , &v);
66             if(!mp[u][v]) {
67                 add(u , v);
68                 add(v , u);
69                 mp[u][v]++;
70             }
71         }
72         tarjan(1 , -1);
73         for(int u = 1 ; u <= n ; u++) {
74             for(int i = head[u] ; ~i ; i = edge[i].next) {
75                 int v = edge[i].to;
76                 if(block[u] != block[v]) {
77                     du[block[u]]++;
78                 }
79             }
80         }
81         int res = 0;
82         for(int i = 1 ; i <= sccnum ; i++) {
83             if(du[i] == 1)
84                 res++;
85         }
86         printf("%d\n" , (res + 1) / 2);
87     }
88 }
时间: 2024-08-06 19:45:08

POJ 3177 Redundant Paths(强连通分量)的相关文章

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 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 re

poj 3177 Redundant Paths

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 forc

POJ 3177 Redundant Paths(Tarjan)

题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf,最后要添加的边的条数就是(leaf+1)/2 : 1 // 3177 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图 思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了) 代码: #include <cstdio> #include <cstring> #include <algorithm&

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点) ACM 题目地址: POJ 3352 Road Construction POJ 3177 Redundant Paths 题意: 问要添加几条边才能使所给无向图图变成边双连通图. 分析: 边连通度:使无向图G不连通的最少删边数量为其边连通度. 边双连通图:边连通度大于1的无向图. 首先缩点,让图变成一个DAG. 现在问题转化为:在树中至少添加多少条边能使图变

poj 3177 Redundant Paths (双联通)

/******************************************************* 题目:Redundant Paths (poj 2177) 链接:http://poj.org/problem?id=3177 算法:双联通+缩点 思路:先找出所有双联通分量,把这些分量缩成一个点 再找出所有度为一的点,用这些点数加一除2就可以了 ********************************************************/ #include<cs

POJ 3177 Redundant Paths(无向图缩点)

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 for

[双连通分量] POJ 3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 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

POJ 3177 Redundant Paths (双连通)

题目地址:POJ 3177 找出各个双连通分量度数为1的点,然后作为叶子节点,那么ans=(叶子结点数+1)/2.需要注意的是有重边. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include