算法笔记_149:图论之桥的应用(Java)
目录
1 问题描述
1310 One-way traffic
In a certain town there are n intersections connected by two- and one-way streets. The town is very
modern so a lot of streets run through tunnels or viaducts. Of course it is possible to travel between
any two intersections in both ways, i.e. it is possible to travel from an intersection a to an intersection
b as well as from b to a without violating traffic rules. Because one-way streets are safer, it has been
decided to create as much one-way traffic as possible. In order not to make too much confusion it has
also been decided that the direction of traffic in already existing one-way streets should not be changed.
Your job is to create a new traffic system in the town. You have to determine the direction of
traffic for as many two-way streets as possible and make sure that it is still possible to travel both ways
between any two intersections.
Write a program that:
• reads a description of the street system in the town from the standard input,
• for each two-way street determines one direction of traffic or decides that the street must remain
two-way,
• writes the answer to the standard output.
Input
The first line of the input contains two integers n and m, where 2 ≤ n ≤ 2000 and n−1 ≤ m ≤ n(n−1)/2.
Integer n is the number of intersections in the town and integer m is the number of streets.
Each of the next m lines contains three integers a, b and c, where 1 ≤ a ≤ n, 1 ≤ b ≤ n, a ̸= b and
c belongs to {1, 2}. If c = 1 then intersections a and b are connected by an one-way street from a to
b. If c = 2 then intersections a and b are connected by a two-way street. There is at most one street
connecting any two intersections.
Output
The output contains exactly the same number of lines as the number of two-way streets in the input.
For each such street (in any order) the program should write three integers a, b and c meaning, the new
direction of the street from a to b (c = 1) or that the street connecting a and b remains two-way (c = 2).
If there are more than one solution with maximal number of one-way streets then your program should
output any of them but just one.
Sample Input
4 4
4 1 1
4 2 2
1 2 1
1 3 2 Sample Output
2 4 1
3 1 2
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4056
2 解决方案
首先看看关于图论的割点和桥的相关概念定义:
引用文末参考资料1:
1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点。
2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合。
3.点连通度:最小割点集合中的顶点数。
4.割边(桥):删掉它之后,图必然会分裂为两个或两个以上的子图。
5.割边集合:如果有一个边集合,删除这个边集合以后,原图变成多个连通块,就称这个点集为割边集合。
6.边连通度:一个图的边连通度的定义为,最小割边集合中的边数。
7.缩点:把没有割边的连通子图缩为一个点,此时满足任意两点之间都有两条路径可达。
注:求块<>求缩点。缩点后变成一棵k个点k-1条割边连接成的树。而割点可以存在于多个块中。
8.双连通分量:分为点双连通和边双连通。它的标准定义为:点连通度大于1的图称为点双连通图,边连通度大于1的图称为边双连通图。通俗地讲,满足任意两点之间,能通过两条或两条以上没有任何重复边的路到达的图称为双连通图。无向图G的极大双连通子图称为双连通分量。
具体代码如下:
package com.liuzhen.practice; import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack; public class Main {
public static int n; //给定图的顶点数
public static int count; //记录遍历次序
public static int[] DFN;
public static int[] Low;
public static int[] parent; //parent[i] = j,表示顶点i的直接父母顶点为j
public static Stack<Integer> stack;
public static ArrayList<edge>[] map;
public static ArrayList<edge> ans; //存储最终输出结果 static class edge {
public int a; //边的起点
public int b; //边的终点
public int c; //c = 1表示单向边,c = 2表示双向边 public edge(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
} @SuppressWarnings("unchecked")
public void init() {
count = 0;
DFN = new int[n + 1];
Low = new int[n + 1];
parent = new int[n + 1];
stack = new Stack<Integer>();
map = new ArrayList[n + 1];
ans = new ArrayList<edge>();
for(int i = 1;i <= n;i++) {
DFN[i] = -1;
Low[i] = -1;
parent[i] = -1;
map[i] = new ArrayList<edge>();
}
} public void TarJan(int start, int father) {
DFN[start] = count++;
Low[start] = DFN[start];
parent[start] = father;
stack.push(start);
for(int i = 0;i < map[start].size();i++) {
edge temp = map[start].get(i);
int j = 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]) { //当边temp为割边(或者桥)时
ans.add(temp);
} else {
ans.add(new edge(temp.a, temp.b, 1));
}
}
} else if(j != parent[start]) { //当j不是start的直接父母节点时
Low[start] = Math.min(Low[start], DFN[j]);
if(temp.c == 2) {
ans.add(new edge(temp.a, temp.b, 1));
}
}
}
} public void getResult() {
for(int i = 1;i <= n;i++) {
if(parent[i] == -1)
TarJan(i, 0);
}
for(int i = 0;i < ans.size();i++)
System.out.println(ans.get(i).a+" "+ans.get(i).b+" "+ans.get(i).c);
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
n = in.nextInt();
int k = in.nextInt();
test.init();
for(int i = 0;i < k;i++) {
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
map[a].add(new edge(a, b, c));
if(c == 2)
map[b].add(new edge(b, a, c));
}
test.getResult();
}
}
运行结果:
4 4
4 1 1
4 2 2
1 2 1
1 3 2
2 4 1
1 3 2
参考资料:
2. uva 1310 - One-way traffic(连通分量)
算法笔记_149:图论之桥的应用(Java)的更多相关文章
- 算法笔记_150:图论之双连通及桥的应用(Java)
目录 1 问题描述 2 解决方案 1 问题描述 Description In order to get from one of the F (1 <= F <= 5,000) graz ...
- 算法笔记_180:历届试题 国王的烦恼(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 C国由n个小岛组成,为了方便小岛之间联络,C国在小岛间建立了m座大桥,每座大桥连接两座小岛.两个小岛间可能存在多座桥连接.然而,由于海水冲 ...
- 算法笔记_139:二分图的最大权匹配(Java)
目录 1 问题描述 2 解决方案 1 问题描述 何为二分图的最大权匹配问题? 最大权二分匹配问题就是给二分图的每条边一个权值,选择若干不相交的边,得到的总权值最大. 2 解决方案 对于此问题的讲解 ...
- 算法笔记_133:最大连续乘积子数组(Java)
目录 1 问题描述 2 解决方案 2.1 蛮力法 2.2 动态规划法 1 问题描述 给定一个浮点数组,任意取出数组中的若干个连续的数相乘,请找出其中乘积最大的子数组. 2 解决方案 2.1 蛮力法 ...
- 算法笔记_131:出现次数超过一半的数(Java)
目录 1 问题描述 2 解决方案 2.1 每次删除两个不同的数 2.2 记录两个值 1 问题描述 数组中有一个数出现的次数超过了数组长度的一半,请找出这个数. 2 解决方案 2.1 每次删除两个不 ...
- 算法笔记_044:表达式计算求值(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的 ...
- 算法笔记_052:蓝桥杯练习Multithreading(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 现有如下一个算法: repeat ni times yi := y y := yi+1 end repeat 令n[1]为你需要算加法的第 ...
- 算法笔记_013:汉诺塔问题(Java递归法和非递归法)
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...
- 算法笔记_198:历届试题 打印十字图(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示: ..$$$$$$$$$$$$$....$...........$..$$ ...
随机推荐
- 可以在GitHub或者码云里 直接搜索 项目 比如 哔哩哔哩
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha Search · 哔哩哔哩 哔哩哔哩 · 搜索 - 码云 还有就是 以前的项目 可以不要 ...
- FFTW3学习笔记3:FFTW 和 CUFFT 的使用对比
一.流程 1.使用cufftHandle创建句柄 2.使用cufftPlan1d(),cufftPlan3d(),cufftPlan3d(),cufftPlanMany()对句柄进行配置,主要是配置句 ...
- 【轮廓线DP】POJ2411-Mondriaan's Dream
今天美国的院士过来讲课XD以为会很无聊但是谜之好听,而且英语基本上都听懂了的样子♪(´▽`) 逃到图书馆来写解题报告 [题目大意] 给出一个m*n的方格,用2*1的骨牌覆盖有几种情况. [思路] 最基 ...
- 【树上主席树】BZOJ2588-Count on a tree
[题目大意] 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第 ...
- [SimpleOJ236]暴风雨
题目大意: 给你一棵n个点的树,以及m+q条信息. m条描述点a到b有边直接相连. q条描述点a和点b的LCA为c. 问有多少符合条件的以1为根的树. 思路: 状压DP. e[i]记录需要与点i直接相 ...
- uboot启动内核的实现
前面我们分析了uboot 的整个流程,我们知道uboot启动以后所有功能都是通过命令来实现的,启动kernel就是执行了bootcmd里面的命令.命令执行过程在uboot中是非常重要的现在我们就来看u ...
- Ubuntu下deb包的安装方法 - kevinhg的博客 - 博客频道 - CSDN.NET
Ubuntu下deb包的安装方法 - kevinhg的博客 - 博客频道 - CSDN.NET dpkg -i
- XuLA/XuLA2
http://www.xess.com/prods/prod048.php XuLA http://www.xess.com/prods/prod055.php XuLA2 http://www.xe ...
- 【iOS开发-图层】图层的获取与一些属性
图层 每个控件都有自己的一个图层,这个空间的全部东西就是显示在这个图层上面的,控件本身没有显示东西的功能,仅仅有拥有了图层才干显示东西 图层的获取 CALayer *layer = self.test ...
- visual studio 2008试用版的评估期(万能破解)
教程 http://jingyan.baidu.com/article/a3a3f811ee87268da2eb8ae7.html 参考: http://blog.chinaunix.net/uid- ...