poj2375 强连通
题意:有一个 l * w 大小的滑雪场,每个格子都有一个高度,每个格子可以直接通到上下左右四个格子中高度小于等于自己的格子,现在要建立通道,能够连通任意两个格子,问最少建多少通道能够使所有格子能够互相到达。
其实就是问加多少条边能够使整个图强连通,也就是求强连通分量中入度为 0 和出度为 0 的分量个数的最大值,如果仅一个强连通分量则为 0 。
RE 10 发,我以为是因为我链式前向星数组开太大,于是换邻接矩阵,又 RE,一看DISCUSS,G++RE,C++AC,一交C++A了,把第一次RE的交了一发A了,所以RE的小伙伴们……交C++吧……
链式前向星:
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std; const int maxn=;
const int maxm=1e6+; int head[maxn],point[maxm],nxt[maxm],size;
int n,t,scccnt;
int stx[maxn],low[maxn],scc[maxn];
int id[maxn],od[maxn];
int ma[][];
int xx[]={,-,,};
int yy[]={,,,-};
stack<int>S; int max(int a,int b){return a>b?a:b;} void init(){
memset(head,-,sizeof(head));
size=;
} void add(int a,int b){
point[size]=b;
nxt[size]=head[a];
head[a]=size++;
} void dfs(int s){
stx[s]=low[s]=++t;
S.push(s);
for(int i=head[s];~i;i=nxt[i]){
int j=point[i];
if(!stx[j]){
dfs(j);
low[s]=min(low[s],low[j]);
}
else if(!scc[j]){
low[s]=min(low[s],stx[j]);
}
}
if(low[s]==stx[s]){
scccnt++;
while(){
int u=S.top();S.pop();
scc[u]=scccnt;
if(s==u)break;
}
}
} void setscc(){
memset(stx,,sizeof(stx));
memset(scc,,sizeof(scc));
t=scccnt=;
for(int i=;i<=n;++i)if(!stx[i])dfs(i);
for(int i=;i<=n;++i){
for(int j=head[i];~j;j=nxt[j]){
int k=point[j];
if(scc[i]!=scc[k]){
od[scc[i]]++;
id[scc[k]]++;
}
}
}
} int main(){
int w,l;
scanf("%d%d",&w,&l);
n=w*l;
init();
for(int i=;i<=l;++i){
for(int j=;j<=w;++j){
scanf("%d",&ma[i][j]);
}
}
for(int i=;i<=l;++i){
for(int j=;j<=w;++j){
for(int k=;k<;++k){
int x=i+xx[k],y=j+yy[k];
if(x>=&&x<=l&&y>=&&y<=w&&ma[x][y]<=ma[i][j]){
add((i-)*w+j,(x-)*w+y);
}
}
}
}
setscc();
if(scccnt==)printf("0\n");
else{
int in=,out=;
for(int i=;i<=scccnt;++i){
if(!id[i])in++;
if(!od[i])out++;
}
printf("%d\n",max(in,out));
}
}
邻接矩阵:
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std; const int maxn=*; int n,t,scccnt;
int w,l;
int stx[maxn],low[maxn],scc[maxn];
int id[maxn],od[maxn];
int ma[][];
int xx[]={,-,,};
int yy[]={,,,-};
stack<int>S; int max(int a,int b){return a>b?a:b;} inline int getid(int a,int b){
return (a-)*w+b;
} inline void getpoint(int num,int &a,int &b){
a=num/w;
b=num%w;
if(b)a++;
else b=w;
} void dfs(int s){
stx[s]=low[s]=++t;
S.push(s);
int x,y;
getpoint(s,x,y);
for(int p=;p<;++p){
int dx=x+xx[p],dy=y+yy[p];
if(dx>=&&dx<=l&&dy>=&&dy<=w&&ma[dx][dy]<=ma[x][y]){
int j=getid(dx,dy);
if(!stx[j]){
dfs(j);
low[s]=min(low[s],low[j]);
}
else if(!scc[j]){
low[s]=min(low[s],stx[j]);
}
}
}
if(low[s]==stx[s]){
scccnt++;
while(){
int u=S.top();S.pop();
scc[u]=scccnt;
if(s==u)break;
}
}
} void setscc(){
memset(stx,,sizeof(stx));
memset(scc,,sizeof(scc));
t=scccnt=;
for(int i=;i<=n;++i)if(!stx[i])dfs(i);
for(int i=;i<=n;++i){
int x,y;
getpoint(i,x,y);
for(int p=;p<;++p){
int dx=x+xx[p],dy=y+yy[p];
if(dx>=&&dx<=l&&dy>=&&dy<=w&&ma[dx][dy]<=ma[x][y]){
int k=getid(dx,dy);
if(scc[i]!=scc[k]){
od[scc[i]]++;
id[scc[k]]++;
}
}
}
}
} int main(){
scanf("%d%d",&w,&l);
n=w*l;
memset(id,,sizeof(id));
memset(od,,sizeof(od));
for(int i=;i<=l;++i){
for(int j=;j<=w;++j){
scanf("%d",&ma[i][j]);
}
}
setscc();
if(scccnt==)printf("0\n");
else{
int in=,out=;
for(int i=;i<=scccnt;++i){
if(!id[i])in++;
if(!od[i])out++;
}
printf("%d\n",max(in,out));
}
}
poj2375 强连通的更多相关文章
- POJ2375 Cow Ski Area (强连通)(缩点)
Cow Ski Area Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- HDU5934 强连通分量
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5934 根据距离关系建边 对于强连通分量来说,只需引爆话费最小的炸弹即可引爆整个强连通分量 将所有的强连通分 ...
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
- 有向图的强连通分量的求解算法Tarjan
Tarjan算法 Tarjan算法是基于dfs算法,每一个强连通分量为搜索树中的一颗子树.搜索时,把当前搜索树中的未处理的结点加入一个栈中,回溯时可以判断栈顶到栈中的结点是不是在同一个强连通分量中.当 ...
- The Bottom of a Graph-POJ2553强连通
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9759 Accepted: 4053 ...
- Tarjan算法--强连通分量
tarjan的过程就是dfs过程. 图一般能画成树,树的边有三种类型,树枝边 + 横叉边(两点没有父子关系) + 后向边(两点之间有父子关系): 可以看到只有后向边能构成环,即只有第三张图是强连通分量 ...
- bzoj 1051 (强连通) 受欢迎的牛
题目:这里 题意: Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为 ...
- 强连通分量的一二三 | | JZOJ【P1232】 | | 我也不知道我写的什么
贴题: 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间之 ...
- 有向图强连通分量的Tarjan算法
有向图强连通分量的Tarjan算法 [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G ...
随机推荐
- Line计划今年全面进军中国市场:建立本地团队
北京时间6月13日下午消息,<华尔街日报>报道称,移动消息应用Line计划于今年晚些时候进军中国市场.Line将在中国建立本地团队,开发内容和功能,从而进一步开拓中国这一全球最大的移动市场 ...
- 获取一个 app 的 URL Scheme 的方法:
获取一个 app 的 URL Scheme 的方法: 上这个网站 URL Schemes 查一下相应的 app 的 URL Scheme 是否有被收录 第一种方法没找到的话,把相应的 app 的 ip ...
- Ubuntu 14.10 下设置静态IP
修改 /etc/network/interfaces 文件 sudo nano /etc/network/interfaces 修改为 # 前面的不变auto eth0 iface eth0 inet ...
- 重拾java系列一java基础(1)
前言,不知不觉,从接触java到工作至今已有两年的时间,突然感觉自己的基础知识还很薄弱,有些知识虽然知道,但是停留在表面上,没有深挖,或者实践过,感觉掌握的很肤浅,而且时间一长,就觉得忘记了,我觉得这 ...
- C++11 std::copy
这个函数并不是简单的 while(first != last) { *result = *first; result++; first++; } 事实上这种写法是最具普适性的,值要求inputIter ...
- (spring-第10回【IoC基础篇】)InstantiationStrategy--实例化Bean的第三大利器
Bean的实例化整个过程如下图: : 其中,BeanDefinition加入到注册表中,并由BeanFactoryPostProcessor的实现类处理后,需要由InstantiationStrate ...
- 关于GSMMAP分支cell_log扫描不正常问题的解决办法
阔别多年,本周在KALI 2.0下重拾旧时趣味,可怎么折腾都未曾见ARFCN,迫不得已还刷了brust_ind分支 才达到目的.后经仔细翻阅官方文档发现此问题早有披露,解决方案也已经公布,逐分享给大家 ...
- CodeForces 56E-Domino Principle
E - Domino Principle Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- HDU3952-几何
题意:给n个水果,每个水果包括m个点(即m条边),判断一刀能切的最多的水果数目: 思路:数据比较小,n <= 10,m <= 10;可以暴力枚举,枚举两个水果的任意两个点,连成一条直线,然 ...
- linux centos安装编译phantomjs 2.0的方法
phantomjs 2.0最新版的官方不提供编译好的文件下载,只能自己编译,有教程但是过于简单,特别是服务器上要安装N多的支持.折腾到现在终于装好了并且能正常运行了,截图mark一下: linux c ...