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 ...
随机推荐
- PS基础学习 1---基本工具
1,选框工具: 选择以后对选框中的内容进行修改 ① Shift + 选框 为正方形 ② 选中后鼠标放在选框中对选择范围进行拖动 ③ 移动工具可以拉着选框中的内容移动 ④ ctrl+D取消选框 ⑤单行选 ...
- InternetOpen怎么使用代理
如果你用IE的默认代理设置:hinternet=InternetOpen(AfxGetAppName(),INTERNET_OPEN_TYPE_PROXY,NULL,NULL,0); 把INTERNE ...
- android 使用两个surfaceview 在摄像机画面上绘图
转载自http://blog.csdn.net/jesse__zhong/article/details/24934083 使用双surface,将第一个设置为透明背景,在摄像机上绘制图像,纠结搞了一 ...
- boost库区间range基本原理及使用实例
由 www.169it.com 搜集整理 区间的概念类似于STL中的容器概念.一个区间提供了可以访问半开放区间[first,one_past_last)中元素的迭代器,还提供了区间中的元素数量的信息. ...
- TCP/IP——内外网IP+子网掩码作用+PING(网络总结)
目录: 1.如何区分内网IP和外网IP? 保留字段 2.子网掩码是起什么作用的? 将DNS和IP异或,表示哪段起作用 3.ping到底起什么作用? ping本地.ping远程 下面针对上面三个问题分别 ...
- Learn Python The Hard Way学习笔记001
今天搜索了一下raw_input() 和 input()的区别,引用下原文部分内容 两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收).而对于 ...
- iOS开发——消息推送跳转
项目开发用集成是极光推送JPush 这里主要是消息推送过来处理对应界面跳转 同时看到两篇写的不错的相关博客分享一下: http://www.jianshu.com/ ...
- xcode-重新打开欢迎界面
嫌不够逼格关掉 关掉又后悔= = 重新打开方式为: command+shift+1 然后把左下勾上就可以每次都打开了 一个字,折腾
- bzoj 1040 骑士
这题真不爽,各种WA,写个题解浏览器还挂了,真不爽. 所以不多说了,就说关于判断是否是父节点的问题,不能直接判,会有重边,这种情况只能用编号判,传进去入边的编号,(k^1) != fa,这样就可以了. ...
- Windows Phone使用sliverlight toolkit实现页面切换动画效果
使用应用时,好多app在页面切换的时候都有一个动画效果,感觉很炫,也大大增加了用户体验,怎么实现呢? 界面的切换,可以用Windows Phone Toolkit中的TransitionService ...