0
点赞
收藏
分享

微信扫一扫

poj 1438 One-way Traffic(混合图改有向图)

Ichjns 2023-02-07 阅读 55


One-way Traffic
Time Limit: 10000MS         Memory Limit: 10000K
Total Submissions: 969         Accepted: 313         Special Judge
Description

In a certain town there are n intersections connected by two- and one-way streets. The town is very modern so a lot of streets run through tunnels or viaducts. Of course it is possible to travel between any two intersections in both ways, i.e. it is possible to travel from an intersection a to an intersection b as well as from b to a without violating traffic rules. Because one-way streets are safer, it has been decided to create as much one-way traffic as possible. In order not to make too much confusion it has also been decided that the direction of traffic in already existing one-way streets should not be changed. 

Your job is to create a new traffic system in the town. You have to determine the direction of traffic for as many two-way streets as possible and make sure that it is still possible to travel both ways between any two intersections. 

Write a program that: 

> reads a description of the street system in the town from the standard input, 

> for each two-way street determines one direction of traffic or decides that the street must remain two-way, 

> writes the answer to the standard output. 
Input

The first line of the input contains two integers n and m, where 2 <= n <= 2000 and n-1 <= m <= n(n-1)/2. Integer n is the number of intersections in the town and interger m is the number of streets. 

Each of the next m lines contains three integers a, b and c, where 1 <= a <= n, 1 <= b <= n, a != b and c belongs to {1, 2}. If c = 1 then intersections a and b are connected by an one-way street from a to 
b. If c = 2 then intersections a and b are connected by a two-way street. There is at most one street 
connecting any two intersections. 

Process to the end of file. 
Output

The output contains exactly the same number of lines as the number of two-way streets in the input. 

For each such street (in any order) the program should write three integers a, b and c meaning, the new 
direction of the street from a to b (c = 1) or that the street connecting a and b remains two-way (c = 2). If there are more than one solution with maximal number of one-way streets then your program should 
output any of them but just one.
Sample Input

4 4
4 1 1
4 2 2
1 2 1
1 3 2
Sample Output

2 4 1
3 1 2
 

题意:给出一个既有有向边又有无向边的强连通的图。要你让尽可能多的无向边变成有向边,使得这个图为强连通。

分析

途中的桥不能变成单向边,其他的按照dfs顺序变成单向边。

但是为超内存到死,不明白我用数组模拟的邻接表,而用结构体模拟有什么区别

天哪,真是

超内存代码(改成结构体就可以过):

///tarjan算法求无向图(可有重边)的桥、边双连通分量并缩点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int SIZE = 2010;
const int N = 4000010;
int head[SIZE], ver[N], Next[N],flag[N];
///head[i]=x表示以i起点的数组下标,ver[x]表示以i起点的终点编号
///next[x]=y下一个表示以i起点的数组下标 ,ver[y]表示另一个以i起点的终点编号
int dfn[SIZE], low[SIZE];
///dfn表示时间戳
///low表示追溯值
int n, m, tot, num;
int mark[N]; ///标记数组
void add(int x, int y,int c) {
ver[++tot] = y,flag[tot]=c,Next[tot] = head[x], head[x] = tot;
}

void tarjan(int x, int in_edge) {
dfn[x] = low[x] = ++num;
for (int i = head[x]; i; i = Next[i]) {///遍历每一个结点
int y = ver[i];
if(mark[i] != -1) continue;//已经标记过
if(flag[i]==0) continue;
mark[i]=1;//按DFS顺序选择方向
mark[i^1]=0;//反向边先不考虑,如果是桥在考虑
if (!dfn[y]) {
tarjan(y, i);
low[x] = min(low[x], low[y]);
if (low[y] > dfn[x]) ///找到桥
mark[i^1]=1;
}
else if (i != (in_edge ^ 1)) ///利用异或性质解决重边
///防止x结点到父亲结点
low[x] = min(low[x], dfn[y]);
}
}
int main() {

while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(mark,-1,sizeof(mark));
memset(head,0,sizeof(head));
memset(flag,0,sizeof(flag));
num=0;
tot = 1;

for (int i = 1; i <= m; i++) {
int x, y,c;
scanf("%d%d%d", &x, &y,&c);
add(x,y,c);
if(c==1) c=0;
add(y,x,c);
}

for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i, 0);

for (int i =2; i <tot; i+=2)
{

if(flag[i]==2&&mark[i]==1&&mark[i^1]==1)
printf("%d %d 2\n", ver[i^1],ver[i]);
else if(flag[i]==2&&mark[i]==1&&mark[i^1]==0)
printf("%d %d 1\n", ver[i^1],ver[i]);
else if(flag[i]==2&&mark[i]==0&&mark[i^1]==1)
printf("%d %d 1\n", ver[i],ver[i^1]);

}

}
return 0;
}

结构体AC代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int N=5000+50;
const int M=4000010;
int dfn[N],head[N],low[N],belong[N];
bool instack[N];
int in[N],out[N];
struct edge
{
int u,v,next,cnt,d;
}e[M*2];
int n,m;
int cnt,num,index;
stack<int>s;
void addedge(int u,int v,int d)
{
e[num].u=u;
e[num].v=v;
e[num].d=d;
e[num].cnt=-1;
e[num].next=head[u];
head[u]=num++;
}
void init()
{
memset(head,-1,sizeof(head));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(belong,0,sizeof(belong));
memset(instack,false,sizeof(instack));
while(!s.empty())
s.pop();
cnt=index=num=0;
}
void tarjan(int u,int pre)
{
dfn[u]=low[u]=++index;
s.push(u);
instack[u]=true;
int v;
for(int i=head[u];i!=-1;i=e[i].next)
{
v=e[i].v;
if(v==pre)continue;
if(e[i].cnt!=-1)continue;
if(e[i].d==0)continue;
e[i].cnt=1;
e[i^1].cnt=0;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])
{
e[i^1].cnt=1;
}
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
cnt++;
do{
v=s.top();
s.pop();
belong[v]=cnt;
instack[v]=false;
}while(u!=v);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int i,j;
init();
int a,b,c;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c==2)
{
addedge(a,b,c);
addedge(b,a,c);
}
else
{
addedge(a,b,c);
addedge(b,a,0);
}
}
for(i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i,i);
}
for(i=0;i<num;i+=2)
{
if(e[i].d==2&&e[i].cnt==1&&e[i^1].cnt==1)
{
printf("%d %d 2\n",e[i^1].v,e[i].v);
}
else if(e[i].d==2&&e[i].cnt==1&&e[i^1].cnt==0)
{
printf("%d %d 1\n",e[i^1].v,e[i].v);
}
else if(e[i].d==2&&e[i].cnt==0&&e[i^1].cnt==1)
{
printf("%d %d 1\n",e[i].v,e[i^1].v);
}
}
}
return 0;
}

 

举报

相关推荐

0 条评论