题目大意

  每头奶牛都梦想成为牛棚里的明星。被所有奶牛喜欢的奶牛就是一头明星奶牛。所有奶牛都是自恋狂,每头奶牛总是喜欢自己的。奶牛之间的“喜欢”是可以传递的——如果A喜欢B,B喜欢C,那么A也喜欢C。牛栏里共有N 头奶牛,给定一些奶牛之间的爱慕关系,请你算出有多少头奶牛可以当明星。

思路

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <cassert>
using namespace std; const int MAX_NODE = 10010; struct Block
{
int Size;
bool NotLeaf;
}_blocks[MAX_NODE];
int _blockCnt; struct Node
{
int Low, DfsN;
bool InStack;
vector<Node*> Next;
Block* BlockIn;
}_nodes[MAX_NODE];
int _vCount, DfsCnt;
stack<Node*> St; void PopStack(Node *cur)
{
Block *curBlock = _blocks + ++_blockCnt;
Node *temp;
do {
temp = St.top();
temp->InStack = false;
St.pop();
temp->BlockIn = curBlock;
curBlock->Size++;
} while (temp != cur);
} void Dfs(Node *cur)
{
cur->Low = cur->DfsN = ++DfsCnt;
cur->InStack = true;
St.push(cur);
for (unsigned int i = 0; i < cur->Next.size(); i++)
{
if (!cur->Next[i]->DfsN)
{
Dfs(cur->Next[i]);
cur->Low = min(cur->Low, cur->Next[i]->Low);
}
else if (cur->Next[i]->InStack)
cur->Low = min(cur->Low, cur->Next[i]->DfsN);
}
if (cur->Low == cur->DfsN)
PopStack(cur);
} void Tarjan()
{
for (int i = 1; i <= _vCount; i++)
{
if (!_nodes[i].DfsN)
Dfs(_nodes + i);
assert(St.size() == 0);
}
} int GetLeafCnt()
{
int leafCnt = 0;
for (int i = 1; i <= _vCount; i++)
for (unsigned int j = 0; j < _nodes[i].Next.size(); j++)
if (_nodes[i].BlockIn != _nodes[i].Next[j]->BlockIn)
_nodes[i].BlockIn->NotLeaf = true;
for (int i = 1; i <= _blockCnt; i++)
leafCnt += !_blocks[i].NotLeaf;
return leafCnt;
} int GetAns()
{
for (int i = 1; i <= _blockCnt; i++)
if (!_blocks[i].NotLeaf)
return _blocks[i].Size;
return -1;
} int main()
{
int totEdge;
scanf("%d%d", &_vCount, &totEdge);
for (int i = 1; i <= totEdge; i++)
{
int u, v;
scanf("%d%d", &u, &v);
_nodes[u].Next.push_back(_nodes + v);
}
Tarjan();
int leafCnt = GetLeafCnt();
if (leafCnt > 1)
printf("0\n");
else
printf("%d\n", GetAns());
return 0;
}

  

luogu2341 [HAOI2006]受欢迎的牛的更多相关文章

  1. bzoj1051 [HAOI2006]受欢迎的牛

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

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

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

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

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

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

    1051: [HAOI2006]受欢迎的牛 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

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

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

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

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

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

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

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

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

  9. 洛谷 P2341 [HAOI2006]受欢迎的牛 解题报告

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

随机推荐

  1. 简单的KKL诊断线~~~自己在家都可以制作obd诊断接口了 ~~

    简单的KKL诊断线~~~自己在家都可以制作~~ 适合bmw 07年以前的车型,因为新的车型使用D-can作为诊断接口,所以不能再使用kkl诊断接口不过SB开头的宝马3系还是可以使用的 更多内容欢迎查看 ...

  2. nodejs全局安装路径的位置

    一般nodejs安装在默认的C盘,如果不知道安装在哪里,可以打开控制面板-系统和安全-系统-高级配置中找到 所谓全局安装: 是指安装在node中node_module的根目录里,可以在电脑的任何位置调 ...

  3. ORA-02068,ORA-03135错误解决方法

    今天查看了下ERP DB服务器 alter_<SID>.log日志,发现有个错误 Sat Sep 14 14:49:42 CST 2013 Error 2068 trapped in 2P ...

  4. Lazarus 1.6 增加了新的窗体编辑器——Sparta_DockedFormEditor.ipk

    一下是该控件官网的介绍 "Hello A package for a docked form editor can be found in : components/sparta/docke ...

  5. (转)Arcgis for JS之Cluster聚类分析的实现

    http://blog.csdn.net/gisshixisheng/article/details/40711075 在做项目的时候,碰见了这样一个问题:给地图上标注点对象,数据是从数据库来的,包含 ...

  6. codeforces_731C_[dfs][并查集]

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  7. windows环境安装python虚拟环境

    虚拟环境安装参考 https://www.cnblogs.com/suke99/p/5355894.html workon环境变量配置参照 https://www.cnblogs.com/jiuyan ...

  8. 计蒜客 成绩统计 (Hash表)

    链接 : Here! 思路 : 如果用 $STL$ 的 $map$ 或者是使用 $unordered\underline{}map$ 的话是会 $T$ 的, 所以得手写一个 $hash表$. 其实这个 ...

  9. Codeforces Round #468 Div. 2题解

    A. Friends Meeting time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  10. office 2016最新安装及激活教程(KMS)【亲测有效】!!

    前言 博主的一个朋友,咳咳--你们懂得,想装office,于是我就上网找了一下激活的方法,亲测有效,而且也没有什么广告病毒之类的,还比较方便,所以传上来方便大家.好了,进入正题: 安装office 首 ...