弱连通模板题,不过还是不会。。。


这道题在POJ2762有,这个出题人直接翻译弄过来了。。。

弱连通的定义是:从u能到达v从v能到达u,则u和v这两个点弱连通。

显然如果是强连通分量就一定是弱连通分量啦,所以可以直接缩点弄掉。

缩点后的DAG中,可能会不符合条件的不可能被我们缩掉。

那么对于这个DAG,发现只有两个或多个边连入某个点的话,就不符合这个条件。

所以对一个新图弄一个toposort,一旦通过同一个点删边的过程添加了两个以上的点的话,就绝对不符合条件。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
const int maxn = 10005, maxm = 100005;
struct Edges
{int next, from, to;
} e[maxm], e2[maxm];
int head[maxn], tot;
int head2[maxn], tot2;
int n, m;
int dfn[maxn], low[maxn], dtot;
bool vis[maxn];
int color[maxn], ctot;
int indegree[maxn];
std::stack<int> s;
int read()
{int ans = 0, s = 1;char ch = getchar();while(ch > '9' || ch < '0'){if(ch == '-') s = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){ans = ans * 10 + ch - '0';ch = getchar();}return s * ans;
}
void init()
{memset(head, 0, sizeof(head));memset(head2, 0, sizeof(head2));memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(color, 0, sizeof(color));memset(indegree, 0, sizeof(indegree));tot = tot2 = dtot = ctot = 0;while(!s.empty()) s.pop();
}
void link(int u, int v)
{e[++tot] = (Edges){head[u], u, v};head[u] = tot;
}
void link2(int u, int v)
{e2[++tot2] = (Edges){head2[u], u, v};head2[u] = tot2;
}
void tarjan(int u)
{dfn[u] = low[u] = ++dtot;s.push(u); vis[u] = true;for(int i = head[u]; i; i = e[i].next){int v = e[i].to;if(!dfn[v]){tarjan(v);low[u] = std::min(low[u], low[v]);}else if(vis[v]) low[u] = std::min(low[u], dfn[v]);}if(low[u] == dfn[u]){ctot++;while(s.top() != u){int x = s.top(); s.pop();vis[x] = false; color[x] = ctot;}int x = s.top(); s.pop();vis[x] = false; color[x] = ctot;}
}
bool toposort()
{std::queue<int> q;int cnt = 0, count = 0;for(int i = 1; i <= ctot; i++){if(indegree[i] == 0){q.push(i);cnt++;}}if(cnt > 1) return false;while(!q.empty()){count++;cnt = 0;int u = q.front(); q.pop();for(int i = head2[u]; i; i = e2[i].next){int v = e2[i].to;indegree[v]--;if(indegree[v] == 0){q.push(v); cnt++;}}if(cnt > 1) return false;}if(count == ctot) return true;
}
int main()
{//freopen("in.txt", "r", stdin);int T = read();while(T--){init();n = read(), m = read();while(m--){int u = read(), v = read();link(u, v);}for(int i = 1; i <= n; i++){if(!dfn[i]) tarjan(i);}//for(int i = 1; i <= n; i++) printf("%d\n", color[i]);for(int i = 1; i <= tot; i++){int u = e[i].from, v = e[i].to;if(color[u] != color[v]){link2(color[u], color[v]);indegree[color[v]]++;}}if(toposort()) printf("Yes\n");else printf("No\n");}return 0;
}

转载于:https://www.cnblogs.com/Garen-Wang/p/9461792.html

T24412 Cup#182-3 洞穴之旅相关推荐

  1. 单目标应用:世界杯优化算法(World Cup Optimization,WCO)求解单仓库多旅行商问题SD-MTSP(可更改旅行商个数及起点)

    一.世界杯优化算法 世界杯优化算法(World Cup Optimization,WCO)由Navid Razmjooy等人于2016年提出,该算法模拟了国际足联世界杯比赛,思路新颖,收敛速度快,全局 ...

  2. 《仙剑奇侠传》的宗教元素考察(一):赵灵儿的宿命之旅

    英雄之旅,象征范型,与宗教战争 自 1995 年<仙剑奇侠传>(通称 DOS 版)由台湾大宇资讯推出以来,该系列已成为华语地区最有知名度的 RPG 游戏系列之一.二十多年间,冠以<仙 ...

  3. Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) A-F全题解

    Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) 文章目录 A. Simply Strange Sort B. ...

  4. 人物志 | KDD Cup 2017双料冠军燕鹏

    2017年数据挖掘领域最有影响力的赛事KDD Cup近日揭晓,Convolution队从全球70个国家的3582支队伍里脱颖而出,包揽两项任务的冠军.这支双料冠军队成员名单里,有一个我们熟悉的名字-- ...

  5. 瑶琳c语言,来桐庐瑶琳仙境,开启一场18°C的奇妙之旅

    在杭州桐庐的瑶琳镇的瑶琳仙境,常年恒温在18摄氏度,是华东沿海中部亚热带湿润区喀斯特洞穴的典型代表. 瑶琳仙境得名于清朝,据清<桐庐县志>记载:"瑶琳洞,在县西北四十五里,洞口阔 ...

  6. 一次openresty http.lua 性能调优之旅

    记一次openresty http.lua 性能调优之旅 1 背景 最近要用Nginx lua进行http 数据交互,因此想到了resty/http.lua,因此开启一段性能调优之旅. 2 发送HTT ...

  7. 【转】VS2010测试功能之旅:编码的UI测试(2)-操作动作的录制原理(上)

    VS2010测试功能之旅 --编码的UI测试系列之二:操作动作的录制原理(上) RealZhao,2011年2月18日 回顾 在之前我们介绍了如何用VS2010的UI测试功能创建一个简单的示例,大致描 ...

  8. 【POI2004】【Bzoj2069】T2 洞穴 zaw

    T2 洞穴zaw [问题描述] 在 Byte 山的山脚下有一个洞穴入口. 这个洞穴由复杂的洞室经过隧道连接构成. 洞穴的入口是 1 号点.两个洞室要么就通过隧道连接起来,要么就经过若干隧道间接的相连. ...

  9. 人在旅途——》张家界之旅:20190420

    版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者. https://blog.csdn.net/weixin_43453386/article/detai ...

最新文章

  1. js控制select大全
  2. S5PV210开发 -- I2C 你知道多少?(三)
  3. 物联网安全有哪些注意事项
  4. 深入探讨MFC消息循环和消息泵(一)
  5. 两个有序链表序列的交集_腾讯50题---合并两个有序链表(简单)
  6. Linux 删除除某个文件之外的所有文件
  7. thzthz.net forum.php,xthz画质修改器
  8. dcm4che操作dcm文件
  9. microhard p900数传配置方法
  10. Python 根据身份证号码计算持有者年龄
  11. 思维方式 | 深入浅出解释“第一性原理”
  12. qt触摸屏隐藏鼠标指针
  13. for in在python中什么意思_python中for in的用法详解
  14. 年度目标进度和完成进度对比
  15. 【多线程】多线程基础知识
  16. HIT-ICS2022大作业-程序人生-Hello’s P2P
  17. 阿翔编程学-Axis传递Pojo对象
  18. android耳机音量与外放音量同步
  19. 二进制数字调制器的设计
  20. 国家科技型中小企业申报条件

热门文章

  1. 将虚拟主机加入到netskills.net域环境_网站建设阿里云虚拟主机、ECS服务器、企业邮箱选择购买指南...
  2. mysql数据库导入导出_MySQL数据库导入导出详解
  3. 内存颗粒位宽和容量_64M的SDRAM颗粒 一般内存是多大的?
  4. 伪静态php配置,PHP开启伪静态配置
  5. 图形学教程Lecture 2: Review of Linear Algebra知识点总结
  6. 颜色协调模型Color Harmoniztion
  7. 干货 | 深度学习名词表:57个专业术语加相关资料解析(附论文)
  8. sql 日期类型空值等于 1900-01-01
  9. Flask--WebSocket
  10. Spring Cloud Sleuth 中id的使用