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 ...
随机推荐
- LinearLayout中间布局填充出现的问题
线性布局如何中间填充,会挤掉他下面的布局,所以中间填充使用layout_weight属性.
- html5——css选择器
复习 div>p: 子代 div+p:div后面相邻的第一个p div~p: div后面所有的兄弟p 属性选择器 标志:[]:区别于id选择器:#,区别于类名选择器:. 特殊符号:^:开头 ...
- JS——模拟百度搜索
注意事项: 1.for循环移除子节点时,其长度是变化的 2.在文档流中,input.img.p等标签与其他标签有3px的距离,利用左浮动,可以消除3px距离 3.背景图片定位时,第一个值是x轴方向的值 ...
- java攻城狮之路--复习JDBC(利用BeanUtils、JDBC元数据编写通用的查询方法;元数据;Blob;事务;批量处理)
1.利用BeanUtils的前提得要加入以下两个jar包: commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar package com.shel ...
- codeforces_731C_[dfs][并查集]
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- iOS UIWebView 访问https绕过证书验证的方法
@implementation NSURLRequest (NSURLRequestWithIgnoreSSL) + (BOOL)allowsAnyHTTPSCertificateForHost:(N ...
- jquery ajax中各个事件执行顺序如下
$(function(){ setTimeout(function(){ $.ajax({ url:'/php/selectStudent.php', }); },0); $(document).aj ...
- 模态框(layer)
推荐一个好看的模态框(layer) 地址:http://layer.layui.com/ 相应列子及配置 全部来自于官网,可直接访问官网学习了解. //信息框-例1 layer.alert('见 ...
- 详解proxy_pass、upstream与resolver
详解proxy_pass.upstream与resolver boldcautious 关注 2018.06.04 10:48 字数 1204 阅读 1434评论 0喜欢 2 应用场景 这里列举几个应 ...
- Web 常用
System.Web.Hosting.HostingEnvironment.MapPath(); HttpUtility.UrlEncode();