Redundant Paths
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11514 | Accepted: 4946 |
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.
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.
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
题意:给定一个连通图,问至少加几条边可使这个图为双连通的。
思路:将原图G的双连通分量浓缩为一个点得到图G',则答案为(图G'中的叶子个数+1)/2。
存在重边
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; const int MAXN=5005; bool mp[MAXN][MAXN];//存在重边用邻接矩阵,用int的话会MLE int n,m; int dfn[MAXN],low[MAXN],time; int stack[MAXN],top; int ins[MAXN]; int belong[MAXN],cnt; void tarjan(int u,int fa) { dfn[u]=low[u]=++time; stack[top++]=u; ins[u]=true; for(int v=1;v<=n;v++) { if(mp[u][v]) { if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); } else if(v!=fa&&ins[v]) low[u]=min(low[u],dfn[v]); } } if(dfn[u]==low[u])//连通分量,发现一个处理一个 { int v; cnt++; do{ v=stack[--top]; ins[v]=false; belong[v]=cnt; }while(u!=v); } } int deg[MAXN]; void cal() { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(mp[i][j]&&belong[i]!=belong[j]) { deg[belong[i]]++; deg[belong[j]]++; } } int res=0; for(int i=1;i<=cnt;i++) { if(deg[i]==2) res++; } printf("%d ",(res+1)/2); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(ins,false,sizeof(ins)); time=0; cnt=0; memset(belong,0,sizeof(belong)); memset(mp,false,sizeof(mp)); memset(deg,0,sizeof(deg)); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); mp[u][v]=mp[v][u]=true; } tarjan(1,-1); cal(); } }
tarjan算法模板:
#include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int MAXN=5005; vector<int> mp[MAXN]; int n,m; int dfn[MAXN],low[MAXN],index; int bridge[MAXN]; int critical[MAXN]; int root; int stack[MAXN],top; bool ins[MAXN]; int belong[MAXN],cnt; void tarjan(int u,int fa) { stack[u]=top++; ins[u]=true; dfn[u]=low[u]=++index; int son=0; for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); son++; if((u==root&&son>1)||(u!=root&&dfn[u]<=low[v])) { critical[u]=1; } if(dfn[u]<low[v]) { bridge[v]=1; } } else if(v!=fa) low[u]=min(dfn[v],low[u]); } if(dfn[u]==low[u]) { int v; cnt++; do{ v=stack[--top]; ins[v]=false; belong[v]=cnt; }while(u!=v); } } int main() { scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u); } for(int i=1;i<=n;i++) if(!dfn[i]) { root=i; tarjan(i,-1); } printf("割点分别为: "); for(int i=1;i<=n;i++) { if(critical[i]) printf("%d ",i); } printf(" "); printf("割边一端的节点为: "); for(int i=1;i<=n;i++) { if(bridge[i]) printf("%d ",i); } printf(" "); printf("连通分量数目为: "); printf("%d ",cnt); return 0; } /* 5 6 1 2 1 3 2 3 3 4 4 5 3 5 */