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 ...
随机推荐
- poj 2446 二分图最大匹配
思路:由(i+j)为偶数的点向(i+j)为奇数的点建边.求一次最大匹配,若正好为空格数(不包含洞)的一半,即输出YES. #include<iostream> #include<cs ...
- html5之meta标签viewport应用
在html5移动页面中,viewport定义必不可少. 首先了解下关于viewport的概念: 先了解移动设备的屏幕尺寸和设备尺寸: iPhone3 设备尺寸 320*480 ; 屏幕尺寸 320* ...
- http协议的总结说明
关于http协议已经有很多大牛们的讨论,从他们的文章中获益匪浅,作为一个通信专业的学生,还是想从计算机网络的角度谈一下自己的认识.http协议全称超文本传输协议,是一种允许将超文本标记语言(HTML) ...
- 上传系列:jquery.upload.js
最近想做一个上传的总结,把自己用过的上传插件都写一写,哪天用到的时候就不用再一次的翻阅资料,现在页面上用到的上传插件,原理上都差不多,那我也就不再废话了,下面我主要记录一下几个比较常用的,由简到繁,这 ...
- Activity的启动模式(android:launchMode)
在android里,有4种activity的启动模式,分别为: “standard” (默认) “singleTop” “singleTask” “singleInstance” 它们主要有如下不同: ...
- Contoso 大学 - 2 – 实现基本的增删改查
原文 Contoso 大学 - 2 – 实现基本的增删改查 目录 Contoso 大学 - 使用 EF Code First 创建 MVC 应用 原文地址:http://www.asp.net/mvc ...
- angular的post请求,SpringMVC后台接收不到参数值的解决方案
这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...
- Hibernate 拥有 Mybits 的SQL/HQL特性 (注解、XML两不误)
第一次写博客.文章有点渣,喜欢就看看,不喜欢路过点个赞. 效果:直接一条语句多种用法 FROM User A WHERE 1=1 <#if id??&g ...
- lex&yacc3
YACC yacc $$ translate relation ================================================================== ...
- 结构型模式——Adapter
1.意图 将一个类的接口转换成客户希望的另一个接口.使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 2.结构 类适配器 对象适配器 3.参与者 Target定义Client使用的与特定领域 ...