转载请注明出处: 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 ;
}

代码君

最新文章

  1. 使用WampServer环境,如何配置虚拟主机域名
  2. CentOS 下使用yum安装nodejs
  3. javascript类型系统——Number数字类型
  4. oracle视图
  5. linux连接12tp vpn
  6. Redis配置文件解读
  7. c# 数据库操作学习
  8. webpack入门(译)
  9. js截取所需字符串长度
  10. Ext.Net中的Task控件的使用
  11. Junit4 架构设计系列(2): Runner.run()与Statement
  12. Linux平台下裸设备的绑定:
  13. 使用XML布局文件和Java代码混合控制UI界面
  14. Codeforces Round #430 (Div. 2)
  15. django请求接收及文件上传
  16. 采用Tensorflow内部函数直接对模型进行冻结
  17. python连接Greenplum数据库
  18. struts2、hibernate以及spring是如何配置的
  19. perf 安装到分析
  20. vue methods computed watch区别

热门文章

  1. Visual C++基础知识(win32exe)
  2. set用法总结
  3. JAVA GUI 工具
  4. 解决Sublime-Text-3在ubuntu下中文输入的问题
  5. Hdu1076(n个闰年后的年份)
  6. 使用ARM和VMSS创建自动扩展的web集群
  7. Codeforces 358D Dima and Hares
  8. HDU 4010 Query on The Trees(动态树)
  9. paip.Adblock屏蔽规则保存位置以及修理恢复
  10. 2015第15周日PostgreSQL学习