poj 2762
Tarjan + Topsort
Tarjan 缩点
Topsort 判断
Topsort 判断:
在DAG中
若初始状态下存在多于1个入度为0的点
则说明这些 入度为0的点之间不会有路径可达
若不存在入度为0的点,则状态为Yes
若只存在1个入度为0的点,将该点指出的边删除
继续上述判断
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> const int N = , M = N * ; #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} int head[N], head_2[N], cnt;
struct Node {int u, v, nxt;};
Node G[M], E[M];
int In[N], n, m;
int Low[N], Dfn[N], Stack[N], Belong[N], Scc, Tim, topp;
bool vis[N]; inline void Add_1(int u, int v) {G[++ cnt].v = v; G[cnt].nxt = head[u]; head[u] = cnt;}
inline void Add_2(int u, int v) {E[++ cnt].v = v; E[cnt].nxt = head_2[u]; head_2[u] = cnt; In[v] ++;} inline void Clear() {
memset(head, -, sizeof head);
memset(head_2, -, sizeof head_2);
memset(In, , sizeof In);
memset(Low, , sizeof Low);
memset(Dfn, , sizeof Dfn);
memset(vis, , sizeof vis);
topp = cnt = Scc = Tim = ;
} inline void Init() {
n = read(), m = read();
for(int i = ; i <= m; i ++) Add_1(read(), read());
} void Tarjan(int x) {
Low[x] = Dfn[x] = ++ Tim;
Stack[++ topp] = x; vis[x] = ;
for(int i = head[x]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(!Dfn[v]) {
Tarjan(v);
Low[x] = std:: min(Low[x], Low[v]);
} else if(vis[v]) Low[x] = std:: min(Low[x], Low[v]);
}
if(Dfn[x] == Low[x]) {
vis[x] = , Belong[x] = ++ Scc;
while(Stack[topp] != x) {
vis[Stack[topp]] = , Belong[Stack[topp]] = Scc;
topp --;
} topp --;
}
} inline void Rebuild() {
cnt = ;
for(int u = ; u <= n; u ++)
for(int i = head[u]; ~ i; i = G[i].nxt)
if(Belong[u] != Belong[G[i].v]) Add_2(Belong[u], Belong[G[i].v]);
} void Topsort() {
if(Scc == ) {puts("Yes"); return ;}
int Ans(), flag;
for(int i = ; i <= Scc; i ++) if(!In[i]) Ans ++, flag = i;
if(Ans > ) {puts("No"); return ;}
int temp = Scc;
for(; temp; temp --) {
Ans = ;
for(int i = head_2[flag]; ~ i; i = E[i].nxt) {
int v = E[i].v;
In[v] --;
if(!In[v]) Ans ++, flag = v;
}
if(Ans > ) {puts("No"); return ;}
if(!Ans) {puts("Yes"); return ;}
}
puts("Yes"); return ;
} void Work() {
Clear();
Init();
for(int i = ; i <= n; i ++) if(!Dfn[i]) Tarjan(i);
Rebuild();
Topsort();
} int main() {
int t = read();
for(; t; t --, Work());
return ;
}
poj 2762的更多相关文章
- POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)
职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...
- poj 2762(强连通+判断链)
题目链接:http://poj.org/problem?id=2762 思路:首先当然是要缩点建新图,由于题目要求是从u->v或从v->u连通,显然是要求单连通了,也就是要求一条长链了,最 ...
- 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. ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
- POJ 2762 Going from u to v or from v to u? (判断单连通)
http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...
- [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?
题目链接: http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory L ...
- POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)
id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...
- [强连通分量] 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 ...
- POJ 2762 tarjan缩点+并查集+度数
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15494 ...
随机推荐
- Java InterpolationSearch
Java InterpolationSearch /** * <html> * <body> * <P> Copyright 1994-2018 JasonInte ...
- 取代Ajax.BeginForm的ajax使用方法
原文:取代Ajax.BeginForm的ajax使用方法 一.前提概要 Asp.net core中已经取消了Ajax.BeginForm,也不会计划出ajax tag helper,所以得利用插件jq ...
- 定时任务FluentScheduler
1.Nuget 安装包 2.创建3个不同的任务 public class MyJob : IJob { void IJob.Execute() { Trace.WriteLine("现在时间 ...
- Spring的SSM标准配置
一.首先是web.xml文件的配置 <welcome-file-list> <!--设置默认显示登陆界面--> <welcome-file>login.jsp< ...
- vulnhub攻略
https://www.vulnhub.com/entry/21ltr-scene-1,3/ 这个靶机国内https://www.cnblogs.com/hack404/p/11423228.html ...
- window service 批处理安装/卸载命令
开启服务 @echo.服务启动...... @echo off @sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32sr ...
- TreeMap核心源码实现解析
TreeMap实现了SotredMap接口,它是有序的集合.而且是一个红黑树结构,每个key-value都作为一个红黑树的节点.如果在调用TreeMap的构造函数时没有指定比较器,则根据key执行自然 ...
- 猫眼 top_100 爬取 ___只完成了第一页
# python 3.7 from urllib.request import Request,urlopen import time,re,csv class Maoyan(object): def ...
- 跟着minium官网介绍学习minium-----(二)
一: 进入minium官方文档 1. 进入minium目录然后运行服务,出现以下提示说明打开成功, 2. 浏览器直接运行http://localhost:3000即可看到效果. 3. 下图为进入网页后 ...
- 文本三剑客之grep及正则表达式
1.grep 1. 什么是grep.egrep和fgrep Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来(匹配到的标红).grep全称是Glo ...