https://codeforces.com/contest/1385/problem/E
题目大意:给定图的m条边,有的为有向,有的无向,让你规定无向边的方向,使它成为有向无环图
题解:DAG图具有拓扑序,若规定无向边方向后仍为DAG图,则让拓扑序小的指向拓扑序大的,这样不会破坏拓扑序。
代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define N 200009
using namespace std;
queue<int>q;
int T,js;
bool flag;
int n,m;
int sumedge;
int head[N],xx[N],yy[N],vis[N],col[N],In_du[N],dep[N];
struct Edge
{
int x,y,nxt;
Edge(int x=0,int y=0,int nxt=0):
x(x),y(y),nxt(nxt){}
}edge[N];
void add(int x,int y)
{
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
}
void Clear_Init()
{
memset(In_du,0,sizeof(In_du));
memset(head,0,sizeof(head));
while(!q.empty()) q.pop();
sumedge=0;flag=true;js=0;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int p,x,y;
cin>>p>>x>>y;
if(p==1)
{
add(x,y);
In_du[y]++;
}
xx[i]=x,yy[i]=y;
}
}
bool DFS(int x)
{
vis[x]=true;
col[x]=0;
for(int i=head[x];i;i=edge[i].nxt)
{
int v=edge[i].y;
if(col[v]==0) return false;
else if(col[v]==-1&&!DFS(v)) return false;
}
col[x]=1;
return true;
}
void pre_slove()
{
for(int i=1;i<=n;i++)
{
if(!vis[i]&&!DFS(i))
{
cout<<"NO
";
flag=false;
return ;
}
}
}
void Top_sort()
{
for(int i=1;i<=n;i++)
{
if(!In_du[i]) q.push(i);
}
while(!q.empty())
{
int now=q.front();q.pop();
dep[now]=++js;
for(int i=head[now];i;i=edge[i].nxt)
{
int v=edge[i].y;
In_du[v]--;
if(!In_du[v]) q.push(v);
}
}
}
void Put()
{
cout<<"YES
";
for(int i=1;i<=m;i++)
{
int x=xx[i],y=yy[i];
if(dep[x]<dep[y]) cout<<x<<" "<<y<<endl;
else cout<<y<<" "<<x<<endl;
}
}
int main()
{
cin>>T;
while(T--)
{
Clear_Init();
//pre_slove();
//if(!flag) continue;
Top_sort();
if(js<n) cout<<"NO
";
else Put();
}
return 0;
}