poj1515 Street Directions

链接:http://poj.org/problem?id=1515

题意:给定一个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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值