hdu 1269 迷宫城堡 (图的联通,tarjan)
2015-03-07 17:13
507 查看
题目链接:hdu 1269
tarjan水题。我来复习一下tarjan。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;
#define maxn (10000+10)
int N,M,tot,idx;
int head[maxn],dfn[maxn],low[maxn],belong[maxn];
bool vis[maxn];
struct node{int v,next;}e[100000+10];
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int k=0;
void adde(int u,int v){
e[k].v=v; e[k].next=head[u]; head[u]=k++;
}
stack<int>q;
void tarjan(int x){
dfn[x]=low[x]=++idx;
vis[x]=1; q.push(x);
for(int i=head[x];i!=-1;i=e[i].next){
int v=e[i].v;
if(!dfn[v]){
tarjan(v); low[x]=min(low[x],low[v]);
}
else if(vis[v])low[x]=min(low[x],dfn[v]);
}
if(dfn[x]==low[x]){//注意要在遍历完x的子树的所有节点后再判断
tot++; int now=0;
while(now!=x){
now=q.top(); q.pop();
belong[now]=tot; vis[now]=0;
}
}
}
int main(){
while(N=read()){
M=read();
memset(belong,0,sizeof(belong));
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
idx=0; tot=0; k=0;
for(int i=1;i<=M;i++){
int x=read(),y=read();
adde(x,y);
}
for(int i=1;i<=N;i++)if(!dfn[i])tarjan(i);
if(tot>1)printf("No\n");
else printf("Yes\n");
}
return 0;
}
tarjan水题。我来复习一下tarjan。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;
#define maxn (10000+10)
int N,M,tot,idx;
int head[maxn],dfn[maxn],low[maxn],belong[maxn];
bool vis[maxn];
struct node{int v,next;}e[100000+10];
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int k=0;
void adde(int u,int v){
e[k].v=v; e[k].next=head[u]; head[u]=k++;
}
stack<int>q;
void tarjan(int x){
dfn[x]=low[x]=++idx;
vis[x]=1; q.push(x);
for(int i=head[x];i!=-1;i=e[i].next){
int v=e[i].v;
if(!dfn[v]){
tarjan(v); low[x]=min(low[x],low[v]);
}
else if(vis[v])low[x]=min(low[x],dfn[v]);
}
if(dfn[x]==low[x]){//注意要在遍历完x的子树的所有节点后再判断
tot++; int now=0;
while(now!=x){
now=q.top(); q.pop();
belong[now]=tot; vis[now]=0;
}
}
}
int main(){
while(N=read()){
M=read();
memset(belong,0,sizeof(belong));
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
idx=0; tot=0; k=0;
for(int i=1;i<=M;i++){
int x=read(),y=read();
adde(x,y);
}
for(int i=1;i<=N;i++)if(!dfn[i])tarjan(i);
if(tot>1)printf("No\n");
else printf("Yes\n");
}
return 0;
}
相关文章推荐
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- [ACM] HDU 1269 迷宫城堡(Tarjan算法求强联通分量)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- hdu1269 迷宫城堡 tarjan求强联通分量
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- HDU 1269 迷宫城堡(tarjan强联通分量)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- HDU 1269:迷宫城堡 (Tarjan强连通分量)
- HDU 1269 迷宫城堡 Tarjan强连通分量
- HDU 1269 迷宫城堡 (有向图强连通分量Tarjan)
- hdu1269 迷宫城堡(tarjan入门)
- HDU 1269 迷宫城堡【Tarjan强连通分量 模板】
- HDU - 1269 迷宫城堡-tarjan-有向图强连通分量
- HDU:1269-迷宫城堡(tarjan模板)
- hdu 1269 迷宫城堡(tarjan强连通)