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

3 3
1 2
2 1
2 3

Sample Output

1

【数据范围】
10%的数据N<=20, M<=50
30%的数据N<=1000,M<=20000
70%的数据N<=5000,M<=50000
100%的数据N<=10000,M<=50000

 
突然发现以前的tarjan都时有问题的,vis标记应该出栈的时候打,而非退出是打。这道题缩点后判断“树根”本是非常简单的问题,然而由于思考不够深入,没有抓住"当且仅当DAG退化为“树”,才有解"的特性,所以编了半天还是错的。
#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缩点的更多相关文章

  1. bzoj 1051: [HAOI2006]受欢迎的牛 (Tarjan 缩点)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1051 思路: 首先用Tarjan把环缩成点,要想收到所有人的欢迎,那么这个点的出度必为0,且 ...

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

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

  3. BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1051 题解: 强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问 ...

  4. BZOJ 1051: [HAOI2006]受欢迎的牛(SCC)

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

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

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

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

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

  7. 【BZOJ1051】1051: [HAOI2006]受欢迎的牛 tarjan求强连通分量+缩点

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

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

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

  9. [HAOI2006]受欢迎的牛 tarjan缩点 + 拓扑排序

    ---题面--- 题解: 首先tarjan缩点应该还是容易想到的,因为喜爱具有传递性,所以一个强联通分量里面的点实际上是全部等效的,所以我们可以缩成一个方便判断, 缩完点之后整张图就变成了一个有向无环 ...

随机推荐

  1. Android-Wheel-Menu

    https://github.com/anupcowkur/Android-Wheel-Menu

  2. ActivityGroup相关--getLocalActivityManager()

    ActivityGroup简介 1.ActivityGroup的核心就是继承了该类,能够通过getLocalActivityManager()得到一个LocalActivityManager 如,Lo ...

  3. innodb_support_xa

    从官方解释来看,innodb_support_xa的作用是分两类: 第一,支持多实例分布式事务(外部xa事务),这个一般在分布式数据库环境中用得较多. 第二,支持内部xa事务,说白了也就是说支持bin ...

  4. 系统性能优化分析—CPU消耗

    http://blog.csdn.net/longdel/article/details/7296446

  5. popupWindow使用详解

    popupWindow说起来简单,但是使用略麻烦,今天带大家来看看怎么使用,先来看看效果图: 先来看看布局文件吧: <RelativeLayout xmlns:android="htt ...

  6. Android开发之适配器-ListView适配器的重复数据

    适配器是Android中的数据与View视图的桥梁,作用就是将数据通过适配器显示到对应的View视图上. 工作中,在用ListView做适配器数据时候,有些人肯定碰见过,如何优化效率,但是又出现重复数 ...

  7. jwplayer 源代码重新编译

    由于原来下载的jwplaery播放器中带有官方的播放统计信息,需要从官方加载统计js脚本,影响播放器加载速度,因此从官方github站点上下载播放器进行重新编译操作,现记录过程如下. 下载最新的jwp ...

  8. quartz简单实现

    quartz介绍 在我们的项目中,经常会遇到某个任务需要在特定的时间点执行,这个时间点可能是每天的某分某秒,可能是一周的指定某个星期某个时间等:quartz就是实现这样一个复杂任务调度逻辑的框架. 官 ...

  9. List和ArrayList,LinkList的区别

    接口 List<E> 是一个接口: ArrayList<E> 是一个类:是一个实现了List接口的类,因此可以List里面定义的所有的方法都实现了. 1.ArrayList是实 ...

  10. 当前时间 js

    当前时间-倒计时下载 效果: 代码: <!doctype html> <html> <head> <meta http-equiv="Content ...