题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1051

由题意可知,被所有牛仰慕的牛之间也互相仰慕,则最后的答案一定是唯一的强连通分量,如图:

且这个强连通分量出度为0。

所以用tarjan缩环,然后在判断出度为0的是否有且仅有一个点。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
struct node {
int e, next;
}edge[];
int head[maxn * ], len;
void add(int s, int e) {
edge[++len].e = e;
edge[len].next = head[s];
head[s] = len;
}
int color[maxn], dfn[maxn], low[maxn], vis[maxn];
int out[maxn];
int num[maxn];
int dfsnum, ansnum, top;
stack<int>q;
void tarjan(int x) {
dfn[x] = low[x] = ++dfsnum;
vis[x] = ;
q.push(x);
for (int i = head[x]; i; i = edge[i].next) {
int y = edge[i].e;
if (!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
}
else if (vis[y])
low[x] = min(low[x], dfn[y]);
}
if (low[x] == dfn[x]) {
ansnum++;
int y;
do {
y = q.top();
q.pop();
color[y] = ansnum;
num[ansnum]++;
vis[y] = ;
} while (x != y);
}
}
int main() {
int n, m, x, y;
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++) {
scanf("%d%d", &x, &y);
add(x, y);
}
for (int i = ; i <= n; i++)
if (!dfn[i])
tarjan(i);
for (int x = ; x <= n; x++) {
for (int i = head[x]; i; i = edge[i].next) {
int y = edge[i].e;
if (color[x] != color[y])
out[color[x]]++;
}
}
int ans = , f = ;
for (int i = ; i <= ansnum; i++)
if (out[i] == )ans = num[i], f++;
if (f == )
printf("%d\n", ans);
else
printf("0\n");
}

[Bzoj1051][HAOI2006]受欢迎的牛(tarjan)的更多相关文章

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

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

  2. BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...

  3. bzoj1051 [HAOI2006]受欢迎的牛 tarjan&&缩点

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  4. 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数

    [bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...

  5. bzoj1051: [HAOI2006]受欢迎的牛(tarjan板子)

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

  6. bzoj1051 [HAOI2006]受欢迎的牛

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

  7. bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点

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

  8. BZOJ 1051: [HAOI2006]受欢迎的牛( tarjan )

    tarjan缩点后, 有且仅有一个出度为0的强连通分量即answer, 否则无解 ----------------------------------------------------------- ...

  9. [Bzoj1051][HAOI2006]受欢迎的牛(缩环)

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

随机推荐

  1. webpack的一般性配置及说明

    1.webpack的常规配置 先给出一个示例: const path = require('path'); const HtmlWebpackPlugin = require('html-webpac ...

  2. 利用python处理txt文件

    前段时间做公司一个自动翻译项目需要处理大量的文案字段,手工去做简直不大可能(懒),因此借用python脚本自动化处理掉了,在此记录一下. import linecache def outputfile ...

  3. 2018-8-10-sublime-Text-正则替换

    title author date CreateTime categories sublime Text 正则替换 lindexi 2018-08-10 19:16:52 +0800 2018-2-1 ...

  4. 尖沙咀到底谁说的算?!--- CSS层叠

    前些天,我朋友发了这个段CSS我. //css *{ color:#fff ; } div{ color:#000 !important; } //html <div><span&g ...

  5. 06Web服务

    1.web开发入门 1.1 引入 软件结构分类: CS结构:客户端和服务器端 特点: 1)必须安装特点的客户端程序 2)服务器端升级,客户端同步升级 BS结构:浏览器和服务器端 特点: 1)不需要安装 ...

  6. ghci对haskell的类型推导

    今天这篇文章分析一下ghci交互解释器对类型的推导. 假设有函数fn定义如下: let fn = map map 现在fn的类型是: map map :: [a -> b] -> [[a] ...

  7. ShellExecute指定IE浏览器打开

    ShellExecute(NULL,L"open", L"iexplore.exe", L"www.baidu.com", NULL, SW ...

  8. git@github.com出现Permission denied (publickey)

    上传项目的时候出现Permission denied (publickey)这个问题 解决方案如下: 看本地的.git/config设置的仓库url地址和github使用的链接地址是否一致如下图,如u ...

  9. 对vue的研究

    beforeDestroy 类型:Function 详细: 实例销毁之前调用.在这一步,实例仍然完全可用. 该钩子在服务器端渲染期间不被调用. 参考:生命周期图示 destroyed 类型:Funct ...

  10. python-Exception异常使用

    Exception #自定义异常类 ,MyInputExcp继承Exception异常 class MyInputExcp(Exception): def __init__(self, lenght, ...