UVA - 796 Critical Links (tarjan, undirected graph bridge)

The meaning of problems: given an undirected graph, then you want to lose all the ascending output of the bridge of FIG.

Ideas: Use tarjan method for finding the nature of the bridge:

If and only if no directed edge (u, v) of the branches, the need to satisfy dfn (u) <low (v), v is not turned up and above the point u, then it must be able to have between the uv one or more edges can not be omitted, because no portion of the ring between them, is a bridge.            

If u then v can turn on one of the rings is uv, delete a path, it can then be connected. So most of the total is to use low (v)> dfn (u) to determine whether the bridge

Complete code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
typedef long long LL;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;const int maxn = 1e3+10;
const int max_edge = 1e6+5;
struct Edge
{
    intto, Next;
     BOOL Cut; 
} Edge [max_edge]; 
int TOT, head [MAXN];
 int ID, DFN [MAXN], Low [MAXN];
 int NUM;
 void addedge ( int U, int V) 
{ 
    Edge [TOT ] .to = V; 
    edge [TOT] .next = head [U]; 
    edge [TOT] .cut = to false ; 
    head [U] = TOT ++ ; 
} 
// Tarjan method for finding the cutting edge 
void Tarjan ( int U, int F ) 
{ 
    // timestamp update
    DFN [U] = Low [U] ++ = ID;
     for ( int I = head [U]; I = -! . 1 ; I = Edge [I] .next) 
    { 
        int V = Edge [I] .to;
         IF (V == F) Continue ;
         // deep search have been updated low value 
        IF (! DFN [V]) 
        { 
            Tarjan (V, U); 
            low [U] = min (low [U], low [V]) ;
             // Tarjan algorithm cutting edge definitive conclusions low [v]> dfn [u ] ( bridge> cutpoint> =) 
            IF (Low [V]> DFN [U]) 
            { 
         // because no edge is connected to FIG. the stored, so as to give a ^ replaced with another side edge to (0 1 = 1 ^, 1 ^ 1 = 0), see the internet written big brother
         // Of course, we can also use the odd round down even, even-odd rounded up 
Edge [I] .cut
= Edge [I ^ . 1 ] = .cut to true ; NUM ++ ; } } the else Low [U] = min (Low [U], DFN [V]); } } void the init () { ID = NUM = TOT = 0 ; Memset (head, - . 1 , the sizeof (head)); Memset (DFN, 0 , the sizeof (DFN)); Memset (Low, 0 , the sizeof (Low)); } int main() { int n; while(~scanf("%d", &n)) { init(); int u, m, v; for(int i = 1; i<=n; i++) { scanf("%d (%d)", &u, &m); for(int j = 1; j<=m; j++) { scanf("%d", &v); addedge(u, v); addedge(v, u); } } for(int i = 0; i<n; i++) if(!dfn[i]) tarjan(i, i); vector<pair<int, int> >a; for(int u = 0; u<n; u++) for(int i = head[u]; i!=-1; i = edge[i].next) { //按升序排列 if(edge[i].cut && u < edge[i].to) a.push_back(make_pair(u, edge[i].to)); } sort(a.begin(), a.end()); printf("%d critical links\n", num); for(int i = 0; i<a.size(); i++) printf("%d - %d\n", a[i].first, a[i].second); printf("\n"); } }

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11317010.html