http://poj.org/problem?id=2186

||

https://www.luogu.org/problem/show?pid=2341

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 33470   Accepted: 13634

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source

tarjan缩点,输出出度为0的点的子图中的点的个数

 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N();
int n,m,u,v,sumchudu,ans,cnt;
int point[N],chudu[N];
int sumedge,head[N];
struct Edge
{
int from,to,next;
Edge(int from=,int to=,int next=) :
from(from),to(to),next(next) {}
}edge[N]; int ins(int from,int to)
{
edge[++sumedge]=Edge(from,to,head[from]);
return head[from]=sumedge;
} int dfn[N],low[N],tim;
int Stack[N],top,instack[N];
int col[N],sumcol; void DFS(int now)
{
dfn[now]=low[now]= ++tim;
Stack[++top]=now; instack[now]=;
for(int i=head[now];i;i=edge[i].next)
{
v=edge[i].to;
if(instack[v]) low[now]=min(low[now],dfn[v]);
else if(!dfn[v])
DFS(v), low[now]=min(low[now],low[v]);
}
if(low[now]==dfn[now])
{
col[now]= ++sumcol;
point[sumcol]++;
for(;Stack[top]!=now;top--)
{
point[sumcol]++;
col[Stack[top]]=sumcol;
instack[Stack[top]]=;
}
instack[now]=; top--;
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d",&u,&v),ins(u,v);
for(int i=;i<=n;i++)
if(!dfn[i]) DFS(i);
for(int i=;i<=m;i++)
{
u=edge[i].from; v=edge[i].to;
if(col[u]!=col[v]) chudu[col[u]]++;
}
for(int i=;i<=sumcol;i++) if(!chudu[i])
++sumchudu,ans=point[i];
if(sumchudu!=) ans=;
printf("%d\n",ans);
return ;
}

POJ——T2186 Popular Cows || 洛谷——P2341 [HAOI2006]受欢迎的牛的更多相关文章

  1. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

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

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

  3. 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)

    P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...

  4. 【模板】Tarjan缩点,强连通分量 洛谷P2341 [HAOI2006]受欢迎的牛 [2017年6月计划 强连通分量01]

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

  5. 【题解】洛谷P2341 [HAOI2006]受欢迎的牛(强连通分量)

    洛谷P2341:https://www.luogu.org/problemnew/show/P2341 前言 这题看错题目 足足花了将近5小时提交了15次 在一位dalao的提醒下才AC了 记得要看清 ...

  6. 洛谷P2341 [HAOI2006]受欢迎的牛|【模板】强连通分量

    https://www.luogu.org/problem/P2341 缩点之后唯一 一个出度为0的点 #include<cstdio> #include<iostream> ...

  7. 洛谷 P2341 [HAOI2006]受欢迎的牛

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

  8. 洛谷 P2341 [HAOI2006]受欢迎的牛 题解

    今天学了强连通分量的Tarjan算法,做了这道类似于板子题的题(尽管我调了1.5h).主要的思路是用Tarjan缩点之后,求每个点的入度(实际上是出度,因为我是反着连边的).如果 有且只有一个点的入度 ...

  9. 洛谷 P2341 [HAOI2006]受欢迎的牛|【模板】强连通分量

    题目传送门 解题思路: 先求强联通分量,缩点,然后统计新图中有几个点出度为0,如果大于1个,则说明这不是一个连通图,答案即为0.否则入度为0的那个强连通分量的点数即为答案 AC代码: #include ...

随机推荐

  1. hdu 3836 强连通+缩点:加边构强连通

    #include<stdio.h>//求出其所有的强连通分量缩点,选出出度和入度最大的那个就是要求的边 #include<string.h> #include<stdli ...

  2. WebApi传参总动员(一)

    目前自己的工作和WebApi相关,免不了传入.接收参数.以前的老办法是从请求流中获取json,再反序列化,这中间有2个不能控制的地方,一个是流,一个是反序列化,都需要try,总感觉非常的不爽.因此对W ...

  3. struts2提交多个对象带图片

    一:实体类 二:前台页面 三:Action处理

  4. Linux安装vmtools

    unbantu下,先把DVD的Vmwarew.gz,文件拷贝到tmp文件.然后 tar zxf VMware Tools-0....... ls cd ./intall.pl 有个文件,先拷贝到roo ...

  5. nyist oj 115 城市平乱 (最短路径)

    城市平乱 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 南将军统领着N个部队.这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安.这M个城市 ...

  6. .NET泛型初探

    总所周知,.NET出现在.net framework 2.0,为什么要在2.0引入泛型那,因为微软在开始开发.net框架时并没有想过多个类型参数传输时对方法的重构,这样一来,开发人员就要面对传输多种类 ...

  7. 深刻理解Java中的String、StringBuffer和StringBuilder的差别

    声明:本博客为原创博客,未经同意.不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(链接为http://blog.csdn.net/bettarwang/article/detai ...

  8. poj--3169--Layout(简单差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9098   Accepted: 4347 Descriptio ...

  9. JAVA好书之《深入理解Java虚拟机》

    最近打算做好现有工作的前提下,扎实一下自己专业的技术知识,并将相关的经典书也记录一下.今天看了一些JVM相关的知识,这里面的经典是<深入理解Java虚拟机>,适合有点基础又想深入理解其中原 ...

  10. nginx报错日志:see security.limit_extensions

    访问出现部分.js.css等部分文件被拒绝错误日志如下: 19:20:13 [error] 1181#0: *287 FastCGI sent in stderr: "Access to t ...