题解【洛谷P2863】 [USACO06JAN]牛的舞会The Cow Prom
题解
\(Tarjan\)板子题。
统计出大小大于\(1\)的强连通分量数量输出即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
int n, m, tot, head[200003], nxt[200003], ver[200003], low[200003], dfn[200003], num, sum, ans, sy[200003], ys[200003], kok;
int sta[200003], vis[200003], cnt;
inline void add(int u, int v)
{
ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
void Tarjan(int u)
{
dfn[u] = low[u] = ++num, sta[++cnt] = u, vis[u] = 1;
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (vis[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u])
{
++kok;
int y = -1;
do
{
y = sta[cnt];
vis[y] = 0;
sy[y] = kok;
++ys[kok];
--cnt;
} while (y != u);
}
}
int main()
{
//File("P2863");
n = gi(), m = gi();
for (int i = 1; i <= m; i+=1)
{
int u = gi(), v = gi();
add(u, v);
}
for (int i = 1; i <= n; i+=1) if (!dfn[i]) Tarjan(i);
for (int i = 1; i <= kok; i+=1)
{
if (ys[i] > 1) ++ans;
}
printf("%d\n", ans);
return 0;
}
题解【洛谷P2863】 [USACO06JAN]牛的舞会The Cow Prom的更多相关文章
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom 题解
每日一题 day11 打卡 Analysis 好久没大Tarjan了,练习练习模板. 只要在Tarjan后扫一遍si数组看是否大于1就好了. #include<iostream> #inc ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom
传送门 题目大意:形成一个环的牛可以跳舞,几个环连在一起是个小组,求几个小组. 题解:tarjian缩点后,求缩的点包含的原来的点数大于1的个数. 代码: #include<iostream&g ...
- 洛谷——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(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】 题解
题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include ...
- 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 ...
随机推荐
- 按钮控制彩灯实验 CSU - 1770 树状数组 差分变单点修改
#include<iostream> #include<algorithm> #include<cstring> using namespace std; ; in ...
- 牛客寒假6-C汉诺塔
链接:https://ac.nowcoder.com/acm/contest/3007/C来源:牛客网 题目描述 现在你有 N 块矩形木板,第 i 块木板的尺寸是 Xi*Yi,你想用这些木板来玩汉诺塔 ...
- pcl库卸载再重装
系统版本:ubuntu 16.04 sudo rm -r /usr/include/pcl-1.7 /usr/share/pcl /usr/bin/pcl* /usr/lib/libpcl* sudo ...
- 自定义React-redux
实现mini版react-redux 1. 理解react-redux模块 1). react-redux模块整体是一个对象模块 2). 包含2个重要属性: Provider和connect 3). ...
- ECMAScript基本对象——Boolean对象
1.创建 var myBoolean=new Boolean(); 2.方法 toString()把布尔值转换为字符串,并返回结果 valueOf()返回 Boolean 对象的原始值. 3.属性 1 ...
- 虚拟化技术xen的简介和安装
虚拟化技术的分类: 1,模拟:Emulation Qemu,PearPC,Bochs 2,完全虚拟化:Full Virtualization,Native Virtualization HVM ...
- 如何与GitHub同步,将本地文件push到到远程仓库
Run git config --global user.email "you@example.com" git config --global user.name "Y ...
- 视频中“5s后可跳过广告” 设计目的
来源:https://wen.woshipm.com/question/detail/0quoes.html 1.保证你在看.用户关掉广告这5秒内,他的眼睛会盯着屏幕,因为他知道5秒之后就能跳过广告. ...
- Pytest学习9-常用插件
pytest-django:为django应用程序编写测试. pytest-twisted:为twisted应用程序编写测试,启动反应堆并处理测试函数的延迟. pytest-cov:覆盖率报告,与分布 ...
- python3练习100题——033
链接:http://www.runoob.com/python/python-exercise-example33.html 题目:按逗号分隔列表. l=[1,2,3,4] k=1 for i in ...