https://www.luogu.org/problem/P2017
Topic background
Hzwer God Ben recently conquered a country, then the next, but also met a problem.
Title Description
The cows have taken to racing each other around the farm but they get very dizzy when running in circles, and everyone knows that dizzy cows don't produce any milk. Farmer John wants to convert all of the two-way cow paths in the farm to one-way paths in order to eliminate any 'cycles' and prevent the cows from getting dizzy. A 'cycle' enables a cow to traverse one or more cow paths and arrive back at her starting point, thus completing a loop or circle.
The farm comprises N pastures (1 <= N <= 100,000) conveniently numbered 1..N. M1 (1 <= M1 <= 100,000) one-way cow paths and M2 two-way cow paths (1 <= M2 <= 100,000) connect the pastures. No path directly connects a pasture to itself, although multiple paths might connect two different pastures. A cow may or may not be able to travel between any two given pastures by following a sequence of cow paths.
Your job is to assign a direction to the two-way cow paths such that the entire farm (ultimately with only one-way paths) has no cycles. That is, there should be no sequence of one-way cow paths which leads back to its starting position. The existing one-way cow paths do not form a cycle and should be left as they are.
One-way cow paths run from pasture A_i (1 <= A_i <= N) to pasture B_i (1 <= B_i <= N). Two-way cow paths connect pastures X_i (1 <= X_i <= N) and Y_i (1 <= Y_i <= N).
Consider this example:
1-->2 | /| | / | |/ | 3<--4
The cow paths between pastures 1 and 3, 2 and 3, and 2 and 4 are two-way paths. One-way paths connect 1 to 2 and also 4 to 3. One valid way to convert the two-way paths into one-way paths in such a way that there are no cycles would be to direct them from 1 to 3, from 2 to 3, and from 3 to 4:
1-->2 | /| | / | vL v 3<--4
Cows found on the farm which race is very interesting thing. But once they constantly circling inside the farm, it will become dizzy. As we all know, is not vertigo cows milk production. So, Farmer John wants his farm inside the two-way road all changed one-way road, make his farm inside a circle are not, in order to avoid his cows were hangs up. If the number of cows can pass the road back to the starting point, then the road to called a circle.
Farm N (1 <N <100000) meadow, numbered 1 to N. The grasslands are connected by unidirectional path M1 and M2 bidirectional road any road and will not consider this a meadow grass itself connected up. However, any number of roads are likely connection between the two grass. there is no guarantee that any connected path between two grass.
Your job is to give all two-way road to set a direction such that the entire farm (only one-way road) the last lap at all. That is, there is no starting point and end point of a sequence of one-way road coincides ensure data one-way road outset, one circle at all. and the outset of one-way roads can not be changed.
The starting point is a one-way road grass Ai, end meadows Bi. Two-way road connecting meadows Xi, Yi
Input Format
The input data from dizzy.in
The first line of three integers n, p1, p2, respectively represent points, the number of directed edges, the number of free edges.
P1 second row from the input lines of two integers, a, b, there is a to b represents a directed edge.
Next p2 input lines of two integers a, b, a and b represents the middle of a undirected edges.
Output Format
Output in the dizzy.out
For each undirected edges, we ask that you output directional input order, that is, if you output a'b, that
A and b represent you intermediate undirected edges oriented to a → b.
Note that many feasible solutions may exist. You just like the output of either one.
(Note: because there is no spj, we guarantee to make answer according to conventional methods will be able to AC)
Sample input and output
Input # 1
4 2 3 1 2 4 3 1 3 4 2 3 2
Output # 1
1 3 4 2 2 3
Meaning of the questions:
You have to give a mixed FIG undirected, undirected ask you oriented edges, such that no ring of FIG.
answer:
This is a question about topological sorting.
If there is not a directed graph ring, then it can be topologically sorted, because the premise of topological sorting is required to sort map must be DAG.
We look at this question of the requirements, you will find, When you put all the way into a two-way side edge, this figure it must be a DAG, otherwise I'll no solution.
So we just have to come in first reading side, topological sorting, you get a sequence
We know that if a point behind the other point of order, and if we add the points back side is clearly unreasonable, may have a ring, so we each read a undirected edges when we judge this point in topological sorting size inside position.
So brief is to:
First Sort topological sort to have sides, record number, and undirected edges oriented in the order of magnitude of the sorting SN
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #include <map> 11 #include <math.h> 12 const int INF=0x3f3f3f3f; 13 typedef long long LL; 14 const int mod=1e9+7; 15 const double PI=acos(-1); 16 const int maxn=100010; 17 using namespace std; 18 //ios::sync_with_stdio(false); 19 // cin.tie(NULL); 20 21 struct Edge 22 { 23 int u; 24 int v; 25 int next; 26 }edge[maxn]; 27 28 int n,e1,e2,u,v; 29 inthead [MAXN]; 30 int in [MAXN]; // penetration 31 is int OD [MAXN]; // order topological sort, used to determine the 32 int cnt_edge; 33 is int CNT; // topological sort of the counter 34 is Queue < int > QE; 35 36 void add_edge member ( int U, int V) 37 [ { 38 is Edge [cnt_edge ++] = .next head [U]; 39 Edge [cnt_edge] .u = U; 40 Edge [cnt_edge] .v = V; 41 is head [U] =cnt_edge; 42 } 43 44 int main() 45 { 46 //freopen("sample.txt","r",stdin); 47 scanf("%d %d %d",&n,&e1,&e2); 48 for(int i=0;i<e1;i++) 49 { 50 scanf("%d %d",&u,&v); 51 add_edge(u,v); 52 in[v]++; 53 } 54 for(int= I . 1 ; I <= n-; I ++) // will enter into force is 0 55 { 56 is IF ( in [I] == 0 ) 57 is { 58 qe.push (I); 59 OD [I] = ++ CNT; 60 } 61 is } 62 is the while (qe.empty ()!) // topological sort 63 is { 64 U = qe.front (); 65 qe.pop (); 66 for ( int I = head [U] ; I; I = Edge [I] .next) 67 { 68 in[edge[i].v]--; 69 if(!in[edge[i].v]) 70 { 71 qe.push(edge[i].v); 72 od[edge[i].v]=++cnt; 73 } 74 } 75 } 76 for(int i=1;i<=e2;i++)//判断无向边的方向 77 { 78 scanf("%d %d",&u,&v); 79 if(od[u]<od[v]) 80 printf("%d %d\n",u,v); 81 else 82 printf("%d %d\n",v,u); 83 } 84 return 0; 85 }