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

中文说明

为了让他们的儿子勇敢,嘉嘉和风把他们带到一个大山洞。这个洞穴有N个房间,还有连接一些房间的单向走廊。每次,风都会选择两个房间x和y,并要求他们的一个小儿子从一个房间到另一个房间。儿子可以从X到Y,也可以从Y到X。Wind承诺她的任务都是可能的,但实际上她不知道如何决定任务是否可能。为了让她的生活更轻松,贾佳决定选择一个洞穴,在那里每对房间都是一个可能的任务。给定一个山洞,你能告诉佳佳风是否可以随意选择两个房间而不必担心什么吗?

输入

第一行包含一个整数t,即测试用例的数量。并跟踪T案。

每种情况的第一行包含两个整数n,m(0<n<1001,m<6000),洞穴中房间和走廊的数量。下一条M线分别包含两个整数u和v,表示有一条走廊直接连接u和v房间。

输出

输出应包含T行。如果洞穴具有上述财产,则写“是”,否则写“否”。

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));
}
}

运行结果:

3 3
2
3
1
Yes

Java实现判断单联通(强连通缩点+拓扑排序)Going from u to v or from v to u的更多相关文章

  1. poj 2762 强连通缩点+拓扑排序

    这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...

  2. 【BZOJ2330】糖果(差分约束系统,强连通分量,拓扑排序)

    题意: 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖 ...

  3. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  4. 转 java - 如何判断单链表有环

    转自 https://blog.csdn.net/u010983881/article/details/78896293 1.穷举遍历 首先从头节点开始,依次遍历单链表的每一个节点.每遍历到一个新节点 ...

  5. 【Luogu P3387】缩点模板(强连通分量Tarjan&拓扑排序)

    Luogu P3387 强连通分量的定义如下: 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶 ...

  6. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

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

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

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

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

  9. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

随机推荐

  1. IOS App打包发布完整流程

    注册成为开发者 登录苹果开发者中心,点击Accounts,在这里需要你填写你的Appple ID进行登录,如果没有,点击这里申请一个,填写信息就成,这里就不再赘述.申请完成之后,使用申请的AppID进 ...

  2. EOS基础全家桶(十一)智能合约IDE-EOS_Studio

    简介 我们马上要进入智能合约的开发了,以太坊最初提供了智能合约的功能,并宣告区块链进入2.0时代,而EOS的智能合约更进一步,提供了更多的便利性和可能性.为了进一步了解智能合约,并进行开发,我们需要先 ...

  3. Spring全家桶之spring boot(四)

    spring boot拦截器.过滤器.servlet和健康检查机制  spring boot拦截器 spring boot配置拦截器与原来大致相同,只是需要在拦截器的配置类上添加@Configurat ...

  4. 基于Vue搭建自己的组件库(1)

    本项目演示地址:https://husilang.github.io/zm-ui 项目参考文章:从零开始搭建Vue组件库 VV-UI 项目的初衷是学习怎么封装一个基于Vue的UI组件库,顺便记录每个步 ...

  5. Redux:with React(一)

    作者数次强调,redux和React没有关系(明明当初就是为了管理react的state才弄出来的吧),它可以和其他插件如 Angular, Ember, jQuery一起使用.好啦好啦知道啦.Red ...

  6. 聚类算法——DBSCAN算法原理及公式

    聚类的定义 聚类就是对大量未知标注的数据集,按数据的内在相似性将数据集划分为多个类别,使类别内的数据相似度较大而类别间的数据相似度较小.聚类算法是无监督的算法. 常见的相似度计算方法 闵可夫斯基距离M ...

  7. redis的哨兵集群,自动切换主从库

    Redis-Sentinel是redis官方推荐的高可用性解决方案,当用redis作master-slave的高可用时,如果master本身宕机,redis本身或者客户端都没有实现主从切换的功能. 而 ...

  8. 基于elementUI使用v-model实现经纬度输入的vue组件

    绑定一个 [12.34,-45.67] (东经西经,南纬北纬 正负表示) 形式的经纬度数组,能够按度分秒进行编辑,效果如下所示,点击东经,北纬可切换. 经纬度的 度转度分秒 能够获取度分秒格式数据 C ...

  9. maven开发SSH

    虽然开发SSH的基本步骤都差不多,但每次都从头开始做真的会有点儿烦,把maven的SSH框的基本代码放出来,下次就可以复制粘贴哈哈. 1. 配置文件: (1)pom.xml <project x ...

  10. JUC(4)---java线程池原理及源码分析

    线程池,既然是个池子里面肯定就装很多线程. 如果并发的请求数量非常多,但每个线程执行的时间很短,这样就会频繁的创建和销毁 线程,如此一来会大大降低系统的效率.可能出现服务器在为每个请求创建新线程和销毁 ...