目录

1 问题描述

2 解决方案

 


1 问题描述

Description

In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

Input

The first line contains a single integer T, the number of test cases. And followed T cases.

The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.

Output

The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

Sample Input

1
3 3
1 2
2 3
3 1

Sample Output

Yes

Source

 

2 解决方案

具体代码如下:

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 boolean[] inStack;
public static int group; //强连通分量组
public static int[] belong;
public static Stack<Integer> stack;
public static ArrayList<edge>[] map;
public static ArrayList<String> result = new ArrayList<String>(); static class edge {
public int a;
public int b; public edge(int a, int b) {
this.a = a;
this.b = b;
}
} @SuppressWarnings("unchecked")
public void init() {
count = 1;
DFN = new int[n + 1];
Low = new int[n + 1];
inStack = new boolean[n + 1];
group = 0;
belong = new int[n + 1];
stack = new Stack<Integer>();
map = new ArrayList[n + 1];
for(int i = 1;i <= n;i++) {
DFN[i] = -1;
Low[i] = -1;
inStack[i] = false;
belong[i] = -1;
map[i] = new ArrayList<edge>();
}
} public void TarJan(int start) {
DFN[start] = count++;
Low[start] = DFN[start];
inStack[start] = true;
stack.push(start);
int j = start;
for(int i = 0;i < map[start].size();i++) {
j = map[start].get(i).b;
if(DFN[j] == -1) {
TarJan(j);
Low[start] = Math.min(Low[start], Low[j]);
} else if(inStack[j]) {
Low[start] = Math.min(Low[start], DFN[j]);
}
}
if(DFN[start] == Low[start]) {
group++;
do {
j = stack.pop();
belong[j] = group;
inStack[j] = false;
} while(j != start);
}
} public boolean TopSort(ArrayList<edge>[] lessMap, int[] degree) {
int count = 0;
Stack<Integer> s = new Stack<Integer>();
for(int i = 1;i < degree.length;i++) {
if(degree[i] == 0) {
count++;
s.push(i);
}
}
if(count > 1)
return false;
while(!s.empty()) {
int start = s.pop();
count = 0;
for(int i = 0;i < lessMap[start].size();i++) {
int j = lessMap[start].get(i).b;
degree[j]--;
if(degree[j] == 0) {
count++;
s.push(j);
}
}
if(count > 1)
return false;
}
return true;
} @SuppressWarnings("unchecked")
public void getResult() {
for(int i = 1;i <= n;i++) {
if(DFN[i] == -1)
TarJan(i);
}
ArrayList<edge>[] lessMap = new ArrayList[group + 1];
int[] degree = new int[group + 1];
for(int i = 1;i <= group;i++)
lessMap[i] = new ArrayList<edge>();
for(int i = 1;i < map.length;i++) {
for(int j = 0;j < map[i].size();j++) {
int a = map[i].get(j).a;
int b = map[i].get(j).b;
if(belong[a] != belong [b]) {
lessMap[belong[a]].add(new edge(belong[a], belong[b]));
degree[belong[b]]++;
}
}
}
if(TopSort(lessMap, degree)) {
result.add("Yes");
} else {
result.add("No");
}
return;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t > 0) {
t--;
n = in.nextInt();
int k = in.nextInt();
test.init();
for(int i = 0;i < k;i++) {
int a = in.nextInt();
int b = in.nextInt();
map[a].add(new edge(a, b));
}
test.getResult();
}
for(int i = 0;i < result.size();i++)
System.out.println(result.get(i));
}
}

运行结果:

1
3 3
1 2
2 3
3 1
Yes

参考资料:

1. 欧拉回路

2. POJ2762 Going from u to v or from v to u?(强连通分量缩点+拓扑排序)

算法笔记_147:有向图欧拉回路判断应用(Java)的更多相关文章

  1. 算法笔记_148:有向图欧拉回路求解(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 Description A catenym is a pair of words separated by a period such that t ...

  2. 算法笔记_030:回文判断(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 给定一个字符串,如何判断这个字符串是否是回文串? 所谓回文串,是指正读和反读都一样的字符串,如madam.我爱我等. 2 解决方案 解决上述问题,有 ...

  3. 算法笔记_144:有向图强连通分量的Tarjan算法(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...

  4. 算法笔记_177:历届试题 城市建设(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 栋栋居住在一个繁华的C市中,然而,这个城市的道路大都年久失修.市长准备重新修一些路以方便市民,于是找到了栋栋,希望栋栋能帮助他. C市中有 ...

  5. 算法笔记_135:格子取数问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 有n*n个格子,每个格子里有正数或者0,从最左上角往最右下角走,只能向下和向右走,一共走两次(即从左上角往右下角走两趟),把所有经过的格子里的数加起 ...

  6. 算法笔记_045:币值最大化问题(Java)

    目录 1 问题描述 2 解决方案 2.1 动态规划法   1 问题描述 给定一排n个硬币,其面值均为正整数c1,c2,...,cn,这些整数并不一定两两不同.请问如何选择硬币,使得在其原始位置互不相邻 ...

  7. 算法笔记_029:约瑟夫斯问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 引用自<算法设计与分析基础>第三版: 约瑟夫斯问题,是以弗拉瓦斯.约瑟夫斯(Flavius Josephus)的名字命名的.约瑟夫斯是一 ...

  8. 算法笔记_024:字符串的包含(Java)

    目录 1 问题描述 2 解决方案 2.1 蛮力轮询法 2.2 素数相乘法 2.3 位运算法 1 问题描述 给定一长字符串A和一短字符串B.请问,如何最快地判断出短字符串B中的所有字符是否都在长字符串A ...

  9. 算法笔记_051:荷兰国旗问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换任意两个球,使得从左至右的球依次为红球.白球.蓝球.这个问题之所以叫荷兰国旗,是因为 ...

随机推荐

  1. FFTW3学习笔记1:VS2015下配置FFTW3(快速傅里叶变换)库

    一.FFTW简介 FFTW ( the Faster Fourier Transform in the West) 是一个快速计算离散傅里叶变换的标准C语言程序集,其由MIT的M.Frigo 和S. ...

  2. 2017-2018-1 JAVA实验站 冲刺 day06

    2017-2018-1 JAVA实验站 冲刺 day06 各个成员今日完成的任务 小组成员 今日工作 完成进度 张韵琪 进行工作总结.博客.小组成员头像 100% 齐力锋 找背按钮声音 100% 张浩 ...

  3. HDU 5575 Discover Water Tank 并查集 树形DP

    题意: 有一个水槽,边界的两块板是无穷高的,中间有n-1块隔板(有高度),现有一些条件(i,y,k),表示从左到右数的第i列中,在高度为(y+0.5)的地方是否有水(有水:k = 1),问最多能同时满 ...

  4. 常用 iOS 开源库和第三方组件

    1.通过CocoaPods安装 项目名称 项目信息 AFNetworking 网络请求组件 FMDB 本地数据库组件 SDWebImage 多个缩略图缓存组件 UICKeyChainStore 存放用 ...

  5. Android 按钮长按下去重复执行某个动作,放开后停止执行动作

    Android开发中,常遇到一种需求,即按钮长按下去重复执行某个动作,放开后停止执行动作.网上找了许多代码,都没有适合的,于是自己动手写了一个. 基本思路是:首先设置一个标识变量,用于标识是否处于按下 ...

  6. WIFI模块 RTL8188EUS Realtek

    http://item.taobao.com/item.htm?spm=a230r.1.14.24.KnooKa&id=26119704895 W12 产品是一款采用国际先进台湾瑞昱Realt ...

  7. Android开发:ListView加上长按事件

    为ListView加上长按事件 lvMain.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public b ...

  8. SQLiteOpenHelper 操作不成功

    SDK和ADT为22.6.2版本号 project为4.4.2 今天在练习SQLiteOpenHelper里,使用的是三个JAVA文件操作.DatabaseHelper.java,Const.java ...

  9. java读写锁实现数据同步访问

    锁机制最大的改进之一就是ReadWriteLock接口和它的唯一实现类ReentrantReadWriteLock.这个类有两个锁,一个是读操作锁,另一个是写操作锁.使用读操作锁时可以允许多个线程同时 ...

  10. 搞定android多点触摸模拟

    原理在android 创建多点触摸虚拟设备,然后往设备写模拟数据可以