P2017 [Usaco09dec] halo ox Dizzy Cows

Source: Internet
Author: User

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

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.