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. PuTTY介绍、安装、使用

    简介 PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连接软件.较早的版本仅支持Windows平台,在最近的版本中开始支持各类Unix平台,并打算移植至Mac OS X上.除了 ...

  2. navicate11不能激活的问题

    navicate11不能激活的问题 学习了:http://blog.csdn.net/sanbingyutuoniao123/article/details/52589678 不要安装在系统盘,如果安 ...

  3. Android nomedia 避免图片等资源泄露在系统图库其中

    总结 Android nomedia 避免文件泄露在系统图库和系统铃声中 在应用开发中 项目的图片总是被系统的图库收录了 避免图片被系统图库收录的发现有2个方法 第一种针对图片 将 .png为后缀的图 ...

  4. MYSQL Training: MySQL I

    让以admin身份登录.源代码: 非常easy的注入 在username输入 admin' OR '1'='1 OK.

  5. apiCloud中的数据库操作mcm-js-sdk的使用

    1.引入js <!-- 引入mcm-js-sdk Begin --> <script type="text/javascript" src="../pl ...

  6. maven冲突管理及依赖管理实践

    1.“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突 Maven采用“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突,即如果一个项目最终 ...

  7. [jzoj 6092] [GDOI2019模拟2019.3.30] 附耳而至 解题报告 (平面图转对偶图+最小割)

    题目链接: https://jzoj.net/senior/#main/show/6092 题目: 知识点--平面图转对偶图 在求最小割的时候,我们可以把平面图转为对偶图,用最短路来求最小割,这样会比 ...

  8. 如何在maven项目中使用spring

    今天开始在maven项目下加入spring. 边学习边截图. 在这个过程中我新建了一个hellospring的项目.于是乎从这个项目出发开始研究如何在maven项目中使用spring.鉴于网上的学习资 ...

  9. POJ 2665 模拟,,

    It is confirmed that these sections do not overlap with each other. 一句话 就变成水题了,,, // by SiriusRen #i ...

  10. 入门 IT 行业,该具备哪些技能?

    对于刚开始进入IT的新人来说,“必备技能”往往意味着一个长长的.标有重要度的学习列表,但是过长的列表通常会导致新人不知如何开始学习,压力倍增.本文尝试列举出最重要的几个技能,也期望通过此列表能给新人一 ...