luogu2341 [HAOI2006]受欢迎的牛
题目大意
每头奶牛都梦想成为牛棚里的明星。被所有奶牛喜欢的奶牛就是一头明星奶牛。所有奶牛都是自恋狂,每头奶牛总是喜欢自己的。奶牛之间的“喜欢”是可以传递的——如果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]受欢迎的牛的更多相关文章
- bzoj1051 [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4773 Solved: 2541[Submit][Sta ...
- 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数
[bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...
- 1051: [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2276 Solved: 1190[Submit][Sta ...
- BZOJ 1051: [HAOI2006]受欢迎的牛 缩点
1051: [HAOI2006]受欢迎的牛 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...
- 【BZOJ】1051: [HAOI2006]受欢迎的牛
[HAOI2006]受欢迎的牛 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢 ...
- bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2092 Solved: 1096[Submit][Sta ...
- BZOJ 1051: [HAOI2006]受欢迎的牛( tarjan )
tarjan缩点后, 有且仅有一个出度为0的强连通分量即answer, 否则无解 ----------------------------------------------------------- ...
- bzoj1051: [HAOI2006]受欢迎的牛(tarjan板子)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6064 Solved: 3179[Submit][Sta ...
- 洛谷 P2341 [HAOI2006]受欢迎的牛 解题报告
P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的"喜欢&q ...
随机推荐
- 12.Nodes
Nodes(节点) Animation(动画) KeyframeAnimation 逐帧动画,该节点中包含了所有绑定属性的动画逻辑 Animation Group 逐帧动画分组 S ...
- [ CQOI 2009 ] 中位数图
\(\\\) \(Description\) 给出\(N\)的一个全排列,统计该排列有多少个长度为奇数的连续子序列,中位数是\(B\). \(N\in [0,10^5]\),\(B\in [0,N]\ ...
- 给网站添加运行时间的JavaScript完整代码
function secondToDate(second) { if (!second) { return 0; } var time = new Array(0, 0, 0, 0, 0); if ( ...
- Python3之Zip
from collections import defaultdict from collections import OrderedDict d = defaultdict(list) d['a'] ...
- JS——if条件判断
现在只说特殊情况: 1.一个变量,例如n1=null <script> var n1 = null; alert(n1);/*弹窗的值为null*/ if (n1 == null) {/* ...
- servlet的多线程并发问题
package gz.itcast.e_thread; import java.io.IOException; import javax.servlet.ServletException; impor ...
- RTL Compiler之synthesis steps
1 synthesis steps 1) Search Paths rc:/> set_attribute lib_search_path path / rc:/> set_attribu ...
- cordova插件分类
1.android自动更新功能所需插件 cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.gi ...
- Spring学习_day02_AOP,AspectJ,JdbcTemplate
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! Spring_day02 一.AOP面向切面编程 1. ...
- cookie范例
GET /locate/api/getLocByIp?key=C6E22B7D480E3312C74EC7EF013E50C5&callback=bowlder.cb._0 HTTP/1.1 ...