当前位置:网站首页>[Luogu] p2341 popular cattle
[Luogu] p2341 popular cattle
2022-07-26 23:19:00 【Record algorithm solution】
Title address :
https://www.luogu.com.cn/problem/P2341
Title Description :
Every cow dreams of being a star in the cowshed . The cow that all cows like is a star cow . All cows are narcissists , Every cow always likes its own . Between cows “ like ” It can be transmitted —— If A A A like B B B, B B B like C C C, that A A A I also like C C C. It's in the bullpen N N N A cow , Given some of the relationships between cows , Please figure out how many cows can be stars .
Input format :
first line : Two integers separated by spaces : N N N and M M M.
Next M M M That's ok : Two integers separated by spaces on each line : A A A and B B B, Express A A A like B B B.
Output format :
A single integer in a row , The number of star cows .
Data range :
about 10 % 10\% 10% The data of , N ≤ 20 N\le20 N≤20, M ≤ 50 M\le50 M≤50.
about 30 % 30\% 30% The data of , N ≤ 1 0 3 N\le10^3 N≤103, M ≤ 2 × 1 0 4 M\le2\times 10^4 M≤2×104.
about 70 % 70\% 70% The data of , N ≤ 5 × 1 0 3 N\le5\times 10^3 N≤5×103, M ≤ 5 × 1 0 4 M\le5\times 10^4 M≤5×104.
about 100 % 100\% 100% The data of , 1 ≤ N ≤ 1 0 4 1\le N\le10^4 1≤N≤104, 1 ≤ M ≤ 5 × 1 0 4 1\le M\le5\times 10^4 1≤M≤5×104.
It can be used Tarjan The algorithm shrinks the graph first , In this way, we can see that the output after shrinking point is 0 0 0 How many points of , If it's not the only one , No cow can be a star . Otherwise, the exposure is 0 0 0 Point of size that will do . Train of thought reference https://blog.csdn.net/qq_46105170/article/details/116681582. The code is as follows :
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e4 + 10, M = 5e4 + 10;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int stk[N], top;
bool in_stk[N];
int id[N], scc_cnt, sz[N];
int dout[N];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++timestamp;
stk[top++] = u, in_stk[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (in_stk[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
scc_cnt++;
int y;
do {
y = stk[--top];
in_stk[y] = false;
id[y] = scc_cnt;
sz[scc_cnt]++;
} while (y != u);
}
}
int main() {
memset(h, -1, sizeof h);
scanf("%d%d", &n, &m);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= n; i++)
for (int j = h[i]; ~j; j = ne[j])
if (id[i] != id[e[j]])
dout[id[i]]++;
int zero = 0, res;
for (int i = 1; i <= scc_cnt; i++) {
if (!dout[i]) {
if (++zero > 1) {
res = 0;
break;
}
res = sz[i];
}
}
printf("%d\n", res);
}
Time complexity O ( N + M ) O(N+M) O(N+M), Space O ( N ) O(N) O(N).
边栏推荐
- Weilai cup 2022 Niuke summer multi school training camp 2
- Siliwei's counterattack: huiding's under screen optical fingerprint patent involved in the case was declared invalid
- Xu Li, CEO of Shangtang Technology: the company's valuation has exceeded $7billion, so we are not in a hurry to go public
- Calendar documents implemented by several qwidgets
- 公有云安全性和合规性方面的考虑事项
- 实战项目:Boost搜索引擎
- 基本的SELECT语句
- ZTE: more than 50000 5g base stations have been shipped worldwide!
- PostgreSQL 与 Navicat:数据库行业的中坚力量
- Incremental secure file system SFS based on C language design
猜你喜欢

HCIA-R&S自用笔记(20)VLAN综合实验、GVRP

Monte Carlo search tree (UCT) based on confidence upper bound to realize four sub chess

Disk expansion process and problems encountered in the virtual machine created by VMWare

Programmer growth chapter 29: how to motivate employees?

Plato farm is expected to further expand its ecosystem through elephant swap
![[postgresql]postgresqlg使用enerate_series() 函数补全统计](/img/62/893986eb97a61f4e9ef32abc8d2a90.png)
[postgresql]postgresqlg使用enerate_series() 函数补全统计

Kt6368a Bluetooth chip development precautions and problem collection - long term update

Eureka basic use

Vector execution engine framework gluten announced the official open source and appeared at spark technology summit

Hcia-r & s self use notes (23) DHCP
随机推荐
数据库全栈工程师(DevDBOps)低首付、高回报,先就业后付款
New employees of black maredge takeout
org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line
8-其他编程语言--记录
基于gRPC编写golang简单C2远控
PostgreSQL 与 Navicat:数据库行业的中坚力量
Kingbasees database administrator's Guide -- 11 manage data files and temporary files
ZTE: more than 50000 5g base stations have been shipped worldwide!
Shardingsphere JDBC keyword problem
Use ECs and OSS to set up personal network disk
Write golang simple C2 remote control based on grpc
Dao:op token and non transferable NFT are committed to building a new digital democracy
P5469 [noi2019] robot (Lagrange interpolation, interval DP)
每周招聘|PostgreSQL数据库研发工程师,年薪60+,名企高薪,挑战自我!
Vit:vision transformer super detailed with code
Huawei conspires to acquire Brazilian operators?
面试官问:JS的this指向
Public cloud security and compliance considerations
gateway基本使用
After closing the Suzhou plant, Omron Dongguan plant announced its dissolution, and more than 2000 people are facing unemployment!