poj 3177 边双连通分量(处理重边)

来源:互联网 发布:vs源码加密 编辑:程序博客网 时间:2024/05/01 06:58

Redundant Paths
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10257 Accepted: 4418

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 71 22 33 42 54 55 65 7

Sample Output

2
题意:问你在图中加几条边使每个点到其他点的路径数不小于2条。

分析:由于图是无向图,所以在一个连通分量中的每个点到其他点的路径至少是2条的,然后就考虑每个连通分量与多少个连通分量相连,如果只和一个相连,那么他就必须要连出一条边。

本题有重边,所以直接按照双连通分量的方法可能会把不在同一分量的加在同一分量中,然而并不想用一个二维数组标记(浪费时间,浪费空间),所以把桥标记为1,重复的桥标记为2。


#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<vector>#include<queue>#include<cmath>#include<stack>#include<set>#include<map>#define INF 0x3f3f3f3f#define MAX 10005#define mod 10000007#define CLR(a,b) memset((a),(b),sizeof((a)))#pragma comment(linker, "/STACK:102400000,102400000")#define ul u<<1#define ur (u<<1)|1using namespace std;typedef long long ll;struct edge {    int v,next;} e[MAX*2];int tot,head[MAX];int dfs_clock,bcc_cnt,pre[MAX],low[MAX],bridge[MAX*2],vis[MAX];void addedge(int u,int v) {    e[tot].v=v;    e[tot].next=head[u];;    head[u]=tot++;}void tdfs(int u,int fa) {    pre[u]=low[u]=++dfs_clock;    for(int i=head[u]; i!=-1; i=e[i].next) {        int v=e[i].v;        if(!pre[v]) {            tdfs(v,u);            low[u]=min(low[u],low[v]);            if(low[v]>pre[u])                bridge[i]=bridge[i^1]=1; ///标记桥        } else {            if(pre[v]<pre[u]&&v!=fa)                low[u]=min(low[u],pre[v]);            if(low[v]>pre[u])                bridge[i]=bridge[i^1]=2; ///标记重复的桥        }    }}int e_cnt;void dfs(int u) {    vis[u]=1;    for(int i=head[u]; i!=-1; i=e[i].next) {        if(bridge[i]) {            if(bridge[i]==1) ///记录该双连通分量的桥            e_cnt++;            continue;        }        int v=e[i].v;        if(!vis[v]) {            dfs(v);        }    }}void find_bcc(int n) {    CLR(pre,0);    CLR(low,0);    CLR(bridge,0);    dfs_clock=0;    for(int i=1; i<=n; i++)        if(!pre[i]) tdfs(i,i);}void init() {    tot=0;    CLR(head,-1);}int main() {    int n,m,u,v;    init();    scanf("%d%d",&n,&m);    for(int i=1; i<=m; i++) {        scanf("%d%d",&u,&v);        addedge(u,v);        addedge(v,u);    }    find_bcc(n);    int num=0;    for(int i=1; i<=n; i++) {        e_cnt=0;        if(!vis[i]) {            dfs(i);            if(e_cnt==1) num++; ///桥为1时说明只有一条路到其他连通分量        }    }    printf("%d\n",(num+1)/2);    return 0;}


0 0