题意:给定一个n个点m条边的无向图。现在需要将所有边变为一条单向或者两条单向并且使得任意两点i,j能互相到达,要求变成两条单向的边最少。
分析:只有桥需要变成两条单向边,其他的边按dfs过程指向即可。
代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps=1e-5;
const int N=1e3+10;
const int M=2e6+10;
const ll MOD=1000000007;
const int mod=1000000007;
const int MAX=1000000010;
const double pi=acos(-1.0);
int tot,u[N],v[M],id[M],pre[M];
void add(int a,int b,int c) {
v[tot]=b;id[tot]=c;pre[tot]=u[a];u[a]=tot++;
v[tot]=a;id[tot]=c;pre[tot]=u[b];u[b]=tot++;
}
vector<int>E[N];
int dfs_clock,fir[N],sig[M];
int dfs(int a) {
int i,lowa,lowv;
fir[a]=lowa=++dfs_clock;
for (i=u[a];~i;i=pre[i])
if (!sig[id[i]]) {
sig[id[i]]=1;
if (!fir[v[i]]) {
lowv=dfs(v[i]);
lowa=min(lowa,lowv);
E[a].push_back(v[i]);
if (lowv>fir[a]) E[v[i]].push_back(a);
} else E[a].push_back(v[i]),lowa=min(lowa,fir[v[i]]);
}
return lowa;
}
int main()
{
int a,b,i,j,n,m,ca=0;
while (scanf("%d%d", &n, &m)&&(n+m)) {
for (i=0;i<=n;i++) u[i]=-1,E[i].clear();
for (tot=0,i=1;i<=m;i++) scanf("%d%d", &a, &b),sig[i]=0,add(a,b,i);
memset(fir,0,sizeof(fir));
for (i=1;i<=n;i++)
if (!fir[i]) dfs(i);
printf("%d\n\n", ++ca);
for (i=1;i<=n;i++)
for (j=0;j<E[i].size();j++) printf("%d %d\n", i, E[i][j]);
printf("#\n");
}
return 0;
}