Graph theory everyday does not series ...
Test instructions: Given directed and non-directed edges, and then directed to each non-directional edge, so that the resulting graph is not ring.
I wanted to shrink a little bit, but the more I wanted to faint.
Then he turned the puzzle and suddenly ...
In fact, only need to give a picture of the edge to run once toposort. The orientation of the non-directional side is then considered to be the addition of a directed edge.
Obviously do not violate the topological order to add a forward edge, this diagram can not have a ring!
So just do it.
Code:
#include <cstdio> #include <queue>const int maxn = 100005;struct edges{int next, to;} E[MAXN * 3];int Head[ma XN], tot;int indegree[maxn];int TOPO[MAXN], ttot;int where[maxn];struct query{int u, v;} Q[maxn];int N, M1, M2;int read () {int ans = 0, s = 1; char ch = getchar (); while (Ch > ' 9 ' | | ch < ' 0 ') {if (ch = = '-') s =-1; ch = GetChar ();} while (Ch >= ' 0 ' && ch <= ' 9 ') ans = (ans << 3) + (ans << 1) + CH-' 0 ', ch = getchar (); return s * ans;} void link (int u, int v) {E[++tot] = (Edges) {Head[u], v}; Head[u] = tot;} void Toposort () {std::queue<int> q; for (int i = 1; I <= n; i++) if (indegree[i] = = 0) Q.push (i); while (!q.empty ()) {int u = q.front (); Q.pop (); Topo[++ttot] = u; for (int i = head[u]; i; i = e[i].next) {int v = e[i].to; indegree[v]--; if (indegree[v] = = 0) {Q.push (v); }}}}int Main() {n = read (), m1 = Read (), M2 = read (); while (m1--) {int u = read (), V = read (); Link (u, v); indegree[v]++; } for (int i = 1; I <= m2; i++) {q[i].u = Read (), Q[I].V = Read (); } toposort (); for (int i = 1; I <= ttot; i++) where[topo[i]] = i; for (int i = 1; I <= m2; i++) {int temp1 = where[q[i].u], temp2 = where[q[i].v]; if (Temp1 < TEMP2) printf ("%d%d\n", q[i].u, Q[I].V); else printf ("%d%d\n", Q[I].V, q[i].u); } return 0;}
P2017 [Usaco09dec] halo ox Dizzy Cows