洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom
题目大意:形成一个环的牛可以跳舞,几个环连在一起是个小组,求几个小组。
题解:tarjian缩点后,求缩的点包含的原来的点数大于1的个数。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 10009
using namespace std; int n,m,sumedge,top,sumclr,tim,ans;
int Stack[maxn],instack[maxn],low[maxn],dfn[maxn],cnt[maxn],head[maxn]; struct Edge{
int x,y,nxt;
Edge(int x=,int y=,int nxt=):
x(x),y(y),nxt(nxt){}
}edge[maxn*]; void add(int x,int y){
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
} void Tarjian(int x){
Stack[++top]=x;instack[x]=true;
low[x]=dfn[x]=++tim;
for(int i=head[x];i;i=edge[i].nxt){
int v=edge[i].y;
if(instack[v])low[x]=min(low[x],dfn[v]);
else if(!dfn[v]){
Tarjian(v);low[x]=min(low[x],low[v]);
}
}
if(low[x]==dfn[x]){
sumclr++;
while(Stack[top+]!=x){
instack[Stack[top]]=false;
cnt[sumclr]++;
top--;
}
}
} int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
for(int i=;i<=n;i++)if(!dfn[i])Tarjian(i);
for(int i=;i<=sumclr;i++)if(cnt[i]>)ans++;
printf("%d\n",ans);
return ;
}
AC
洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom的更多相关文章
- 洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom
https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so exci ...
- 洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom
代码是粘的,庆幸我还能看懂. #include<iostream> #include<cstdio> #include<cmath> #include<alg ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom 题解
每日一题 day11 打卡 Analysis 好久没大Tarjan了,练习练习模板. 只要在Tarjan后扫一遍si数组看是否大于1就好了. #include<iostream> #inc ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom(Tarjan)
一道tarjan的模板水题 在这里还是着重解释一下tarjan的代码 #include<iostream> #include<cstdio> #include<algor ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom-强连通分量(Tarjan)
本来分好组之后,就确定好了每个人要学什么,我去学数据结构啊. 因为前一段时间遇到一道题是用Lca写的,不会,就去学. 然后发现Lca分为在线算法和离线算法,在线算法有含RMQ的ST算法,前面的博客也写 ...
- P2863 [USACO06JAN]牛的舞会The Cow Prom
洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's ...
- bzoj1654 / P2863 [USACO06JAN]牛的舞会The Cow Prom
P2863 [USACO06JAN]牛的舞会The Cow Prom 求点数$>1$的强连通分量数,裸的Tanjan模板. #include<iostream> #include&l ...
- luogu P2863 [USACO06JAN]牛的舞会The Cow Prom |Tarjan
题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...
- 【luogu P2863 [USACO06JAN]牛的舞会The Cow Prom】 题解
题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include ...
随机推荐
- ASP.NET MVC CheckBoxFor的int to bool
当我们使用CheckBoxFor类型需要使用bool ,可以将 int转换成bool <div class="form-group"> <label class= ...
- Maven的SSM框架配置文件:
applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- ES6 实现阶乘
// 实现一个5的阶乘function factorial(n, acc = 1) { console.log(`n=${n};acc=${acc}`) if(n <= 1) return ac ...
- Logback Pattern 日志格式配置
Logback日志配置示例 <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppe ...
- 【bzoj3170】[Tjoi2013]松鼠聚会(数学题)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3170 这道题要在n个点中求一个点使其他点到该点的切比雪夫距离最小. 有个结论:$ (x ...
- thinkphp Composer安装指南
1.首先我们去composer的官网下载composer,当然也可以用命令行的形势下下载,我是在windows安装的.https://www.phpcomposer.com/ 2.下载以后进行安装,一 ...
- springboot处理session生命周期
在使用springboot开发过程中发现用户登陆后60s后session就自动失效了,需要重新登陆,明明 application.yml 文件里已经配置了 server.session.timeou ...
- Hibernate -- 对象关系映射基础
- 《Computational Statistics with Matlab》硬译2
T=; sigma=; thetamin=-;thetamax=; theta=zeros(,T); seed=;rand('state',seed);randn('state',seed); the ...
- 通过application.properties配置SpringBoot项目
application.properties可以自己新建,放在这里:(该文件可以放在4个地方,详情百度) 在文件中添加:file_path=E://Tools//apache-tomcat-9.0.1 ...