Uva610-street directions ctions (Tarjan)

Source: Internet
Author: User

Question Link


Question: Give an undirected graph and make as many edges as possible as one-way edges. The changed graph is still strongly connected.

Train of Thought: Find out all the bridges, the bridge must not be changed to one-way edge. After that, not the bridge side can form N connected blocks and define the direction in the order of DFS.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <utility>#include <algorithm>using namespace std;const int MAXN = 10005;struct Edge{    int to, next;    bool cut, vis;}edge[MAXN * 10];int head[MAXN], tot;int Low[MAXN], DFN[MAXN];int Index, top;int bridge;vector<pair<int, int> > ans; void addedge(int u, int v) {    edge[tot].to = v;    edge[tot].next = head[u];    edge[tot].vis = false;    head[u] = tot++;}void Tarjan(int u, int pre) {    int v;    Low[u] = DFN[u] = ++Index;    for (int i = head[u]; i != -1; i = edge[i].next) {        v = edge[i].to;        if (v == pre) continue;         if (edge[i].vis) continue;        edge[i].vis = edge[i^1].vis = true;        ans.push_back(make_pair(u, v));        if (!DFN[v]) {            Tarjan(v, u);             if (Low[u] > Low[v]) Low[u] = Low[v];            if (Low[v] > DFN[u]) {                ans.push_back(make_pair(v, u));            }        }         else if (Low[u] > DFN[v])            Low[u] = DFN[v];    }}void init() {    memset(head, -1, sizeof(head));    memset(DFN, 0, sizeof(DFN));     tot = Index = 0;}int main() {    int n, m, t = 1;     while (scanf("%d%d", &n, &m)) {        if (n == 0 && m == 0) break;        init();        int u, v;        for (int i = 0; i < m; i++) {            scanf("%d%d", &u, &v);             addedge(u, v);             addedge(v, u);          }        ans.clear();        for (int i = 1; i <= n; i++) {            if (!DFN[i])                Tarjan(i, i);        }        printf("%d\n\n", t++);        for (int i = 0; i < ans.size(); i++)             printf("%d %d\n", ans[i].first, ans[i].second);        printf("#\n");    }    return 0;}



Uva610-street directions ctions (Tarjan)

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.