题面

题解

\(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的更多相关文章

  1. 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom 题解

    每日一题 day11 打卡 Analysis 好久没大Tarjan了,练习练习模板. 只要在Tarjan后扫一遍si数组看是否大于1就好了. #include<iostream> #inc ...

  2. 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom

    传送门 题目大意:形成一个环的牛可以跳舞,几个环连在一起是个小组,求几个小组. 题解:tarjian缩点后,求缩的点包含的原来的点数大于1的个数. 代码: #include<iostream&g ...

  3. 洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom

    https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so exci ...

  4. 洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom

    代码是粘的,庆幸我还能看懂. #include<iostream> #include<cstdio> #include<cmath> #include<alg ...

  5. 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom(Tarjan)

    一道tarjan的模板水题 在这里还是着重解释一下tarjan的代码 #include<iostream> #include<cstdio> #include<algor ...

  6. 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom-强连通分量(Tarjan)

    本来分好组之后,就确定好了每个人要学什么,我去学数据结构啊. 因为前一段时间遇到一道题是用Lca写的,不会,就去学. 然后发现Lca分为在线算法和离线算法,在线算法有含RMQ的ST算法,前面的博客也写 ...

  7. P2863 [USACO06JAN]牛的舞会The Cow Prom

    洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's ...

  8. bzoj1654 / P2863 [USACO06JAN]牛的舞会The Cow Prom

    P2863 [USACO06JAN]牛的舞会The Cow Prom 求点数$>1$的强连通分量数,裸的Tanjan模板. #include<iostream> #include&l ...

  9. 【luogu P2863 [USACO06JAN]牛的舞会The Cow Prom】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include ...

  10. 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 ...

随机推荐

  1. 论文阅读笔记(十一)【ICCV2017】:Jointly Attentive Spatial-Temporal Pooling Networks for Video-based Person Re-Identification

    Introduction (1)Motivation: 当前采用CNN-RNN模型解决行人重识别问题仅仅提取单一视频序列的特征表示,而没有把视频序列匹配间的影响考虑在内,即在比较不同人的时候,根据不同 ...

  2. Quartz.NET常用方法 02

    上一篇里介绍了Job和Trigger的常用方法,这一节将介绍Calendar,它的作用是排除特定的日期时间.   Calendar的常用类 DailyCalendar 排除每天某个时间段任务的执行 例 ...

  3. jQuery---html方法和text方法

    html方法和text方法 //html:innerHTML text:innerText console.log($("div").html());//<h3>我是标 ...

  4. es报错org.frameworkset.elasticsearch.ElasticSearchException: {"error":{"root_cause":[{"type":"cluster_block_exception","reason":"blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];"}],

    今天es在保存数据的时候报错 org.frameworkset.elasticsearch.ElasticSearchException: {"error":{"root ...

  5. RN开发-ReactJS组件

    虚拟DOM :将真实的DOM结构虚拟成json类型数据    props : 不可改变,用于数据传递    state : 组件属性,主要用来存储组件自身需要的数据,每次改变都会引起组件的更新     ...

  6. C语言strcat()函数:字符串连接(拼接)

    C语言strcat()函数:字符串连接(拼接)   C语言 strcat() 函数用来将两个字符串连接(拼接)起来. 头文件:string.h 语法/原型: char*strcat(char* str ...

  7. 笔记(tm_springboot)

    1.@change="getFile($event)" 2.File imageFolder= new File(request.getServletContext().getRe ...

  8. 测试并整理的 Airpods Pro 刻字可用的最全特殊符号

    天气符号 ☉ 文化符号 卍卐 办公室符号 № ℗ ℡ ℀ ℅ ™ © ® ‰ ¶ § 技术符号 ⇧  打勾符号 ˇ ∨ √ 人的符号 ♀ ♂ ヅ ツ ü 星星符号 ☆ ★ ✽ 箭头符号 ↖ ↗ ↘ ...

  9. 【16】LRUChache

    题目 LRU 思路 LRU 大家都不陌生,操作系统的作业做过,思路就是一旦添加或者访问某个元素,则将其的"访问属性"置零,而其他元素的访问属性统统减一,这样一来,访问属性最小的元素 ...

  10. 【HTML】三种方法使HTML单页面输入密码才能访问

    方法一 <script type="text/javascript"> function password() { var testV = 1; var pass1 = ...