HDU1269(有向图缩点模板题)
迷宫城堡
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11070 Accepted Submission(s): 4948
#include"cstdio"
#include"cstring"
#include"vector"
using namespace std;
const int MAXN=;
vector<int> G[MAXN];
vector<int> rG[MAXN];
vector<int> vs;
int V,E;
int vis[MAXN];
inline int max(int a,int b)
{
return a > b? a: b;
}
void dfs(int u)
{
vis[u]=;
for(int i=;i<G[u].size();i++)
if(!vis[G[u][i]]) dfs(G[u][i]);
vs.push_back(u);
}
void rdfs(int u)
{
vis[u]=;
for(int i=;i<rG[u].size();i++)
if(!vis[rG[u][i]]) rdfs(rG[u][i]);
}
bool scc()
{
memset(vis,,sizeof(vis));
for(int i=;i<=V;i++)
if(!vis[i]) dfs(i);
memset(vis,,sizeof(vis));
int k=;
for(int i=vs.size()-;i>=;i--)
if(!vis[vs[i]])
{
rdfs(vs[i]);
k++;
if(k>) return false;
}
return true;
}
int main()
{
while(scanf("%d%d",&V,&E)!=EOF&&V)
{
vs.clear();
for(int i=;i<=V;i++)
{
G[i].clear();
rG[i].clear();
}
for(int i=;i<E;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
rG[v].push_back(u);
} if(scc())
{
printf("Yes\n");
}
else
{
printf("No\n");
}
} return ;
}
模板2:tarjan
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=;
vector<int> mp[MAXN];
int n,m;
int dfn[MAXN],low[MAXN],time;
int stack[MAXN],top;
bool ins[MAXN];
int cnt=;
void tarjan(int u)
{
dfn[u]=low[u]=++time;
stack[top++]=u;
ins[u]=true;
for(int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int v;
cnt++;
do{
v=stack[--top];
ins[v]=false;
}while(u!=v);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)
break;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(ins,false,sizeof(ins));
time=;
top=;
cnt=;
for(int i=;i<=n;i++)
mp[i].clear();
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u].push_back(v);
} for(int i=;i<=n;i++)
{
if(!dfn[i])
{
tarjan(i);
}
} if(cnt==)
printf("Yes\n");
else
printf("No\n");
}
}
该题目也可用并查集做。
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
int V,E;
int par[][MAXN];//par[0][MAXN]记录正向边,par[1][MAXN]记录反向边
int fnd(int x,int type)
{
if(par[type][x]==x)
{
return x;
}
return par[type][x]=fnd(par[type][x],type);
}
void unite(int x,int y)
{
if(x>) par[][x]=fnd(y,);//将结点1作为根结点
if(y>) par[][y]=fnd(x,);
}
bool judge()
{
for(int i=;i<=V;i++)
if(fnd(i,)!=||fnd(i,)!=) return false;//若所有结点(除1)沿正向边均能到达1结点且将边反向后也均能到达1结点,那么改图为强连通图
return true;
}
int main()
{
while(scanf("%d%d",&V,&E)!=EOF&&V)
{
for(int i=;i<=V;i++) par[][i]=par[][i]=i;
for(int i=;i<E;i++)
{
int u,v;
scanf("%d%d",&u,&v);
unite(u,v);
}
if(judge()) printf("Yes\n");
else printf("No\n");
} return ;
}
HDU1269(有向图缩点模板题)的更多相关文章
- HDU-3549 最大流模板题
1.HDU-3549 Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)
HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...
- [USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
- LuoGu-P2863牛的舞会The Cow Prom[tarjan 缩点模板]
传送门:https://www.luogu.org/problemnew/show/P2863 思路:tarjan模板题,之前会的tarjan,一直想学缩点到底是什么操作,发现就是把同组的放在一个数组 ...
- POJ 2186:Popular Cows Tarjan模板题
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25945 Accepted: 10612 De ...
- 对Tarjan——有向图缩点算法的理解
开始学tarjan的时候,有关无向图的割点.桥.点双边双缩点都比较容易地理解了,唯独对有向图的缩点操作不甚明了.通过对luoguP2656_采蘑菇一题的解决,大致搞清了tarjan算法的正确性. 首先 ...
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
随机推荐
- 关于HDFS NFS3的配置
1.在core-site.xml中配置 <property> <name>hadoop.proxyuser.root.groups</name> <value ...
- Array容易被忽略的join
var lists, items = '', i; lists = [{ Fruits:'苹果' },{ Fruits:'香蕉' },{ Fruits:'菠萝' }]; /*items += '< ...
- mongodb的mongod.lock文件及oplog文件
在mongodb的启动时,在数据目录下,会生成一个mongod.lock文件.如果在正常退出时,会清除这个mongod.lock文件,若要是异常退出,在下次启动的时候,会禁止启动,从而保留一份干净的一 ...
- 查看并修改Linux主机名命令hostname
查看主机名 hostname可以查看主机名 export也可以查看 修改主机名 echo new-hostname > /proc/sys/kernel/hostname (系统启动时,从此文件 ...
- diy文件系统上创建文件的流程
[0]README 0.1) source code are from orange's implemention of a os , and for complete code , please v ...
- 目标检测之vibe---ViBe(Visual Background extractor)背景建模或前景检测
ViBe算法:ViBe - a powerful technique for background detection and subtraction in video sequences 算法官网: ...
- iOS 逆向 - Class-dump 安装和使用方法
1.下载安装包 http://stevenygard.com/projects/class-dump/,这里我下载的是 class-dump-3.5.dmp.然后把下载下来的 dmg 打开,复制文件里 ...
- bmdiff snappy lzw gzip
https://github.com/google/snappy Introduction [速度第一,压缩比适宜] [favors speed over compression ratio] Sna ...
- consistence availability partition tolerance quit
理论证明
- swift开发学习笔记-闭包
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jiangqq781931404/article/details/32913421 文章转自:http ...