poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 12733 | Accepted: 3286 |
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
Source

/**
Judge Status:Accepted Memory:4896K
Time:485MS Language:G++
Code Lenght:2940B Author:cj
*/ #include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stack> using namespace std;
#define N 1010
#define M 6060 struct Edge
{
int v;
int next;
int u;
}edge[M]; //M 开始传的N RE int head[N],edge_cnt;//这一行变量是构造边的 int pre[N],lowlink[N],sccno[N],dfs_cnt,scc_cnt; //这两行是Tarjan算法求强连通分量用的
stack<int> stk; vector<int> G[N]; //缩点重构图用的邻接链表
int in[N],mark[N][N]; //入度统计,以及重复边的标记 void init()
{
memset(head,-,sizeof(head));
edge_cnt = ;
} void addEdge(int a,int b)
{
edge[edge_cnt].v = b;
edge[edge_cnt].next = head[a];
edge[edge_cnt].u = a; //这里记录a,重构图用,方便遍历所有边
head[a] = edge_cnt++;
} void Tarjan(int u) //强连通分量
{
stk.push(u);
pre[u] = lowlink[u] = ++dfs_cnt;
int i;
for(i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].v;
if(!pre[v])
{
Tarjan(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(pre[u]==lowlink[u])
{
scc_cnt++;
int x;
do
{
x = stk.top();
stk.pop();
sccno[x] = scc_cnt; //sccno[x]表示下标为x的节点所在的第几个强连通分量
}while(x!=u);
}
} void findscc(int n)
{
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
dfs_cnt = scc_cnt = ;
while(!stk.empty()) //初始化 以防万一
{
stk.pop();
}
int i;
for(i=;i<=n;i++) if(!pre[i]) Tarjan(i);
} void getNewMap() //重构缩点后的图,存入邻接链表G中
{
int i,j;
for(i=;i<=scc_cnt;i++)
{
G[i].clear();
in[i] = ;
}
memset(mark,,sizeof(mark));
for(i=;i<edge_cnt;i++)
{
int v = edge[i].v;
int u = edge[i].u;
if(sccno[u]!=sccno[v])
{
if(!mark[u][v]) //重复边标记
{
G[sccno[u]].push_back(sccno[v]);
in[sccno[v]]++; //入度统计
}
mark[u][v] = ;
}
}
} int cntInid() //计算入度为0的位置,以及是不是一个
{
int i,cnt=,id=;
for(i=;i<=scc_cnt;i++)
{
if(!in[i])
{
cnt++;
id = i;
}
}
if(cnt==)
return id;
return ;
} int isOK() //用拓扑排序判断是否可行
{
int id = cntInid();
if(!id) return ;
int i;
in[id] = -;
for(i=;i<G[id].size();i++)
{
in[G[id][i]]--;
}
if(G[id].size()>) return isOK();
return ;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int i;
init();
for(i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
addEdge(a,b);
}
findscc(n); //求强连通分量
if(scc_cnt==) //如果只有一个那么肯定可行
{
puts("Yes");
continue;
}
getNewMap(); //用强连通分量缩点,重构图
if(isOK()) puts("Yes"); //拓扑排序判断
else puts("No");
}
return ;
}
poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)的更多相关文章
- poj 2553 The Bottom of a Graph(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- 【强连通分量缩点】poj 1236 Network of Schools
poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...
- POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
- 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?【强连通分量缩点+拓扑排序】
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15812 ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序
题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...
随机推荐
- Linux 命令 - route: 显示或操作路由表
route 命令查看路由表或者手动地添加.删除和修改路由表中的条目. 命令格式 route [-CFvnNee] [-A family] route [-v] [-A family] add [-ne ...
- Nginx - Core Module Directives
The following is the list of directives made available by the Core module. Most of these directives ...
- ZooKeeper(3.4.5) - 配置伪集群模式
1. 准备 Java 运行环境,需要安装 Java1.6 或更高版本的 JDK. 2. 下载 ZooKeeper 的稳定版本 zookeeper-x.x.x.tar.gz,将其解压,约定目录名称为 % ...
- HDOJ2006求奇数的乘积
求奇数的乘积 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Android Wi-Fi基本操作
从用户角度看,Android Wi-Fi模块自下向上可以看为5层:硬件驱动程序,wpa_suppplicant,JNI,WiFi API,WifiSettings应用程序. 1.wpa_supplic ...
- asp.net 文件操作小例子(创建文件夹,读,写,删)
静态生成要在虚拟目录下创建文件夹 来保存生成的页面 那么就要对文件进行操作 一.创建文件夹 using System.IO; string name = "aa"; strin ...
- 第一章、C#委托和事件(Delegate、Event、EventHandler、EventArgs)
第一章.C#委托和事件(Delegate.Event.EventHandler.EventArgs) 分类: 学习笔记-C#网络编程2012-12-08 14:10 7417人阅读 评论(3) 收藏 ...
- 免费的HTML5连载来了《HTML5网页开发实例详解》连载(五)图解通过Fiddler加速开发
Fiddler是Windows底下最强大的请求代理调试工具,监控任何浏览器的HTTP/HTTPS流量,窜改客户端请求和服务器响应,解密HTTPS Web会话,图4.44为Fiddler原理示意图. 图 ...
- CMakeLists实战解读--YouCompleteMe
原文转载自:Ricky.K http://www.cnblogs.com/rickyk/p/3877238.html 个人一直有一个想法,就是想出一系列关于CMakeLists.txt国外经典例子的实 ...
- 《APUE》第五章笔记
第五章具体介绍了标准I/O库的各种细节,要是一一列出来,有费精力且可能列不全,故只讲平常多用到的.标准输入输出是由一大批函数组成的. 要记住,标准输入输出是有缓冲的,就是当缓冲区的数据满了的时候,才会 ...