判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意:

符合要求的不一定是链
拓扑排序列结果唯一,即在队列中的元素始终只有一个

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<stack>
using namespace std;
typedef long long LL;
const int N = 1008, INF = 0x3F3F3F3F;
int dfn[N],id[N];
int lab,cnt;
stack <int> st;
int n, m;
int head[N], tot;
int indeg[N];
vector<int> g[N];
void init(){
memset(head, - 1,sizeof(head));
tot= 0;
} struct Edge{
int to, next;
}edge[20008]; void add(int u, int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} int dfs(int u){
int lowu=dfn[u]=++lab;
st.push(u);
for(int i = head[u];i!=-1;i=edge[i].next){
int v = edge[i].to;
if(!dfn[v]){
int lowv = dfs(v);
lowu = min(lowu, lowv);
}else if(!id[v]) {
lowu = min(lowu, dfn[v]);
}
}
if(lowu == dfn[u]){
cnt++;
while(1){
int x = st.top();
st.pop();
id[x] = cnt;
if(x == u) break;
}
}
return lowu;
}
int tarjan(){
for(int i=1;i<=n;i++) {
dfn[i] = id[i] = 0;
}
lab=cnt=0;
for(int i=1;i<=n;i++) {
if(!dfn[i]){
dfs(i);
}
}
return cnt;
} //符合要求的不一定是链
//拓扑排序列结果唯一,即在队列中的元素始终只有一个
bool topsort(int n){
queue<int > q;
int sum = 0;
for(int i = 1; i <= n; i++){
if(indeg[i] == 0){
q.push(i);
if(q.size() > 1){
return false;
}
}
}
while(!q.empty()){
int u = q.front();
q.pop();
sum++;
for(int i= 0; i < g[u].size(); i++){
int v = g[u][i];
indeg[v]--;
if(indeg[v] == 0){
q.push(v);
}
}
if(q.size() > 1){
return false;
}
}
if(sum != n){
return false;
}
return true;
}
int main(){
int t;
cin>>t;
while(t--){
init();
memset(indeg, 0, sizeof(indeg));
scanf("%d %d", &n, &m);
while(m--){
int u, v;
scanf("%d %d", &u, &v);
add(u, v);
}
tarjan();
for(int i =1; i<= cnt; i++){
g[i].clear();
}
for(int u = 1; u <= n; u++){
for(int i = head[u] ; ~i ; i = edge[i].next){
int v = edge[i].to;
if(id[u] != id[v]){
indeg[id[v]]++;
g[id[u]].push_back(id[v]);
}
}
}
if(topsort(cnt)){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}

  

POJ2762 Going from u to v or from v to u(单连通 缩点)的更多相关文章

  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?(判定单连通图:强连通分量+缩点+拓扑排序)

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

  3. [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud     Going from u to v or from v to u? Tim ...

  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. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  2. Servlet的使用方法详细说明

    Servlet的生命周期方法: init() destroy() doGet(HttpServletRequest request,HttpServletResponse response) 客户端请 ...

  3. MySQL 中根据A表的ID查询B表数据

    例1:查询某个文章及其对应的评论(单个详情) ) FROM A; 例2:查询分类表中,每种分类各包含多少商品(汇总) SELECT category_id, (SELECT count(goods_i ...

  4. IOSGCD

    http://blog.csdn.net/duxinfeng2010/article/details/8958681/ http://kyfxbl.iteye.com/blog/1997516

  5. Aufs与Devicemapper的关系

    Aufs与Devicemapper的应用 Aufs是Docker最初采用的文件系统,由于Aufs未能加入到Linux内核,考虑到兼容性问题,加入了Devicemapper的支持.目前,除少数版本如Ub ...

  6. 获取编辑框字符串,传入Intent

    private EditText etData; etData = (EditText) findViewById(R.id.etData); Intent i = new Intent(MainAc ...

  7. 【GoLang】GoLang map 非线程安全 & 并发度写优化

    Catena (时序存储引擎)中有一个函数的实现备受争议,它从 map 中根据指定的 name 获取一个 metricSource.每一次插入操作都会至少调用一次这个函数,现实场景中该函数调用更是频繁 ...

  8. block,inline和inline-block对比

    总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通 ...

  9. ios 获得设备型号方法

    以前用UIScreen 的大小来判断设备类型,现在有了iphone6 和 iphone6 plus, 这种方法不能用了.因为当程序不提供相应的启动图片时,系统会把程序运行在320*568的size下, ...

  10. Delphi中限制文本框(TEdit)只能输入数字

    procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);var edt: TEdit; str, strL, strR: stri ...