Hybrid Graph
(Digoal. c/cpp/pas)
[Problem description]
There is a mix of N points, M1 directed edges, and M2 undirected edges. Ask a solution for all undirected edges. So that the final graph does not have a ring. Make sure there is a solution.
[Input format]
The first line contains three numbers: N, M1, and M2.
Next, M1 + M2 rows, each line contains two numbers: Ai and Bi. Indicates an edge.
The first M1 is a directed edge. The direction is from Ai to Bi.
[Output format]
Output M2 rows, which are output in the order of output in a direction not determined by the undirected edge. Ai Bi or Bi Ai.
Any solution is output when multiple solutions exist.
[Example input]
4 2 3
1 2
4 3
1 3
4 2
3 2
[Sample output]
1 3
2 4
2 3
[Data scale]
1 <= N <= 100 000
1 <= M1, M2 <= 100000
Topology Sorting works even with duplicate edges!
Note that the hash table h [u] does not have multiple sets of q [u]...
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cmath>
# Include <cstdlib>
# Include <iostream>
# Include <functional>
# Include <algorithm>
Using namespace std;
# Define MAXN (100000 + 10)
# Define MAXM (100000 + 10)
Int n, m1, m2, indegree [MAXN] = {0}, head [MAXN], next [MAXM] = {0}, edge [MAXM] = {0 }, tot = 0;
Void addedge (int u, int v)
{
Edge [++ tot] = v;
Next [tot] = head [u];
Head [u] = tot;
}
Int q [MAXN * 2];
Bool B [MAXN] = {0 };
Void topsort ()
{
Int head _ = 1, tail = 0;
For (int I = 1; I <= n; I ++)
If (indegree [I] = 0)
{
Q [++ tail] = I; B [I] = 1;
}
While (head _ <= tail)
{
Int now = q [head _];
Int p = head [now];
While (p)
{
Int v = edge [p];
Indegree [v] --;
If (indegree [v] = 0)
{
Q [++ tail] = v; B [v] = 1;
}
P = next [p];
}
Head _ ++;
}
}
Int h [MAXN];
Int main ()
{
Freopen ("digoal. in", "r", stdin );
Freopen ("digoal. out", "w", stdout );
Scanf ("% d", & n, & m1, & m2 );
Memset (head, 0, sizeof (head ));
For (int I = 1; I <= m1; I ++)
{
Int u, v;
Scanf ("% d", & u, & v );
Addedge (u, v );
Indegree [v] ++;
}
Topsort ();
For (int I = 1; I <= n; I ++)
{
H [q [I] = I;
// Cout <q [I] <'';
}
/*
For (int I = 1; I <= n; I ++) cout <q [I] <'';
Cout <endl;
For (int I = 1; I <= n; I ++) cout
Cout <endl;
*/
For (int I = 1; I <= m2; I ++)
{
Int u, v;
Scanf ("% d", & u, & v );
If (h [u]
Else printf ("% d \ n", v, u );
}
// While (1 );
Return 0;
}