POJ 3177 Redundant Paths

来源:互联网 发布:数据库表结构设计 编辑:程序博客网 时间:2024/05/16 03:27

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 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.
Source

USACO 2006 January Gold

//缩点之后  叶节点数+1 除以2 #include<iostream>#include<cstring>#include<cstdio>#include<stack>using namespace std;const int MAXN = 5005;const int MAXM = 10005;int dfn[MAXN],low[MAXN],visx,tot,head[MAXN],n,m,belong[MAXN],cur;bool exist[MAXN];struct Edge{ int to,next; }e[MAXM];stack<int> st;void init(){    memset(dfn,-1,sizeof dfn );memset(low,-1,sizeof low );    memset(exist,0,sizeof exist );memset(head,-1,sizeof head );    tot=0;visx=0;cur=0;    while(!st.empty()) st.pop();}void Add_Edge(int u,int v){    e[++tot].to=v;e[tot].next=head[u];head[u]=tot;}void Tarjan(int u,int father){    dfn[u]=low[u]= ++visx;    st.push(u);exist[u]=true;    int flag=0;    for(int i=head[u];i!=-1;i=e[i].next){        int v=e[i].to;        if(v==father&&!flag){            flag=1;continue;//精华之所在         }        if(dfn[v]==-1){            Tarjan(v,u);            low[u]=min(low[u],low[v]);        }        else if(exist[v]) low[u]=min(low[u],dfn[v]);    }    if(low[u]==dfn[u]){        cur++;belong[u]=cur;exist[u]=false;        while(st.top()!=u){            belong[st.top()]=cur;            exist[st.top()]=false;            st.pop();        }        st.pop();    }}int Solve(){    int ans=0,du[MAXN]={0};    for(int i=1;i<=n;i++)        for(int j=head[i];j!=-1;j=e[j].next){            int v=e[j].to;            if(belong[v]!=belong[i]){                du[belong[i]]++;du[belong[v]]++;            }        }    for(int i=1;i<=cur;i++)        if(du[i]==2) ans++;//对于叶节点只有一个入度但是双向边枚举到了两边     return ans;}int main(){    while(scanf("%d%d",&n,&m)==2){        init();        for(int u,v,i=1;i<=m;i++){            scanf("%d%d",&u,&v);            Add_Edge(u,v);Add_Edge(v,u);        }        Tarjan(1,0);        printf("%d\n",(Solve()+1)/2);    }    return 0;}
原创粉丝点击