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这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
随机推荐
- Google账户_GooglePlay_关联
PS:这过程叫一个折腾...穷逼的无奈啊... 1. 1.1. 网上搜到说,安装 google play & google框架服务,手机需要root,没去证实这个事情... 用了之前的 And ...
- POJ2676,HDU4069解决数独的两种实现:DFS、DLX
搜索实现:解决数独有两种思考策略,一种是枚举当前格能填的数字的种数,这里有一优化策略就是先搜索能填入种数小的格子:另一种是考虑处理某一行(列.宫)时,对于某一个没用过的数字,若该行(列.宫)只有一个可 ...
- ean13码的生成,python读取csv中数据并处理返回并写入到另一个csv文件中
# -*- coding: utf-8 -*- import math import re import csv import repr def ean_checksum(eancode): &quo ...
- php提示:Call to undefined function curl_init
我要利用curl函数进行数据采集时发现提示Call to undefined function curl_init错误了,后来从官网了解到因为curl默认不是php开启的函数,我们需要手工打开哦,下面 ...
- TortoiseSvn
TortoiseSVN 是svn版本控制系统的一个免费开源客户端,它是svn版本控制的 Windows 扩展.可以使你避免使用枯燥而且不方便的命令行.它完全嵌入 Windows Explorer,使用 ...
- 一些android系统参数的获取
//获取网络类型 2G/3G/WIFI public String getNetworkType(){ String mNetWorkType = ""; Connectivity ...
- C++——友元、异常和其他
一.友元 类并非只能拥有友元函数,也可以将类作为友元.在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数. ...
- js 重点 (转载)
- 【CDN】海外免费加速CDN:Incapsula,CloudFare
最近服务器要搬迁到香港,因为后续有国外用户使用,基于此要使用大陆和海外都比较好的cdn才好 一开始国外同事推荐CloudFare,后来看看效果开始使用Incapsula CloudFare 官网:ht ...
- Python开发者须知 —— Bottle框架常见的几个坑
Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...