hdu 1269 求连通图的模板题
#include<stdio.h>
#include<string.h>
#include<iostream>//只存在一个连通分量
#include<string.h>
using namespace std;
#define N 11000
#define NN 110000
struct node {//链表实现
int v,next;
}bian[NN];
int sta[N];//数组模拟寨
int top,dfn[N],low[N],yong,visit[N],head[N],index,ans;
void addedge(int u,int v) {
bian[yong].v=v;
bian[yong].next=head[u];
head[u]=yong++;
}
int min(int a,int b) {
return a>b?b:a;
}
void tarjan(int u) {//tarjan算法
int i;
dfn[u]=low[u]=++index;
sta[++top]=u;
visit[u]=1;
for(i=head[u];i!=-1;i=bian[i].next) {
int v=bian[i].v;
if(!dfn[v]) {
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
if(visit[v]==1)
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]) {
int t;
ans++;
do{
t=sta[top--];
visit[t]=2;
}while(t!=u);
}
}
int main() {
int n,m,i,j,k;
while(scanf("%d%d",&n,&m),n||m) {
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(visit,0,sizeof(visit));
yong=0;
top=0;
memset(sta,0,sizeof(sta));
while(m--) {
scanf("%d%d",&i,&j);
addedge(i,j);
}
ans=0;
index=0;
for(i=1;i<=n;i++)
if(visit[i]!=2) {
tarjan(i);
if(ans>1)//如果有多个连通分量就直接退出
break;
}
if(ans>1)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
hdu 1269 求连通图的模板题的更多相关文章
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
hdu 2544 求点1到点n的最短路 无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- hdu1115 Lifting the Stone(几何,求多边形重心模板题)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1115">http://acm.hdu.edu.cn/showproblem.php ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2544 最短路(模板题——Floyd算法)
题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...
- HDU 3966 Aragorn's Story(模板题)【树链剖分】+【线段树】
<题目链接> 题目大意: 给定一颗带点权的树,进行两种操作,一是给定树上一段路径,对其上每个点的点权增加或者减少一个数,二是对某个编号点的点权进行查询. 解题分析: 树链剖分的模板题,还不 ...
- HDU 1711Number Sequence【KMP模板题】
<题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...
- HDU 1114 Piggy-Bank(完全背包模板题)
完全背包模板题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...
随机推荐
- bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡【dp+高斯消元】
算是比较经典的高斯消元应用了 设f[i]为i点答案,那么dp转移为f[u]=Σf[v]*(1-p/q)/d[v],意思是在u点爆炸可以从与u相连的v点转移过来 然后因为所有f都是未知数,高斯消元即可( ...
- [Swift通天遁地]一、超级工具-(9)在地图视图MKMapView中添加支持交互动作的标注图标
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- ssh&远程桌面连接工具finalshell
无意间发现的一款工具,有兴趣的可以看看点我进入官网 百度云盘 链接:https://pan.baidu.com/s/1wMuGav64e2zV91QznBkvag 密码:zpyb软件特点直接搬运的官方 ...
- GIt学习之路 第二天 创建版本库
本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 创建版本库 阅读: 1859216 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文 ...
- 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game
题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...
- Html 标签的事件绑定(转自 MSDN)
参考 MSDN 网页给 HTML 标签绑定 click 事件: function makeFoldersCollapsible(folderIcon, openFolderIcon, pathToIc ...
- log4j2异步日志解读(二)AsyncLogger
前文已经讲了log4j2的AsyncAppender的实现[log4j2异步日志解读(一)AsyncAppender],今天我们看看AsyncLogger的实现. 看了这个图,应该很清楚AsyncLo ...
- arduino 字符解析
Arduino String.h库函数详解 此库中包含1 charAT()2 compareTo()3 concat()4 endsWith()5 equals()6 equalslgnoreCa ...
- 什么 是JUnit?
JUnit详解: http://www.cnblogs.com/eggbucket/archive/2012/02/02/2335697.html
- Angular——作用域
基本介绍 应用App是无法嵌套的,但是controller是可以嵌套的,每个controller都会对应一个模型(model)也就是$scope对象,不同层级的controller下的$scope遍产 ...