POJ3177 redundant Paths "side double Unicom Component" "Tarjan"

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=3177


Main topic:

Bessie's Farm has an F-block ranch, which is known to have at least one path connected to any of the two farms (not necessarily directly connected)

To move from one ranch to another, Bessie and her companions often need to go through the decaying woods. The cows were particularly

Disgusted by the bad way, so Bessie decided to build a few more roads on the farm, so that when going to a place can always have two

A completely independent way to choose. So here's the question: F-Block Ranch, R-Road, ask at least a few more roads to make the farm

There are at least two independent paths between any of the two ranches.


Ideas:

In order to have at least two independent paths between any of the two pastures in the farm. That is, the F-Ranch as a point, R-Bar.

The road is considered to be a two-way street between two farms. Then the problem is converted to: add at least a few edges, you can make the graph does not exist cutting edge.

That is, the same problem as POJ3352: add at least a few edges, so that any two edge connected components are connected to each other.

Specific Practice Reference Blog: http://blog.csdn.net/lianai911/article/details/43867789

Note: This problem is a bit more than the POJ3352, need to judge the weight ... So I used the map[][] array.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    std;const int MAXN = 5050;const int maxm = 50500;bool map[maxn][maxn];struct edgenode{int to; int next;} Edges[maxm];int head[maxn];int dfn[maxn],low[maxn],stack[maxn],degree[maxn];int m,id,scc,lay,M,N;void AddEdges (int    U,int v) {edges[id].to = v;    Edges[id].next = Head[u]; Head[u] = id++;}    void Tarjanbfs (int pos,int from) {//stack[m++] = pos;    Dfn[pos] = Low[pos] = ++lay;        for (int i = Head[pos]; I! =-1; i = edges[i].next) {if (edges[i].to = = from) continue;            if (!dfn[edges[i].to]) {TARJANBFS (edges[i].to,pos);                Low[pos] = min (low[pos],low[edges[i].to])//if (Dfn[pos] < low[edges[i].to])//{// while (stack[m--]! = edges[i].to);/}} else Low[pos] = min (low[pos],low[edges[i].t    O]);    }}int Main () {int u,v; while (~SCANF ("%d%d ", &n,&m)) {memset (head,-1,sizeof (Head));        memset (dfn,0,sizeof (DFN));        memset (low,0,sizeof (Low));        memset (degree,0,sizeof (degree));        m = Lay = SCC = id = 0;        memset (map,0,sizeof (MAP));            for (int i = 0; i < M; ++i) {scanf ("%d%d", &u,&v);                if (map[u][v] = = 0) {addedges (u,v);                Addedges (V,u);            MAP[U][V] = Map[v][u] = 1;        }} tarjanbfs (1,-1);                for (int i = 1, i <= N; ++i) {for (int j = head[i]; J! =-1; j = edges[j].next) {            if (low[i]! = low[edges[j].to]) degree[low[i]]++;        }} int ans = 0;        for (int i = 1; I <= N; ++i) if (degree[i] = = 1) ans++;    printf ("%d\n", (ans+1)/2); } return 0;}


POJ3177 redundant Paths "side double Unicom Component" "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.