The Cow Prom POJ - 3180

题意:

奶牛圆舞:N头牛,M条有向绳子,能组成几个歌舞团(团内奶牛数 n >= 2)?要求顺时针逆时针都能带动舞团内所有牛。

分析:

所谓能带动,就是舞团构成一个强连通分量,就是赤裸裸的SCC。

代码实现:很好的一道题,有利于理解 korasaju 算法

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define ms(a,b) memset(a,b,sizeof a)
const int maxn = 1e5 + 10; int V; // 顶点数
vector<int> G[maxn]; // 图的邻接表表示
vector<int> rG[maxn]; // 反向图
vector<int> vs; // 后序遍历顺序的顶点列表
bool book[maxn]; // 访问标记
int cmp[maxn]; // 所属强连通分量的拓补序 void add_edge(const int& from, const int& to) {
G[from].push_back(to);
rG[to].push_back(from);
} void dfs(const int& v) {
book[v] = true;
for (int i = 0; i < G[v].size(); ++i)
if (!book[G[v][i]])dfs(G[v][i]);
vs.push_back(v);
} void rdfs(const int& v, const int& k) {
book[v] = true; cmp[v] = k;
for (int i = 0; i < rG[v].size(); ++i)
if (!book[rG[v][i]])rdfs(rG[v][i], k);
} int scc() {
ms(book, false); vs.clear();
for (int v = 0; v < V; ++v)
if (!book[v])dfs(v);
ms(book, false); int k = 0;
for (int i = vs.size() - 1; i >= 0; --i)
if (!book[vs[i]])rdfs(vs[i], k++);
//cout << k << endl; //3个连通分量
return k;
} int main() {
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int m; cin >> V >> m;
for (int i = 0; i < m; ++i) {
int from, to; cin >> from >> to;
add_edge(--from, --to);
}
int n = scc();
vector<int>count(n, 0);
for (int v = 0; v < V; ++v) {
//cout << cmp[v] << " "; // 2 2 1 2 0
++count[cmp[v]];
}
//cout << endl;
int ans = 0;
for (int i = 0; i < n; ++i)
if (count[i] >= 2)++ans;
cout << ans << endl;
return 0;
}

POJ - 3180 The Cow Prom ( korasaju 算法模板)的更多相关文章

  1. poj 3180 The Cow Prom(强联通分量)

    http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  2. poj 3180 The Cow Prom(tarjan+缩点 easy)

    Description The N ( <= N <= ,) cows are so excited: it's prom night! They are dressed in their ...

  3. POJ 3180 The Cow Prom(SCC)

    [题目链接] http://poj.org/problem?id=3180 [题目大意] N头牛,M条有向绳子,能组成几个歌舞团?要求顺时针逆时针都能带动舞团内所有牛. [题解] 等价于求点数大于1的 ...

  4. POJ 3180 The Cow Prom(强联通)

    题目大意: 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞.           只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的 ...

  5. [poj] 3180 the cow prom

    原题 这是一道强连通分量板子题. 我们只用输出点数大于1的强连通分量的个数! #include<cstdio> #include<algorithm> #include< ...

  6. POJ 3180 The cow Prom Tarjan基础题

    题目用google翻译实在看不懂 其实题目意思如下 给一个有向图,求点个数大于1的强联通分量个数 #include<cstdio> #include<algorithm> #i ...

  7. POJ 2195 Going Home(KM算法模板)

    题目链接:http://poj.org/problem?id=2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致. man每移动一格需花费$1 ...

  8. POJ 3617 Best Cow Line 贪心算法

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26670   Accepted: 7226 De ...

  9. POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)

    题目链接:http://poj.org/problem?id=2186 题目大意:有n头牛和m对关系, 每一对关系有两个数(a, b)代表a牛认为b牛是“受欢迎”的,且这种关系具有传递性, 如果a牛认 ...

  10. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

随机推荐

  1. 吉客云与用友U8的系统数据集成对接方案

    吉客云与用友U8之间的系统数据集成方案.吉客云作为一款电商ERP产品,旨在为企业的数字化升级提供全方位的支持.用友U8是一个经过多年发展的信息化管理系统,见证了企业信息化从简单到精细.从局部到全面的转 ...

  2. 【Spring Boot】【外包杯】学习day02 | 快速搭建一个Spring Boot项目

    1.

  3. vertx的学习总结2

    一.什么是verticle verticle是vertx的基本单元,其作用就是封装用于处理事件的技术功能单元  (如果不能理解,到后面的实战就可以理解了) 二.写一个verticle 1. 引入依赖( ...

  4. 使用nacos配置,启动服务时一直报 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. APPLICATION FAILED TO START

    报错日志如下: Error starting ApplicationContext. To display the conditions report re-run your application ...

  5. 文心一言 VS 讯飞星火 VS chatgpt (156)-- 算法导论12.3 3题

    三.用go语言,对于给定的 n 个数的集合,可以通过先构造包含这些数据的一棵二叉搜索树(反复使用TREE-INSERT 逐个插入这些数),然后按中序遍历输出这些数的方法,来对它们排序.这个排序算法的最 ...

  6. JVM整理笔记

    1.JVM位置 JVM是作用在操作系统之上的,它与硬件没有直接的交互 2.JVM体系结构 3.类装载器ClassLoader 类装载器:负责加载class文件,class文件在文件开头有特定的文件标示 ...

  7. 中企网安信息科技:基于数据化大屏的BI数据分析管理系统概述

    由华企网安总公司北京中企网安信息科技有限责任公司开发的<基于数据化大屏的BI数据分析管理系统>,获得国家版权局颁发的计算机软件著作权登记证书. 基于数据化大屏的BI数据分析管理系统利用大数 ...

  8. 数字孪生技术结合GIS系统能在农业领域作出什么改变?

    数字孪生技术和地理信息系统(GIS)是两个独立但高度互补的领域,它们的结合在农业领域具有巨大的潜力,可以带来巨大的改变.在这篇文章中,我们将讨论数字孪生技术和GIS系统如何协同作用,为农业带来创新和可 ...

  9. ElasticSearch之Clone index API

    使用已有的索引,复制得到一个索引. 关闭testindex_001的写入操作,命令样例如下: curl -X PUT "https://localhost:9200/testindex_00 ...

  10. ElasticSearch之Nodes info API

    查看当前集群中各节点的信息,执行如下命令: curl -X GET "https://localhost:9200/_nodes?pretty" --cacert $ES_HOME ...