poj2762 判断一个图中任意两点是否存在可达路径 也可看成DAG的最小覆盖点是否为1
Going from u to v or from v to u?
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 17993 | Accepted: 4816 |
Description
Input
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
Sample Input
1
3 3
1 2
2 3
3 1
Sample Output
Yes 题目大意就是给你张图,问你是否任意两点u,v 可以从u到v或者从v到u
两种解法 先贴拓扑的
另外有些数据
1
3 2
1 2
3 2 1
3 3
1 2
1 3
2 3
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
const int M=;
int head[N],dfn[N],in[N],low[N],bl[N],q[N];
int tot,cnt,scnt,n,m,l;
bool instack[N];
bool adj[N][N];
struct node{
int u,to,next;
}e[M<<];
void init(){
memset(dfn,,sizeof(dfn));
memset(instack,,sizeof(instack));
memset(in,,sizeof(in));
memset(head,-,sizeof(head));
memset(adj,,sizeof(adj));
l=tot=cnt=scnt=;
}
void add(int u,int v){
e[tot].u=u;e[tot].to=v;e[tot].next=head[u];head[u]=tot++;
}
void Tajan(int u){
instack[u]=;
q[l++]=u;
low[u]=dfn[u]=++cnt;
for(int i=head[u];i+;i=e[i].next){
int v=e[i].to;
if(!dfn[v]) {
Tajan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]&&low[u]>dfn[v]) low[u]=dfn[v];
}
if(low[u]==dfn[u]){
int t;
++scnt;
do{
t=q[--l];
instack[t]=;
bl[t]=scnt;
}while(t!=u);
}
}
bool Ju(int u){
while(scnt--){
int cont=;
for(int i=head[u];i+;i=e[i].next){
int v=e[i].to;
--in[v];
if(in[v]==) {
cont++;u=v;
}
}
if(cont>) return ;
}
return ;
}
int main(){
int T,u,v;
for(scanf("%d",&T);T--;){
scanf("%d%d",&n,&m);
init();
for(int i=;i<=m;++i){
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=;i<=n;++i)
if(!dfn[i]) Tajan(i);
memset(head,-,sizeof(head));
for(int i=;i<tot;++i){
u=e[i].u,v=e[i].to;
if(bl[u]==bl[v]||adj[u][v]) continue;
else {
adj[bl[u]][bl[v]]=; // 注意这里
add(bl[u],bl[v]);
++in[bl[v]];
}
}
int cont=,k;
for(int i=;i<=scnt;++i)
if(!in[i]) {++cont;k=i;}
if(cont>) {puts("No");continue;}
else {
if(Ju(k)) puts("Yes");
else puts("No");
}
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm> using namespace std; const int MAXN = ;
const int MAXM = ; struct Edge{
int to, next;
}edge[MAXM]; int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN], Belong[MAXN];
int Index, top;
int scc;
bool Instack[MAXN];
int num[MAXN];
int n, m; void init() {
tot = ;
memset(head, -, sizeof(head));
} void addedge(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void Tarjan(int u) {
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for (int i = head[u]; i != -; i = edge[i].next) {
v = edge[i].to;
if (!DFN[v]) {
Tarjan(v);
if (Low[u] > Low[v]) Low[u] = Low[v];
}
else if (Instack[v] && Low[u] > DFN[v])
Low[u] = DFN[v];
}
if (Low[u] == DFN[u]) {
scc++;
do {
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
} while (v != u);
}
} void solve() {
memset(Low, , sizeof(Low));
memset(DFN, , sizeof(DFN));
memset(num, , sizeof(num));
memset(Stack, , sizeof(Stack));
memset(Instack, false, sizeof(Instack));
Index = scc = top = ;
for (int i = ; i <= n; i++)
if (!DFN[i])
Tarjan(i);
} vector<int> g[MAXN];
int linker[MAXN], used[MAXN]; bool dfs(int u) {
for (int i = ; i < g[u].size(); i++) {
int v = g[u][i];
if (!used[v]) {
used[v] = ;
if (linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int hungary() {
int res = ;
memset(linker, -, sizeof(linker));
for (int i = ; i <= scc; i++) {
memset(used, , sizeof(used));
if (dfs(i)) res++;
}
return (scc - res)==;
} int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d", &n, &m);
init();
int u, v;
for (int i = ; i < m; i++) {
scanf("%d%d", &u, &v);
addedge(u, v);
}
solve(); for (int i = ; i <= scc; i++) g[i].clear();
for (int u = ; u <= n; u++) {
for (int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if (Belong[u] != Belong[v])
g[Belong[u]].push_back(Belong[v]);
}
}
if(hungary()) puts("Yes");
else puts("No");
}
return ;
}
poj2762 判断一个图中任意两点是否存在可达路径 也可看成DAG的最小覆盖点是否为1的更多相关文章
- Floyd-Warshall求图中任意两点的最短路径
原创 除了DFS和BFS求图中最短路径的方法,算法Floyd-Warshall也可以求图中任意两点的最短路径. 从图中任取两点A.B,A到B的最短路径无非只有两种情况: 1:A直接到B这条路径即是最短 ...
- javascript实现有向无环图中任意两点最短路径的dijistra算法
有向无环图 一个无环的有向图称做有向无环图(directed acycline praph).简称DAG 图.DAG 图是一类较有向树更一般的特殊有向图, dijistra算法 摘自 http://w ...
- C#实现如何判断一个数组中是否有重复的元素 返回一个数组升序排列后的位置信息--C#程序举例 求生欲很强的数据库 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework
C#实现如何判断一个数组中是否有重复的元素 如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hasht ...
- 【C++】判断一个图是否有环 无向图 有向图(转载)
没有找到原文出处,请参考一下链接: http://www.cnblogs.com/hiside/archive/2010/12/01/1893878.html http://topic.csdn.ne ...
- C#实现如何判断一个数组中是否有重复的元素
如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hashtable的Contains方法进行查找 /// ...
- Python判断一个字符串中是否存在多个子串中的一个
在使用python的开发过程中,常常需要判断,字符串中是否存在子串的问题, 但判断一个字符串中是否存在多个字串中的一个时,如if (a or b) in c或者if x contains a|b|c| ...
- c c++怎么判断一个字符串中是否含有汉字
c c++怎么判断一个字符串中是否含有汉字 (2013-02-05 10:44:23) 转载▼ #include #include int main() { char sztext[] = ...
- C#判断一个类中有无"指定名称"的方法
C#中可以通过反射分析元数据来解决这个问题,示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...
- javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数
javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...
随机推荐
- QML-密码管理器
Intro 年初刚学Qml时写的密码管理器.用到Socket通信.AES加密等.UI采用Material Design,并实现了Android App的一些常见基本功能,如下拉刷新.弹出提示.再按一次 ...
- 计算4的n次幂html代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 《PostgreSQL服务器编程》一一1.3 超越简单函数
本节书摘来自华章计算机<PostgreSQL服务器编程>一书中的第1章,第1.3节,作者:(美)Hannu Krosing, Jim Mlodgenski, Kirk Roybal 著,更 ...
- JAVA_WEB--jsp语法
JSP声明 一个声明语句可以声明一个或多个变量.方法,供后面的Java代码使用.在JSP文件中,必须先声明这些变量和方法然后才能使用它们. JSP声明的语法格式: <%! declaration ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
- [UWP]推荐一款很Fluent Design的bilibili UWP客户端 : 哔哩
UWP已经有好几个Bilibili的客户端,最近有多了一个: 哔哩 - Microsoft Store 作者云之幻是一位很擅长设计的UWP开发者,我也从他那里学到了很多设计方面的技巧.它还是一位Bil ...
- 精通awk系列文章
精通awk系列文章 我录制了两个awk相关的视频教程: Awk经典实战案例精讲 精通awk精品课程:awk从入门到精通 1.安装新版本的gawk 2.本教程测试所用示例文件 3.铺垫知识:读取文件的几 ...
- ASP.NET Core MVC 如何获取请求的参数
一次HTTP请求,就是一次标准IO操作.请求是I,是输入:响应式O,是输出.任何web开发框架,其实都是在干这两件事: 接受请求并进行解析获取参数 根据参数进行渲染并输出响应内容 所以我们学习一个框架 ...
- hdu1074之状压dp
#include <iostream> #include <cstdio> #include <cstring> using namespace std; cons ...
- 网络流 I - Fox And Dinner CodeForces - 510E
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...