poj 3177

来源:互联网 发布:梦龙软件 编辑:程序博客网 时间:2024/05/01 22:16
Redundant Paths
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6723 Accepted: 2924

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

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.
 
 
类似3352,求割边,但是要判断重边。此题MLE了N次。直接快要崩溃了,看网上的代码,然后改自己的,最后改的与网上的几乎一样了,NND还是MLE。最后发现标记数组需要用bool型的,int型的就MLE,超无语啊。o(╯□╰)o。
 
#include<iostream>#include<string.h>#include<stdio.h>#include<stack>using namespace std;#define max_n 5005#define max_e 10002stack<int> st;//栈//int isInStack[max_n];//是否在栈内int low[max_n],dfn[max_n],tim;//点的low,dfn值;time从1开始int node_id;//强连通分量的个数int head[max_n],s_edge;//邻接表头  s_edge从1开始int gro_id[max_n];//记录某个点属于哪个强连通分量int n,m;int du[max_n];//出度与入度bool vis[max_n][max_n];struct Node{    int u,v;    int next;} edge[max_e];void init()//初始化{    s_edge=0;tim=0;    node_id=0;//存储    memset(head,-1,sizeof(head));    //memset(isInStack,0,sizeof(isInStack));    memset(dfn,0,sizeof(dfn));    memset(du,0,sizeof(du));//出度入度的初始化    memset(vis,0,sizeof(vis));}void addedge(int u,int v){    edge[s_edge].u=u;    edge[s_edge].v=v;    edge[s_edge].next=head[u];    head[u]=s_edge++;    edge[s_edge].u=v;    edge[s_edge].v=u;    edge[s_edge].next=head[v];    head[v]=s_edge++;}int min(int a,int b){    return a<b?a:b;}void tarjan(int u,int father){    st.push(u);    //isInStack[u]=1;    dfn[u]=++tim; //记录点u出现的记录,并放在栈中    low[u]=tim;    int e,v;    for(e=head[u]; e!=-1; e=edge[e].next) //如果是叶子节点,head[u]=0,edge[e].next=0;    {        v=edge[e].v;        if(v==father)continue;        if(!dfn[v])        {            tarjan(v,u);            low[u]=min(low[u],low[v]);        }        else            low[u]=min(low[u],dfn[v]);    }    int j;    if(dfn[u]==low[u])//找到一个强连通,元素出栈    {        node_id++;        while(1)        {            j=st.top();            st.pop();            gro_id[j]=node_id;            if(j==u)break;        }    }}int main(){    int a,b;    scanf("%d %d",&n,&m);    init();    for(int i = 0 ; i <m ; ++i)    {        scanf("%d%d",&a,&b);        if(vis[a][b])continue;        addedge(a,b);        vis[a][b]=vis[b][a]=1;    }    tarjan(1,0);    int sum=0,sum1=0;    for(int i=0; i<s_edge; i++)    {        if(low[edge[i].v]!=low[edge[i].u])        {            du[gro_id[edge[i].u]]++;            du[gro_id[edge[i].v]]++;        }    }    for(int i=1; i<=node_id; i++)        if(du[i]==2)            sum1++;    cout<<(sum1+1)/2<<endl;    return 0;}/*7 71 22 33 42 54 55 65 7*/

原创粉丝点击