Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6723 | Accepted: 2924 |
Description
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
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7
Sample Output
2
Hint
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.
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stack>
using namespace std;
#define max_n 5005
#define max_e 10002
stack<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 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
*/