传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1051

保存tarjan模版。

求强联通分量,缩点。

#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm> const int maxn = 10005, maxm = 50005; int n, m, t1, t2;
int top[maxn], dfn[maxn], dfs_clock, scc[maxn], scc_cnt, stk[maxn], top_;
int head1[maxn], to1[maxm], next1[maxm], lb1;
int siz[maxn];
char out[maxn]; inline void ist1(int aa, int ss) {
to1[lb1] = ss;
next1[lb1] = head1[aa];
head1[aa] = lb1;
++lb1;
}
void dfs(int r) {
dfn[r] = top[r] = ++dfs_clock;
stk[top_++] = r;
for (int j = head1[r]; j != -1; j = next1[j]) {
if (!dfn[to1[j]]) {
dfs(to1[j]);
top[r] = std::min(top[r], top[to1[j]]);
}
else if (!scc[to1[j]]) {
top[r] = std::min(top[r], dfn[to1[j]]);
}
}
if (dfn[r] == top[r]) {
++scc_cnt;
while (stk[top_ - 1] != r) {
scc[stk[top_ - 1]] = scc_cnt;
--top_;
++siz[scc_cnt];
}
scc[r] = scc_cnt;
--top_;
++siz[scc_cnt];
}
} int main(void) {
//freopen("in.txt", "r", stdin);
memset(head1, -1, sizeof head1);
memset(next1, -1, sizeof next1);
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d", &t1, &t2);
ist1(t1, t2);
} for (int i = 1; i <= n; ++i) {
if (!scc[i]) {
dfs(i);
}
} for (int i = 1; i <= n; ++i) {
for (int j = head1[i]; j != -1; j = next1[j]) {
if (scc[i] != scc[to1[j]]) {
out[scc[i]] = 1;
}
}
} char flag = 0;
int ans = 0;
for (int i = 1; i <= scc_cnt; ++i) {
if (!out[i]) {
if (flag) {
ans = 0;
break;
}
else {
flag = 1;
ans = siz[i];
}
}
}
printf("%d\n", ans);
return 0;
}

  

_bzoj1051 [HAOI2006]受欢迎的牛【强联通】的更多相关文章

  1. [BZOJ1051] [HAOI2006] 受欢迎的牛 (强联通分量)

    Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...

  2. bzoj1051: [HAOI2006]受欢迎的牛(强联通)

    1051: [HAOI2006]受欢迎的牛 题目:传送门 题解: 今天又做一道水题... 强联通啊很明显 水个模板之后统计一下每个强联通分量中点的个数,再统计一下出度... 不难发现:缩点之后当且仅当 ...

  3. [BZOJ1051][HAOI2006] 受欢迎的牛 tarjan求联通分量

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5687  Solved: 3016[Submit][Sta ...

  4. 1051: [HAOI2006]受欢迎的牛

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2276  Solved: 1190[Submit][Sta ...

  5. 【BZOJ 1051】 1051: [HAOI2006]受欢迎的牛 (SCC)

    1051: [HAOI2006]受欢迎的牛 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如 ...

  6. P2341 [HAOI2006]受欢迎的牛(tarjan+缩点)

    P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的 ...

  7. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

  8. bzoj1051 [HAOI2006]受欢迎的牛

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4773  Solved: 2541[Submit][Sta ...

  9. 【BZOJ】1051: [HAOI2006]受欢迎的牛

    [HAOI2006]受欢迎的牛 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢 ...

随机推荐

  1. 利用WiFi Pineapple Nano渗透客户端获取SHELL

    前言: 前两篇文章介绍了The WiFi Pineapple Nano设备的一些主要功能模块,例如PineAP.SSLsplit和Ettercap等.今天给大家实际场景演示下如何利用Pineapple ...

  2. Highways POJ 2485【Prim】

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  3. HDU3459:Rubik 2&#215;2&#215;2(IDA)

    Problem Description Sonny is probably the only computer science Ph.D. student who cannot solve a Rub ...

  4. JavaSE入门学习23:Java面向对象之构造方法

    学了JavaSE面向对象这一部分,也该对构造方法做一个总结了. 一构造方法 在多数情况下,初始化一个对象的终于步骤是去调用这个对象的构造方法. 构造方法负责对象的初始化工作,为 实例变量赋予合适的初始 ...

  5. Linux_C——动态库,静态库

    /usr/lib /lib:标准系统库文件          库是一组预先编译好的函数的集合,这些函数都是按照可重用的原则编写的.它们通常有一组相互关联的函数组成以          执行某项常见的任 ...

  6. jetty9 web app的部署

    jetty9将web app和web app的context配置文件都放在${JETTY_HOME}/webapps下面. 例如,如果有一个myapp.war,首先将其放入${JETTY_HOME}/ ...

  7. Virtual IP address

    https://en.wikipedia.org/wiki/Virtual_IP_address Virtual IP address From Wikipedia, the free encyclo ...

  8. 动态追踪技术 Dynamic Tracing

    https://openresty.org/posts/dynamic-tracing/ 工欲性能调优,必先利其器(2)- 火焰图| PingCAP https://pingcap.com/blog- ...

  9. Ant 打包 问题

    Ant 打包问题及解决归纳总结: 1.build.xml注意设置basedir: <project name="s2si"  default="dist" ...

  10. Struts2自定义过滤器的小例子-入门篇

    创建web项目    实现的效果! 用户点击页面不同的链接,后台调用不同的代码! 创建两个类实现共同的接口! public interface Action { String execute(); } ...