http://poj.org/problem?id=2762

Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12733   Accepted: 3286

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

 
【题解】:
  
  这题题目大概意思就是说 判断两个点之间是不是能够走通,u到v 或者 v到u 都行,这题开始题目意思都理解错了,一开始以为就是一个判断强连通的题;
  先求强连通分量;
  然后将强连通分量缩点,重构图;
  强连通分量内部的点肯定是可以两两到的,所以可以不用管了;
  缩点后的图,然后进行拓扑排序,如果有两个及以上的点的入度为0,那么他们之间肯定是不可达的,所以不符合
  如图:
  
【code】:
 /**
Judge Status:Accepted Memory:4896K
Time:485MS Language:G++
Code Lenght:2940B Author:cj
*/ #include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stack> using namespace std;
#define N 1010
#define M 6060 struct Edge
{
int v;
int next;
int u;
}edge[M]; //M 开始传的N RE int head[N],edge_cnt;//这一行变量是构造边的 int pre[N],lowlink[N],sccno[N],dfs_cnt,scc_cnt; //这两行是Tarjan算法求强连通分量用的
stack<int> stk; vector<int> G[N]; //缩点重构图用的邻接链表
int in[N],mark[N][N]; //入度统计,以及重复边的标记 void init()
{
memset(head,-,sizeof(head));
edge_cnt = ;
} void addEdge(int a,int b)
{
edge[edge_cnt].v = b;
edge[edge_cnt].next = head[a];
edge[edge_cnt].u = a; //这里记录a,重构图用,方便遍历所有边
head[a] = edge_cnt++;
} void Tarjan(int u) //强连通分量
{
stk.push(u);
pre[u] = lowlink[u] = ++dfs_cnt;
int i;
for(i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].v;
if(!pre[v])
{
Tarjan(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(pre[u]==lowlink[u])
{
scc_cnt++;
int x;
do
{
x = stk.top();
stk.pop();
sccno[x] = scc_cnt; //sccno[x]表示下标为x的节点所在的第几个强连通分量
}while(x!=u);
}
} void findscc(int n)
{
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
dfs_cnt = scc_cnt = ;
while(!stk.empty()) //初始化 以防万一
{
stk.pop();
}
int i;
for(i=;i<=n;i++) if(!pre[i]) Tarjan(i);
} void getNewMap() //重构缩点后的图,存入邻接链表G中
{
int i,j;
for(i=;i<=scc_cnt;i++)
{
G[i].clear();
in[i] = ;
}
memset(mark,,sizeof(mark));
for(i=;i<edge_cnt;i++)
{
int v = edge[i].v;
int u = edge[i].u;
if(sccno[u]!=sccno[v])
{
if(!mark[u][v]) //重复边标记
{
G[sccno[u]].push_back(sccno[v]);
in[sccno[v]]++; //入度统计
}
mark[u][v] = ;
}
}
} int cntInid() //计算入度为0的位置,以及是不是一个
{
int i,cnt=,id=;
for(i=;i<=scc_cnt;i++)
{
if(!in[i])
{
cnt++;
id = i;
}
}
if(cnt==)
return id;
return ;
} int isOK() //用拓扑排序判断是否可行
{
int id = cntInid();
if(!id) return ;
int i;
in[id] = -;
for(i=;i<G[id].size();i++)
{
in[G[id][i]]--;
}
if(G[id].size()>) return isOK();
return ;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int i;
init();
for(i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
addEdge(a,b);
}
findscc(n); //求强连通分量
if(scc_cnt==) //如果只有一个那么肯定可行
{
puts("Yes");
continue;
}
getNewMap(); //用强连通分量缩点,重构图
if(isOK()) puts("Yes"); //拓扑排序判断
else puts("No");
}
return ;
}

poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)的更多相关文章

  1. poj 2553 The Bottom of a Graph(强连通分量+缩点)

    题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  2. 【强连通分量缩点】poj 1236 Network of Schools

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

  3. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  4. 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. ...

  5. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

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

  6. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  7. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  9. 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 ...

随机推荐

  1. IE9+浏览器input文本框/密码框后面的小叉子/小眼睛清除

    为了方便我们的触控操作,IE高等浏览器针对input及input type="password"分别提供了快速清除钮(X图标)以及密码文字显示钮(小眼睛图标)的功能. 由于这经常跟 ...

  2. sql常识-Join

    SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据. Join 和 Key 有时为了得到完整的结果,我们需要从两个或更多的表中获取结果.我们就需要执行 join. 数据库中的表 ...

  3. 给 Android 初学者的 Gradle 知识普及

    给 Android 初学者的 Gradle 知识普及:http://gold.xitu.io/entry/5778f8bd165abd0054b443b0/promote?utm_source=bai ...

  4. ios 一个app启动另一个app

    问题描述:需要从一个ios应用程序中,能启动另一个ios应用程序. 开发环境:xcode7.3.1 关键词:白名单(LSApplicationQueriesSchemes).注册自己的URL Demo ...

  5. 事件[event]_C#

    事件(event): 1.       事件是类在发生其关注的事情时用来提供通知的方式.例如,封装用户界面控件的类可以定义一个在单击该控件时发生的事件.控件类不关心单击按钮时发生了什么,但它需要告知派 ...

  6. [Bootstrap]全局样式(三)

    表格 1.基本类  .table  {width/margin-bottom/}  {padding/border-top} e.g.:<table class="table" ...

  7. (四)Qt之右键菜单

    1.右键菜单创建和显示 作为一种交互性强.使用方便的右键菜单在程序中是非常常用的,在Qt中可以轻松的实现. QMenu menu; //添加菜单项,指定图标.名称.响应函数 menu.addActio ...

  8. lex&yacc

    LEX: yytext 数组包含匹配模式的文本; 使词法分析程序工作的两条规则是:1. lex 模式只匹配输入字符或字符串一次.2. lex 执行当前输入的最长可能匹配的动作. 由 lex 产生的词法 ...

  9. 《RHEL6.3 FTP服务器虚拟用户的配置(含图)》——如此简单

    虚拟用户就是传说中的ftp服务vip用户,大致分为这么几步: 1.安装ftp软件包 yum install *ftp* 2.启动vsftpd服务 /etc/init.d/vsftpd restart  ...

  10. FileStream使用小记

    流用于对IO处理 在System.IO名称空间中有以下类 BinaryReader/Writer TextReader/Writer Stream 其中类Stream为抽象类.由此有三个派生类: Me ...