[Codeforces Round #656 (Div. 3)] (E. Directing Edges)拓扑排序
思路:
首先考虑给定的有向边构成的图就存在环的话,输出NO。
其他情况则一定可以有合法的方案,输出YES,然后先拿给定的那些有向边进行拓扑排序,设(id_i)为节点(mathit i)在拓扑排序中是第几个出队列的(拓扑序的排名)。
然后对于那些无向边,我们只需要让拓扑序小的指向拓扑序大的即可。
证明如下:
对于一个无向边((u,v)),
1、如果((u,v))两节点在原图(只含有有向边的那个图)是联通的:
即两者存在至少一个单向路径,那么我们让该边拓扑序小的节点指向拓扑序大的节点,只是增加了一个和原路径首位端点相同的单向路径,不会产生环。
2、如果((u,v))两节点在原图中不连通,设它们分别在联通块(A,B),那么对于任意(xin A,yin B),([id_x>id_y])(真值表达式)的值都是一样的,所以按照拓扑序确定方向也不会产生环。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ' ', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, m;
pii e[maxn];
int op[maxn];
std::vector<int> v[maxn];
int tot;
int id[maxn];
int du[maxn];
void init() {
repd(i, 1, n)
{
du[i] = 0;
v[i].clear();
}
tot = 0;
}
bool solve()
{
queue<int> q;
int cnt = 0;
repd(i, 1, n)
{
if (du[i] == 0)
{
q.push(i);
cnt++;
}
}
int temp;
while (!q.empty())
{
temp = q.front();
q.pop();
id[temp] = ++tot;
for (auto &y : v[temp])
{
if (--du[y] == 0)
{
q.push(y);
cnt++;
}
}
}
return cnt == n;
}
int main()
{
#if DEBUG_Switch
freopen("C:\code\input.txt", "r", stdin);
#endif
//freopen("C:\code\output.txt","w",stdout);
int t;
t = readint();
while (t--)
{
n = readint();
m = readint();
repd(i, 1, m)
{
op[i] = readint();
int x = readint();
int y = readint();
e[i] = mp(x, y);
if (op[i] == 1)
{
du[y]++;
v[x].push_back(y);
}
}
if (solve())
{
printf("YES
");
repd(i, 1, m)
{
if (op[i] == 0)
{
if (id[e[i].fi] > id[e[i].se])
{
swap(e[i].fi, e[i].se);
}
}
}
repd(i, 1, m)
{
printf("%d %d
", e[i].fi, e[i].se );
}
} else
{
printf("NO
");
}
init();
}
return 0;
}