转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud
 
 
Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14778   Accepted: 3911

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

题意:给一个图,问是否为单向连通。

Kosaraju+缩点,然后拓扑序搞一下,若只有一条没有分支的链,则Yes,否则No

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn=;
vector<int>G[maxn];
vector<int>rG[maxn];
vector<int>vs;
int vis[maxn];
int cmp[maxn];
int last[maxn];
int deg[maxn];
int next[maxn];
int V;
vector<int>vc[maxn];
void add_edge(int u,int v)
{
G[u].push_back(v);
rG[v].push_back(u);
}
void init(int n)
{
for(int i=;i<n;i++)
{
G[i].clear();
rG[i].clear();
vc[i].clear();
vis[i]=;
last[i]=-;
deg[i]=;
next[i]=-;
}
}
void dfs(int v)
{
vis[v]=;
for(int i=;i<G[v].size();i++)
{
if(!vis[G[v][i]])dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v,int k)
{
vis[v]=;
cmp[v]=k;
vc[k].push_back(v);
for(int i=;i<rG[v].size();i++)
{
if(!vis[rG[v][i]])rdfs(rG[v][i],k);
}
}
int scc()
{
memset(vis,,sizeof(vis));
vs.clear();
for(int i=;i<V;i++)
{
if(!vis[i])dfs(i);
}
memset(vis,,sizeof(vis));
int k=;
for(int i=vs.size()-;i>=;i--)
{
if(!vis[vs[i]])rdfs(vs[i],k++);
}
return k;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
V=n;
init(n);
int u,v;
for(int i=;i<m;i++)
{
cin>>u>>v;
add_edge(--u,--v);
}
int k=scc();
memset(vis,,sizeof(vis));
int flag=;
int num=;
for(int i=;i<k;i++)
{
for(int j=;j<vc[i].size();j++)
{
u=vc[i][j];
for(int l=;l<G[u].size();l++)
{
v=G[u][l];
if(cmp[u]!=cmp[v])
{
if(last[cmp[v]]==-)
{
last[cmp[v]]=cmp[u];
}
else if(last[cmp[v]]==cmp[u])continue;
else flag=;
if(next[cmp[u]]==-)
{
next[cmp[u]]=cmp[v];
}
else if(next[cmp[u]]==cmp[v])
{
continue;
}
else flag=;
}
}
}
}
for(int i=;i<k;i++)
{
if(last[i]==-)num++;
}
if(flag&&num==)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
} return ;
}

代码君

[poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)的更多相关文章

  1. POJ2762 Going from u to v or from v to u? 强连通+缩点

    题目链接: poj2762 题意: 给出一幅单向图.问这张图是否满足   随意两点ab 都能 从a到达b 或  从b到达a 题解思路: 推断一幅图是否满足弱连通 首先想到的是将图中的 强连通分量(能互 ...

  2. POJ2762 Going from u to v or from v to u(单连通 缩点)

    判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...

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

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

  4. poj2762 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: 13040 ...

  5. 【缩点+拓扑判链】POJ2762 Going from u to v or from v to u?

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  6. Oracle基本数据字典:v$database、v$instance、v$version、dba_objects

    v$database: 视图结构: SQL> desc v$database; Name                                      Null?    Type - ...

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

  8. 临时文件相关的v$tempfile v$sort_usage与V$tempseg_usage

    SQL> select username,user,segtype,segfile#,segblk#,extents,segrfno# from v$sort_usage; SEGFILE#代表 ...

  9. [强连通分量] 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: 17089 ...

随机推荐

  1. w530 在ubuntu 12.04 _x64 背光调节方法

    So to get the screen brightness keys working with your Nvidia graphics card, create a file in the xo ...

  2. hprof教程

    大部分内容参考http://www.linuxidc.com/Linux/2012-04/58178.htm J2SE中提供了一个简单的命令行工具来对java程序的cpu和heap进行 profili ...

  3. 在windows下获取硬盘序列号(win7 32位,Windows Server 64位测试,希望在其他平台测试,遇到问题的网友留言分享)

    #include <Windows.h> #include <stdio.h> // IOCTL控制码 // #define DFP_SEND_DRIVE_COMMAND CT ...

  4. 根据文字计算Label的尺寸

    CGSize size = [self.username.text boundingRectWithSize:(CGSize){130,20} options:NSStringDrawingUsesL ...

  5. un ange frappe a ma porte

    Un signe, une larme 魂牵 泪扰 un mot, une arme 字断 情烧 nettoyer les étoiles à l'alcool de mon ame 灵魂之酒眷洗 星 ...

  6. cf A. Vasily the Bear and Triangle

    http://codeforces.com/contest/336/problem/A #include <cstdio> #include <cstring> #includ ...

  7. Seafile的手册

    http://manual.seafile.com/http://manual-cn.seafile.com/deploy/using_mysql.html 中文版http://manual-cn.s ...

  8. 开发反模式 - SQL注入

    一.目标:编写SQL动态查询 SQL常常和程序代码一起使用.我们通常所说的SQL动态查询,是指将程序中的变量和基本SQL语句拼接成一个完整的查询语句. string sql = SELECT * FR ...

  9. bzoj1379 [Baltic2001]Postman

    Description 邮递员每天给N个村子的人送信,每个村子可能在某个十字路口上,或一条路的中央. 村子里的人都希望早点收到信,因此与邮递员达成一个协议:每个村子都有一个期望值Wi,如果这个村子是邮 ...

  10. bzoj2741(分块+可持久化Trie)

    题意中文我就不说了 解析: 分块+可持久化Trie,先得到前缀异或值,插入到Trie中,然后分块,对每一块,处理出dp[i][j](i代表第几块,j代表第几个位置),dp[i][j]代表以第i块开始的 ...