poj2762 缩点+topo排序

时间:2022-12-14 21:10:29
Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16486   Accepted: 4386

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
     
题意:
有一张有向图,然后问对于任意的2个点,是否存在x可以到达y,或者y能够到达x。
 
思路:
可以先强连通缩点,这样就没有环的情况。然后这样就是一张新的图。也就是相当于在新的图中判断任意2个点能否存在一条路径。这可以用topo排序来解决。如果存在2个点,他们的入度为0,说明这2个点是不能相互到达的。
 
/*
* Author: sweat123
* Created Time: 2016/6/25 12:33:09
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int from;
int to;
int next;
}edge[MAXN*];
int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind;
int f[MAXN],num,k;
stack<int>s;
void add(int x,int y){
edge[ind].from = x;
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt){
vis[rt] = ;
dfn[rt] = low[rt] = ++k;
s.push(rt);
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!dfn[t]){
dfs(t);
low[rt] = min(low[rt],low[t]);
} else if(vis[t]){
low[rt] = min(low[rt],dfn[t]);
}
}
if(low[rt] == dfn[rt]){
++ num;
while(!s.empty()){
int tp = s.top();
s.pop();
vis[tp] = ;
f[tp] = num;
if(tp == rt)break;
}
}
}
int x[MAXN],y[MAXN],ans,in[MAXN];
int ok(){
queue<int>q;
for(int i = ; i <= num; i++){
if(in[i] == ){
q.push(i);
}
}
if(q.size() > )return ;
while(!q.empty()){
int tp = q.front();
q.pop();
for(int i = pre[tp]; i != -; i = edge[i].next){
int t = edge[i].to;
in[t] --;
if(in[t] == ){
q.push(t);
}
}
if(q.size() > )return ;
}
return ;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
ind = ;
while(!s.empty())s.pop();
memset(pre,-,sizeof(pre));
for(int i = ; i <= m; i++){
scanf("%d%d",&x[i],&y[i]);
add(x[i],y[i]);
}
k = ;
num = ;
memset(vis,,sizeof(vis));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(f,,sizeof(f));
for(int i = ; i <= n; i++){
if(!dfn[i])dfs(i);
}
memset(pre,-,sizeof(pre));
memset(in,,sizeof(in));
int ret = ind;
for(int i = ; i < ret; i++){
int u = f[edge[i].from];
int v = f[edge[i].to];
if(u != v){
add(u,v);
in[v] ++;
}
}
if(ok()){
printf("Yes\n");
} else{
printf("No\n");
}
}
return ;
}

poj2762 缩点+topo排序的更多相关文章

  1. POJ 2762Going from u to v or from v to u&quest;&lpar;强联通 &plus; 缩点 &plus; 拓扑排序&rpar;

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

  2. topo排序 &plus; 用邻接表优化后的

    输入数据: 4 61 21 32 33 42 44 2 4 61 21 32 33 42 41 2 topo排序为偏序: #include<stdio.h> #include<que ...

  3. codeforce Gym 100685F Flood &lpar;topo排序&rpar;

    如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次, 科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次. #include<bits/std ...

  4. POJ2762 Going from u to v or from v to u&quest;(判定单连通图:强连通分量&plus;缩点&plus;拓扑排序)

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

  5. POJ2762 Going from u to v or from v to u&quest; 强连通分量缩点&plus;拓扑排序

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

  6. POJ2762 单向连通图&lpar;缩点&plus;拓扑排序

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19552 ...

  7. Going from u to v or from v to u&quest;&lowbar;POJ2762强连通&plus;并查集缩点&plus;拓扑排序

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

  8. FFF at Valentine&lpar;强连通分量缩点&plus;拓扑排序&rpar;

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. poj 2762 Going from u to v or from v to u&quest;【强连通分量缩点&plus;拓扑排序】

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812 ...

随机推荐

  1. 每次更新obj和bin文件夹都生成nGB的文件

    今天受不了,把它们符号链接到机械硬盘上了 用mklink /d 创建的,也不知道和/j有什么区别

  2. 如何关闭emacs开启时自己打开的欢迎界面

    EMACS在开启后,会自动打开一个欢迎界面.如果要去除他,可以在用emacs或其实编辑工具编辑~/.emacs文件. 在最下面一行加上,保存退出即可. (setq inhibit-startup-me ...

  3. lecture1-NN的简介

    这是DL的发明人Hinton在多伦多大学的2013年冬季教授de课程,并将视频分享到coursera网站上.其中不但有视频,也有课件,但是Hinton主页上还有他上课的课后问题,Hinton告诉学生这 ...

  4. 解决eclipse中安装AIX2插件问题

    为了做webservice,查了下,需要用到AXIS2(当然也有别的方法,貌似更复杂,详情可以参看:java开发webservice的几种方式).可试了N次,下载的codegen和service插件始 ...

  5. min&lowbar;free&lowbar;kbytes

    http://kernel.taobao.org/index.php?title=Kernel_Documents/mm_sysctl min_free_kbytes 先看官方解释:This is u ...

  6. Linux Shell 脚本

    1. 写一个脚本,利用循环计算10的阶乘#!/bin/shfactorial=1for a in `seq 1 10`do       factorial=`expr $factorial \* $a ...

  7. alloc、init你弄懂50&percnt;了吗?

    前言 这是一篇我记录对alloc.init分析思考的笔记.如果读者想看懂我的第二个思考,可能需要您至少了解内存的分段分页管理,如果您对其一点都不知道,可以先看这篇软文简单了解一下.另外很重要的一点是, ...

  8. Python Challenge 过关心得(0)

    最近开始用Openerp进行开发,在python语言本身上并没有什么太大的进展,于是决定利用空闲时间做一点python练习. 最终找到了这款叫做Python Challenge(http://www. ...

  9. Java EE基础之JSP

    从本篇文章开始,我会用文章记录下我在学习Java EE过程中的一些笔记和感悟,至于还没有更新结束的Java SE还是会继续写的,只是我觉得一直写语法很枯燥,自己也没法继续下去,所以带着点web学习,会 ...

  10. python实现散列表的链表法

    在散列中,链接法是一种最简单的碰撞解决技术,这种方法的原理就是把散列到同一槽中的所有元素 都放在一个链表中. 链接法有两个定理,定理一: 在简单一致散列的假设下,一次不成功查找的期望时间为O(1 + ...