POJ 2762 tarjan缩点+并查集+度数
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 15494 | Accepted: 4100 |
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
如果弱连通分量有多个,那么肯定是不可到达的,所以用并查集处理一下。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <stack>
using namespace std; #define N 1005 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n, m;
bool in[N];
int suo[N];
int cnt;
vector<int>ve[N];
int dfn[N], low[N], Time;
stack<int>st;
int father[N]; int findroot(int u){
if(father[u]!=u) father[u]=findroot(father[u]);
return father[u];
} void tarjan(int u){
dfn[u]=low[u]=Time++;
st.push(u);in[u]=true;
int i, j, k;
for(i=;i<ve[u].size();i++){
int v=ve[u][i];
if(dfn[v]==-){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(in[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
while(){
int x=st.top();
suo[x]=cnt;
in[x]=false;
st.pop();
if(x==u||st.empty()) break;
}
cnt++;
}
} main()
{
int i, j, k, u, v;
int t;
cin>>t; while(t--){
scanf("%d %d",&n,&m);
for(i=;i<=n;i++) ve[i].clear();
for(i=;i<m;i++){
scanf("%d %d",&u,&v);
ve[u].push_back(v);
}
Time=cnt=;
memset(dfn,-,sizeof(dfn));
while(!st.empty()) st.pop();
memset(in,false,sizeof(in));
for(i=;i<=n;i++){
if(dfn[i]==-){
tarjan(i);
}
}
int inn[N], out[N];
memset(inn,,sizeof(inn));
memset(out,,sizeof(out));
for(i=;i<=n;i++) father[i]=i;
for(i=;i<=n;i++){
for(j=;j<ve[i].size();j++){
int u=suo[i], v=suo[ve[i][j]];
if(u!=v){
father[findroot(v)]=findroot(u);
inn[v]++;
out[u]++;
}
}
} int num=;
for(i=;i<cnt;i++){
if(father[i]==i) num++;
}
if(num>) {
printf("No\n");continue;
} int n1, n2;
n1=n2=;
for(i=;i<cnt;i++){
if(!inn[i]) n1++;
if(!out[i]) n2++;
}
if(n1>||n2>) printf("No\n");
else printf("Yes\n");
}
}
POJ 2762 tarjan缩点+并查集+度数的更多相关文章
- poj 2762(tarjan缩点+判断是否是单链)
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19234 ...
- poj 2762 tarjan缩点+拓扑序
2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- 《程序员代码面试指南》第三章 二叉树问题 Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题
题目待续.... Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题 java代码
- POJ 3694 (tarjan缩点+LCA+并查集)
好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
- POJ 1182 (经典食物链 /并查集扩展)
(參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
随机推荐
- FlashPlayer for Android
1. Manually install on Android devices 教程地址:“https://helpx.adobe.com/flash-player/kb/installing-flas ...
- Android应用程序构成
一个Android应用程序一般是由以下4个组件构成的: 活动(Activity) 意图(Intent) 服务(Service) 内容提供器(Content Provider) 这4个组件是构成andr ...
- POJ 2385 Apple Catching
比起之前一直在刷的背包题,这道题可以算是最纯粹的dp了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...
- Illegal mix of collations (big5_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation 'like'
解释: 非法的混合排序规则(big5_chinese_ci)和(utf8_general_ci)操作“like”. 原本是单个字段查询数据的,现在是把所有的字段用一个搜索框来查询. 主要出问题是下列这 ...
- IIS_Mvc发布
网站发布步骤: 这部分是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接 ...
- hiho_1078_线段树区间修改
题目 给定一组数,要求进行若干次操作,这些操作可以分为两种类型: (1) CMD 1 beg end value 将数组中下标在[beg, end] 区间内数字都变为value (2) CMD 2 b ...
- Jni碰到的一个异常
Java与C++都有String对象,而c没有,只有char类型,所以在向C传入String类型的时候,如何处理需要注意一点 jstring Java_com_skymaster_hs_test4_M ...
- 第三章 Python容器:列表、元组、字典与集合
数据结构的分类依据?基本的"数组"在python中是列表, 数据结构的作用?容器,盛放数据,是由原子组成的分子.可以将一群数据进行整合.拆分.重排. 3.2 列表 列表是啥?顺 ...
- css制作简单的导航栏
//css代码 #menu{ height: 65px; width:100%; background-color: rgba(0, 0, 0, 0.5);}#menu ul{ list-style: ...
- DI 之 3.3 更多DI的知识(柒)
3.3.3 自动装配 3.3.4 依赖检查 3.3.5 方法注入