Algorithm Note _149: Application of Graph theory Bridge (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description

1310 one-The traffic
In a certain town there is n intersections connected by-and one-The streets. The town was verymodern so a lot of streets run through tunnels or viaducts. Of course it is possible to travel betweenany the intersections in both ways, i.e. it's possible to travel from an inters Ection A to the intersectionb as well as from B to a without violating traffic rules. Because One-the streets is safer, it has beendecided to create as much one-The traffic as possible. In order does too much confusion it hasalso been decided that the direction of traffic in already existing one-The streets should not being changed. Your job is to create aNewtraffic system in the town. you had to determine the direction oftraffic forAs many two-The streets as possible and make sure, that it's still possible to travel both waysbetween any and intersections. Write a program that:? Reads a description of the the street system in the town from the standard input,? forEach two-The street determines one direction of traffic or decides that the street must remaintwo-the writes the answer to the standard output.
Inputthe first line of the input contains, integers n and m, where2≤n≤2000 and N?1≤m≤n (n?1)/2. Integer n is the number of intersections in the town and an integer m is the number of streets. Each of the next m lines contains three integers a, B and C, where1≤a≤n, 1≤b≤n, a? =b Andc belongs to {1, 2}. If C = 1 Then intersections A and B is connected by an one-The on -the-street from a tob. If C= 2 then intersections A and B is connected by a two-That's the street. There is at the most one streetconnecting any and intersections.
Outputthe output contains exactly the same number of lines as the number of-The streets in the input. For each such street (on any order) the program should write three integers a, B and C meaning, theNewdirection of the street from A to B (C= 1) or that the street connecting A and B remains two-way (c = 2). If there is more than one solution with maximal number of one-The streets then your program shouldoutput any of the them but just one.
Sample Input4 44 1 14 2 21 2 11 3 2

Sample Output2 4 13) 1 2

title Link:Https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem &problem=4056

2 Solutions

First look at the definition of concepts related to the cut point and Bridge of graph theory:

Reference at the end of this article 1:

1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点。

2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合。

3.点连通度:最小割点集合中的顶点数。

4.割边(桥):删掉它之后,图必然会分裂为两个或两个以上的子图。

5.割边集合:如果有一个边集合,删除这个边集合以后,原图变成多个连通块,就称这个点集为割边集合。

6.边连通度:一个图的边连通度的定义为,最小割边集合中的边数。

7.缩点:把没有割边的连通子图缩为一个点,此时满足任意两点之间都有两条路径可达。

注:求块<>求缩点。缩点后变成一棵k个点k-1条割边连接成的树。而割点可以存在于多个块中。

8.双连通分量:分为点双连通和边双连通。它的标准定义为:点连通度大于1的图称为点双连通图,边连通度大于1的图称为边双连通图。通俗地讲,满足任意两点之间,能通过两条或两条以上没有任何重复边的路到达的图称为双连通图。无向图G的极大双连通子图称为双连通分量。

The specific code is as follows:

 PackageCom.liuzhen.practice;Importjava.util.ArrayList;ImportJava.util.Scanner;ImportJava.util.Stack; Public classMain { Public Static intN//the number of vertices for a given graph     Public Static intCount//Record Traversal order     Public Static int[] DFN;  Public Static int[] low;  Public Static int[] parent;//Parent[i] = j, which indicates the direct parent vertex of Vertex i is J     Public StaticStack<integer>Stack;  Public StaticArraylist<edge>[] map;  Public StaticArraylist<edge> ans;//Store final output results        Static classEdge { Public intA//the beginning of the side         Public intb//the end of the edge         Public intC//C = 1 represents a one-way edge, and C = 2 indicates a bidirectional edge                 PublicEdgeintAintBintc) { This. A =A;  This. B =b;  This. C =C; }} @SuppressWarnings ("Unchecked")     Public voidInit () {count= 0; DFN=New int[n + 1]; Low=New int[n + 1]; Parent=New int[n + 1]; Stack=NewStack<integer>(); Map=NewArraylist[n + 1]; Ans=NewArraylist<edge>();  for(inti = 1;i <= n;i++) {Dfn[i]=-1; Low[i]=-1; Parent[i]=-1; Map[i]=NewArraylist<edge>(); }    }         Public voidTarjan (intStartintfather) {Dfn[start]= count++; Low[start]=Dfn[start]; Parent[start]=father;        Stack.push (start);  for(inti = 0;i < Map[start].size (); i++) {Edge temp=Map[start].get (i); intj =temp.b; if(Dfn[j] = =-1) {Tarjan (J, start); Low[start]=math.min (Low[start], low[j]); if(Temp.c = = 2) {                    if(Low[j] > Dfn[start]) {//When the side temp is the cut edge (or bridge)Ans.add (temp); } Else{Ans.add (NewEdge (Temp.a, TEMP.B, 1)); }                }            } Else if(J! = Parent[start]) {//When J is not the direct parent node of startLow[start] =math.min (Low[start], dfn[j]); if(Temp.c = = 2) {Ans.add (NewEdge (Temp.a, TEMP.B, 1)); }            }        }    }         Public voidGetResult () { for(inti = 1;i <= n;i++) {            if(Parent[i] = =-1) Tarjan (i,0); }         for(inti = 0;i < Ans.size (); i++) System.out.println (Ans.get (i). a+ "" +ans.get (i). B + "" +Ans.get (i). c); }         Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); N=In.nextint (); intK =In.nextint ();        Test.init ();  for(inti = 0;i < k;i++) {            intA =In.nextint (); intb =In.nextint (); intc =In.nextint (); Map[a].add (NewEdge (A, B, c)); if(c = = 2) Map[b].add (NewEdge (b, A, c));    } test.getresult (); }}

Operation Result:

4 44 1 14 2 21 2 11 3 22 4 11 3 2

Resources:

1. Graph theory-bridge/Cut point/double connected component/Pinch point/lca

2. UVA 1310-one-way Traffic (connected components)

3."graph theory" to find the cut point of undirected connected graph





Algorithm Note _149: Application of Graph theory Bridge (Java)

Related Article

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.