POJ 1515 Street Directions
题意:
一幅无向图 将尽量多的无向边定向成有向边 使得图强连通 无向图保证是连通的且没有重边
思路:
桥必须是双向的 因此先求边双连通分量 并将桥保存在ans中
每一个双连通分量内的边一定都能够变成有向边(毕竟是圈组成的图) 边的定向方式分两种:
1、对于树枝边u->v 假设low[v]>dfn[u]说明v回不到u上面去 所以ans应该是v->u的边 否则是u->v
2、对于逆向边 应该全在ans中 由于对于dfs树而言 这样的边利于low减小
代码:
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long LL;
#define N 1010
#define M 2000010
#define inf 2147483647 int n,m,t=1,tot,idx;
int head[N],dfn[N],low[N];
struct edge
{
int u,v,next;
bool vis,cut,left;
}ed[M]; void add(int u,int v)
{
ed[tot].u=u;
ed[tot].v=v;
ed[tot].next=head[u];
ed[tot].vis=ed[tot].cut=ed[tot].left=false;
head[u]=tot++;
} void tarjan(int u)
{
int i,v;
dfn[u]=low[u]=++idx;
for(i=head[u];~i;i=ed[i].next)
{
v=ed[i].v;
if(ed[i].vis) continue;
ed[i].vis=ed[i^1].vis=true;
if(dfn[v]==-1)
{
tarjan(v);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])
{
ed[i].cut=ed[i^1].cut=true;
ed[i].left=ed[i^1].left=true;
}
}
else low[u]=min(low[u],dfn[v]);
}
} void dfs(int u)
{
int i,v;
dfn[u]=low[u]=++idx;
for(i=head[u];~i;i=ed[i].next)
{
if(ed[i].cut) continue;
v=ed[i].v;
if(dfn[v]==-1)
{
ed[i].vis=ed[i^1].vis=true;
dfs(v);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]) ed[i^1].left=true;
else ed[i].left=true;
}
else
{
low[u]=min(low[u],dfn[v]);
if(!ed[i].vis) ed[i].left=true;
ed[i].vis=ed[i^1].vis=true;
}
}
} void solve()
{
int i;
memset(dfn,-1,sizeof(dfn));
idx=0;
tarjan(1);
memset(dfn,-1,sizeof(dfn));
idx=0;
for(i=0;i<tot;i++) ed[i].vis=false;
for(i=1;i<=n;i++)
{
if(dfn[i]==-1) dfs(i);
}
} int main()
{
int i,u,v;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m) break;
tot=0;
memset(head,-1,sizeof(head));
for(i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
solve();
printf("%d\n\n",t++);
for(i=0;i<tot;i++)
{
if(ed[i].left) printf("%d %d\n",ed[i].u,ed[i].v);
}
printf("#\n");
}
return 0;
}
POJ 1515 Street Directions的更多相关文章
- POJ 1515 Street Directions --一道连通题的双连通和强连通两种解法
题意:将一个无向图中的双向边改成单向边使图强连通,问最多能改多少条边,输出改造后的图. 分析: 1.双连通做法: 双连通图转强连通图的算法:对双连通图进行dfs,在搜索的过程中就能按照搜索的方向给所有 ...
- POJ 1515 Street Directions (边双连通)
<题目链接> 题目大意: 有m条无向边,现在把一些边改成有向边,使得所有的点还可以互相到达.输出改变后的图的所有边(无向边当成双向的有向边输出). 解题分析: 因为修改边后,所有点仍然需要 ...
- UVA 610 - Street Directions(割边)
UVA 610 - Street Directions option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...
- UVALive 5412 Street Directions
Street Directions Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. ...
- POJ 1320 Street Numbers 【佩尔方程】
任意门:http://poj.org/problem?id=1320 Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1515+poj 1438(边双连通)
题目链接:http://poj.org/problem?id=1515 思路:题目的意思是说将一个无向图改成有向图,使其成为强连通,输出所有的边.我们可以求无向图的边双连通分量,对于同一个双连通分量, ...
- POJ 1320 Street Numbers 解佩尔方程
传送门 Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2529 Accepted: 140 ...
- POJ 1320 Street Numbers(佩尔方程)
Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3078 Accepted: 1725 De ...
- POJ 1320 Street Numbers Pell方程
http://poj.org/problem?id=1320 题意很简单,有序列 1,2,3...(a-1),a,(a+1)...b 要使以a为分界的 前缀和 和 后缀和 相等 求a,b 因为序列很 ...
随机推荐
- 将n进制的数组压缩成字符串(0-9 a-z)同一时候解压
比如一个3进制的数组: [1 1 2 2 2 0 0] 用一个字符串表示... 此类题目要明白两点: 1. 打表:用数组下标索引字符.同一时候注意假设从字符相应回数字: int index = (st ...
- spring获取bean 实例
ApplicationContext ctx = new ClassPathXmlApplication("applicationContext.xml"); DataSource ...
- Delphi访问活动目录
活动目录Active Directory是用于Windows Server的目录服务,它存储着网络上各种对象的有关信息,并使该信息易于管理员和用户查找及使用.Active Directory使用结构化 ...
- Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值
原文:[置顶] Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值 前面我们了解了如何启动一个Activity,一个Activity在启动另外一个Activity的时候 ...
- Java的HashCode
·HashCode就是根据对象存储在内存的地址计算出的一个值.这个值可以标识这个对象的位置.也可以对比两个引用变量是否指向同一个对象. ·String重写了hashCode方法——改为根据字符序列计算 ...
- html ui设计案例
1.jquery特效:http://www.5icool.org 2. http://www.open-lib.com/Lib/1992.jsp
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- Bootstrapping (compilers) - Wikipedia, the free encyclopedia
Bootstrapping (compilers) - Wikipedia, the free encyclopedia Bootstrapping (compilers)
- 广东省-IT公司红黑榜排名
红榜Top100 Order Company Name Point Change 1 百富计算机技术(深圳)有限公司 94.00 -- 2 中国网通广州分公司 88.00 -- 3 深圳市汇 ...
- hdu 5090 Game with Pearls
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题意:n个数,k,给n个数加上k的正倍数或者不加,问最后能不能凑成1 到 n的序列 题目分类:暴 ...