传送门: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. system表空间用满解决

      分类: Oracle 早上看到alert日志报说system表空间快满了(oracle版本是11gR2):   如果system表空间不是自动扩展,空间用满甚至会出现数据库无法登陆.使用任何用户登 ...

  2. 改动Android启动画面

    一.Android的启动步骤 1.启动Linux 2.载入Android 3.显示Android桌面 二.分析 Android载入开机动画的源代码文件是: /opt/android4.3/framew ...

  3. 阅读《Android 从入门到精通》(33)——Intent 分类

    Intent 分类 显式 Intent:Intent("android.intent.action.CALL", Uri.parse("tel:" + stri ...

  4. #Virtual hosts #Include conf/extra/httpd-vhosts.conf 开启就不能启动apache

    #Virtual hosts#Include conf/extra/httpd-vhosts.conf我只要把其中任何一个开启就是吧#去掉就启动不了apache.怎么回事error.log是这样的ht ...

  5. CSS 相对|绝对(relative/absolute)定位系列(一)

    一.有话要说 以前写内容基本上都是:眼睛一亮——哟呵,这个不错,写!然后去古人所说的茅房里蹲会儿,就有写作的思路了.但是,构思相对/绝对(relative/absolute)定位系列却有好些时日,考虑 ...

  6. 【git体验】git原理及基础

    原理:分布式版本号控制系统像 Git,Mercurial,Bazaar 以及 Darcs 等,client并不仅仅提取最新版本号 的文件快照,而是把原始的代码仓库完整地镜像下来. 这么一来.不论什么一 ...

  7. C语言 字符串操作 笔记

    /* C语言字符串的操作笔记 使用代码和注释结合方式记录 */ # include <stdio.h> # include <string.h> int main(void) ...

  8. 1 Angular 2 简介与 AngularJS 1.x 历史对比

    Angular 2 是一款JavaScript的开源框架,用于协助单一页面应用程序运行.Angular 2 是 AngularJS 1.x 的升级版本,应Web的进化和前端开发的变革还有从Angula ...

  9. android抓log

    1.Logcat(能截取除了Kernel以外的所有Log信息),连接USB到电脑上,执行如下命令:User版本也可以使用adb logcat –v time >c:\ logcat.txtadb ...

  10. 高清接口芯片---gv7600、sii9135

    http://www.travellinux.com/download/海思Hi3516%20demo单板使用指南.pdf gv7600 sdi 串行数字接口 parrlar 并行数字接口 http: ...