Hybrid graph (h [u] is mistakenly written as h [q [u]…)

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.