bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点
1051: [HAOI2006]受欢迎的牛
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 2092 Solved: 1096
[Submit][Status]
Description
每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头牛被所有的牛认为是受欢迎的。
Input
第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可能出现多个A,B)
Output
一个数,即有多少头牛被所有的牛认为是受欢迎的。
Sample Input
1 2
2 1
2 3
Sample Output
【数据范围】
10%的数据N<=20, M<=50
30%的数据N<=1000,M<=20000
70%的数据N<=5000,M<=50000
100%的数据N<=10000,M<=50000
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define MAXN 11000
#define MAXV MAXN
#define MAXE MAXN*20
struct Edge
{
int np;
Edge *next;
}E[MAXE],*V[MAXV];
int tope=-;
void addedge(int x,int y)
{
E[++tope].np=y;
E[tope].next=V[x];
V[x]=&E[tope];
}
int low[MAXN],dfn[MAXN];
int dfstime=;
int stack[MAXN];
int tops=-;
int color[MAXN],topc=;
int size_c[MAXN];
int size_sub[MAXN];
bool vis[MAXN];
set<int> S[MAXN];
void tarjan(int now)
{
low[now]=dfn[now]=++dfstime;
Edge *ne;
stack[++tops]=now;
for (ne=V[now];ne;ne=ne->next)
{
if (vis[ne->np])continue;
if (dfn[ne->np])
{
low[now]=min(low[now],dfn[ne->np]);
}else
{
tarjan(ne->np);
low[now]=min(low[now],low[ne->np]);
}
}
if (low[now]==dfn[now])
{
++topc;
while (stack[tops]!=now)
{
vis[stack[tops]]=true;
color[stack[tops--]]=topc;
size_c[topc]++;
}
vis[stack[tops]]=true;
color[stack[tops--]]=topc;
size_c[topc]++;
}
}
pair<int,int> edge[MAXE];
int topedge;
int degree[MAXN];
queue<int> Q;
int main()
{
freopen("input.txt","r",stdin);
int n,m;
scanf("%d%d",&n,&m);
int i,j,k,x,y,z;
for (i=;i<m;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
edge[i].first=x;
edge[i].second=y;
}
sort(edge,&edge[m]);
topedge=;
for (i=;i<m;i++)
{
if (edge[i]!=edge[topedge])
edge[++topedge]=edge[i];
}
for (i=;i<=n;i++)
{
if (!dfn[i])tarjan(i);
}
// memset(V,0,sizeof(V));
// tope=-1;
for (i=;i<m;i++)
{
if (color[edge[i].first]==color[edge[i].second])continue;
// if (S[color[edge[i].first]].find(color[edge[i].second])!=S[color[edge[i].first]].end())continue;
// addedge(color[edge[i].first],color[edge[i].second]);
// S[color[edge[i].first]].insert(color[edge[i].second]);
degree[color[edge[i].first]]++;
}
int ans;
ans=;
for (i=;i<=topc;i++)
{
if (degree[i]==)ans++,x=i;
}
if (ans==)
{
printf("%d\n",size_c[x]);
}else
{
printf("0\n");
}
return ;
}
bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点的更多相关文章
- bzoj 1051: [HAOI2006]受欢迎的牛 (Tarjan 缩点)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1051 思路: 首先用Tarjan把环缩成点,要想收到所有人的欢迎,那么这个点的出度必为0,且 ...
- BZOJ 1051: [HAOI2006]受欢迎的牛( tarjan )
tarjan缩点后, 有且仅有一个出度为0的强连通分量即answer, 否则无解 ----------------------------------------------------------- ...
- BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1051 题解: 强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问 ...
- BZOJ 1051: [HAOI2006]受欢迎的牛(SCC)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8172 Solved: 4470[Submit][Sta ...
- BZOJ 1051: [HAOI2006]受欢迎的牛 缩点
1051: [HAOI2006]受欢迎的牛 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...
- 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数
[bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...
- 【BZOJ1051】1051: [HAOI2006]受欢迎的牛 tarjan求强连通分量+缩点
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认 ...
- bzoj1051 [HAOI2006]受欢迎的牛 tarjan&&缩点
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...
- [HAOI2006]受欢迎的牛 tarjan缩点 + 拓扑排序
---题面--- 题解: 首先tarjan缩点应该还是容易想到的,因为喜爱具有传递性,所以一个强联通分量里面的点实际上是全部等效的,所以我们可以缩成一个方便判断, 缩完点之后整张图就变成了一个有向无环 ...
随机推荐
- Maven tomcat插件配置和使用
pom.xml组态 <build> <plugins> <plugin> <groupId>org ...
- netsh
NetSH (Network Shell) 是windows系统本身提供的功能强大的网络配置命令行工具. 导出配置脚本:netsh -c interface ip dump > c:\inter ...
- PCAP 文件内容解析命令
针对网络接口.端口和协议的数据包截取.假定你要截取网络接口eth1,端口号6881的tcp数据包.数据文件保存为test.pcap. tcpdump -w test.pcap -i eth1 tcp ...
- Properties文件,Data,Calendar类的使用
package cn.hncu.day9; import java.io.FileInputStream;import java.io.FileNotFoundException;import jav ...
- Android(java)学习笔记191:Android数据存储5种方式总结
1.使用文件(File)存储 存储一般的数据 2.使用sharedperference(xml) 存储设置信息.配置信息.密码 3.数据库Sqlite 开源的,嵌入式的数据库,轻量级 4.使用Cont ...
- 复杂对象创建终结者(Builder Pattern)
捣鼓了很长时间,终于对建造者模式有初步理解,现在写篇记录下.缘起就是创建的对象比较复杂,需按功能分散.类似造一辆汽车,作为汽车厂家,你需要造车身,造轮胎等,精髓在于领导者(Director),领导者指 ...
- dhcp源码编译支持4G上网卡
1. tar xvzf dhcp-4.2.5-P1.tar.gz 2. ./configure --host=arm-linux ac_cv_file__dev_random=yes 3. vi bi ...
- 转-C# 操作 Excel 常见问题收集和整理
经常会有项目需要把表格导出为 Excel 文件,或者是导入一份 Excel 来操作,那么如何在 C# 中操作 Excel 文件成了一个最基本的问题. 做开发这几年来,陆陆续续也接触过这样的需求,但因为 ...
- Ganglia3.6.0,nginx+php搭建gweb,监控Hadoop2.2 和 Hbase0.98.1
环境:CentOS6.5 Hadoop2.2.0 Hbase0.98.1 服务端(master): 安装 ganglia ganglia-devel ganglia-gmetad ganglia-gm ...
- Cocos2d-x 学习资料收集
框架源代码: http://code.google.com/p/cocos2d-x/downloads/list 搭建环境 http://blog.csdn.net/ccf19881030/artic ...