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 |
[Submit] [Go Back] [Status] [Discuss]
题目大意:
给出一个无向连通图,判断最少需要加多少条边,才能使得任意两点之间至少有两条相互"边独立"的道路(没有公共边).注意:给出的边中可能含有重边.
题解:tarjan
虽然是无向图,但是在加边的时候还有加双向边,做tarjan的时候需要判断连接的节点是否是更新过了的节点,一条边不能来回走,但是需要特判重边的情况。
做完tarjan,会得到一颗树,然后求树中叶子节点的数量。答案就是(叶子节点的数量+1)/2.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 100003
using namespace std;
int n,m,top,tot,sz,cnt,x[N],y[N],du[N];
int point[N],next[N],v[N],belong[N],dfsn[N],ins[N],st[N],low[N];
void add(int x,int y)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x;
}
void tarjan(int x,int f)
{
ins[x]=1; st[++top]=x;
low[x]=dfsn[x]=++sz;
bool pd=false;
for (int i=point[x];i;i=next[i])
{
if (v[i]==f&&!pd) {
pd=true;
continue;
}
if (!dfsn[v[i]]) tarjan(v[i],x),low[x]=min(low[x],low[v[i]]);
else if (ins[v[i]]) low[x]=min(low[x],dfsn[v[i]]);
}
if (dfsn[x]==low[x]) {
++cnt; int j;
do{
j=st[top--];
belong[j]=cnt;
}while (j!=x);
}
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) {
scanf("%d%d",&x[i],&y[i]);
add(x[i],y[i]);
}
for (int i=1;i<=n;i++)
if (!dfsn[i]) tarjan(i,i);
for (int i=1;i<=m;i++)
if (belong[x[i]]!=belong[y[i]]) {
int t=belong[x[i]]; int t1=belong[y[i]];
//cout<<t<<" "<<t1<<endl;
du[t]++; du[t1]++;
}
//cout<<cnt<<endl;
int sum=0;
for (int i=1;i<=cnt;i++)
if (du[i]==1) sum++;
printf("%d\n",(sum+1)/2);
}
非递归版本的tarjan....
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define N 100003
using namespace std;
int tot,nxt[N],point[N],v[N],low[N],dfsn[N],cur[N],st[N];
int fa[N],pd[N],belong[N],sz,n,m,top,ins[N],cnt,du[N];
void add(int x,int y)
{
tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;
tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;
}
void tarjan(int rt)
{
int x=rt; fa[x]=rt;
while (true) {
if (!dfsn[x]) {
low[x]=dfsn[x]=++sz; ins[x]=1; pd[x]=false;
st[++top]=x; cur[x]=point[x];
}
bool mark=false;
for (int i=cur[x];i;i=nxt[i]){
cur[x]=nxt[i];
if (v[i]==fa[x]&&!pd[x]) {
pd[x]=true;
continue;
}
if (!dfsn[v[i]]) {
mark=true; fa[v[i]]=x; x=v[i];
break;
}else if (ins[v[i]]) low[x]=min(low[x],dfsn[v[i]]);
}
if (!mark) {
int t=fa[x];
low[t]=min(low[t],low[x]);
if (low[x]==dfsn[x]) {
int j; ++cnt;
do
{
j=st[top--];
belong[j]=cnt;
ins[j]=0;
}while (j!=x);
}
if (x==rt) break;
x=fa[x];
}
}
}
int main()
{
freopen("a.in","r",stdin);
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) {
int x,y; scanf("%d%d",&x,&y);
add(x,y);
}
tarjan(1);
// for (int i=1;i<=n;i++) cout<<belong[i]<<" "; cout<<endl;
for (int i=1;i<=n;i++)
for (int j=point[i];j;j=nxt[j]){
int r1=belong[v[j]]; int r2=belong[i];
if (r1!=r2) {
du[r1]++; du[r2]++;
}
}
//for (int i=1;i<=cnt;i++) cout<<du[i]<<" ";
// cout<<endl;
int size=0;
for (int i=1;i<=cnt;i++)
if (du[i]==2) size++;
printf("%d\n",(size+1)/2);
}