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 ...
随机推荐
- Dynamic\Static\IsKinematic
1.Dynamic: 有Collider和RigidBody的GameObject, Unity视之为Dynamic. 适用于经常变换移动的对象. 2.Static: 只含有Collider的Game ...
- 我是如何自学Android,资料分享
我是如何自学Android,资料分享(2015 版) 已经完成,我的建议是先把这一篇看完,再看2015 版的.关于我在学习中开发的项目代码,已经发布在: 爱开发-源码搜索,集成了上万个App源码 ...
- 消息推送SignalR
一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...
- Android重力感应开发
http://blog.csdn.net/mad1989/article/details/20848181 一.手机中常用的传感器 在Android2.3 gingerbread系统中,google提 ...
- 使用 EF Power Tool Code Frist 生成 Mysql 实体
原文:使用 EF Power Tool Code Frist 生成 Mysql 实体 1,在要生成的项目上右键 2, 3, 4, 5, 生成后的效果 已知问题: 1,在Mys ...
- Ant 修改项目pom.xml文件应用
<?xml version="1.0" encoding="UTF-8"?> <project name="project" ...
- 第二篇、倾力总结40条常见的移动端Web页面问题解决方案
1.安卓浏览器看背景图片,有些设备会模糊. 用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率 ...
- iOS开发——model类模板(过滤null和ID)
说明:model类模板已默认过滤null值,附加特殊情况的关键字ID名的冲突(需手动去掉注释代码).MyMessageModel为示例的名字.可以自己随便起. 1.自己创建一个继承与N ...
- Linux学习三部曲(之一)
作为.NET程序员,一直以来都是windows环境下工作,很少接触到linux系统.但是随着微软跨出跨平台这一步之后,相信.NET程序员在linux平台进行开发也会变得越来越寻常. 所以,今天这篇文章 ...
- (转)Web2.0 大型互联网站点的架构
这种资料.向来可遇不可求啊 WikiPedia 技术架构学习分享 http://www.dbanotes.net/opensource/wikipedia_arch.html YouTube 的架构扩 ...