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

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

先缩点(把强联通分量看为一个点), 判断出度为 0 的点有几个,如果大于 1 则输出 0, 否则输出 出度为零的点的个数

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; #define N 50005 struct node
{
int v, next;
}a[N]; int Head[N], cnt;
int dfn[N], low[N], Time, bnt, belong[N];
int Stack[N], InStack[N], top; void Init()
{
cnt = Time = bnt = top = ;
memset(Head, -, sizeof(Head));
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(Stack, , sizeof(Stack));
memset(InStack, , sizeof(InStack));
} void Add(int u, int v)
{
a[cnt].v = v;
a[cnt].next = Head[u];
Head[u] = cnt++;
} void Tarjar(int u)
{
int v;
low[u] = dfn[u] = ++Time;
InStack[u] = ;
Stack[top++] = u; for(int j=Head[u]; j!=-; j=a[j].next)
{
v = a[j].v;
if(!dfn[v])
{
Tarjar(v);
low[u] = min(low[u], low[v]);
}
else if(InStack[v])
low[u] = min(low[u], dfn[v]);
} if(dfn[u]==low[u])
{
bnt++;
do
{
v = Stack[--top];
InStack[v] = ;
belong[v] = bnt;
}while(u!=v);
}
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
int i, u, v; Init();
for(i=; i<=m; i++)
{
scanf("%d%d", &u, &v);
Add(u, v);
} for(i=; i<=n; i++)
{
if(!dfn[i])
Tarjar(i);
} int Out[N]={};
for(int i=; i<=n; i++)
{
for(int j=Head[i]; j!=-; j=a[j].next)
{
u = belong[i], v = belong[a[j].v];
if(u!=v)
Out[u]++;
}
} int flag=, Index;
for(i=; i<=bnt; i++)
{
if(!Out[i])
{
flag++;
Index = i;
}
} if(flag>)
printf("0\n");
else
{
int ans = ;
for(i=; i<=n; i++)
{
if(belong[i]==Index)
ans++;
}
printf("%d\n", ans);
}
}
return ;
}

(连通图 缩点 强联通分支)Popular Cows -- poj --2186的更多相关文章

  1. Popular Cows(POJ 2186)

    原题如下: Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40746   Accepted: 16 ...

  2. Popular Cows POJ - 2186(强连通分量)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  3. Popular Cows (POJ No.2186)

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  4. Popular Cows(codevs 2186)

    题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表 ...

  5. poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)

    http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...

  6. 【2186】Popular Cows(强连通分支及其缩点)

    id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS   Memory Limit: 65536 ...

  7. POJ 2186 Popular Cows(强联通+缩点)

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  8. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  9. POJ 2186 Popular Cows(强联通分量)

    题目链接:http://poj.org/problem?id=2186 题目大意:    每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种 ...

随机推荐

  1. noip2009最优贸易(水晶球)

    题目:http://codevs.cn/problem/1173/ https://www.luogu.org/problemnew/show/P1073 本来考虑缩点什么的,后来发现不用. 只要记录 ...

  2. Keil for ARM与C++

    1. 如果你的程序中使用了C++全局变量,那么*不要*使用MicroLIB,否则Keil会说某某Symbol找不到 2. 不使用MicroLIB带来的一个问题是KEIL会使用semihosting S ...

  3. 【Codeforces】Codeforces Round #492 (Div. 2) (Contest 996)

    题目 传送门:QWQ A:A - Hit the Lottery 分析: 大水题 模拟 代码: #include <bits/stdc++.h> using namespace std; ...

  4. 精《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #8 调度策略

    HACK #8 调度策略 本节介绍Linux的调度策略(scheduling policy).Linux调度策略的类别大致可以分为TSS(Time Sharing System,分时系统)和实时系统这 ...

  5. Vue项目中将table组件导出Excel表格以及打印页面内容

    体验更优排版请移步原文:http://blog.kwin.wang/programming/vue-table-export-excel-and-print.html 页面中显示的table表格,经常 ...

  6. 什么是Emit,什么是反射,二者区别到底是什么?(转)

    Emit的准确定义,我们看看微软给出的答案 System.Reflection.Emit 命名空间包含{ 允许编译器或工具发出元数据和发出 Microsoft 中间语言 (MSIL) ,并可选择在磁盘 ...

  7. 使用Visual Studio进行 Android开发的十大理由

    [原文发表地址]Top 10 reasons to use Visual Studio for C++ Android Development! Visual Studio: C++跨平台的移动解决方 ...

  8. LevelDB Cache机制

    [LevelDB Cache机制] 对于levelDb来说,读取操作如果没有在内存的memtable中找到记录,要多次进行磁盘访问操作.假设最优情况,即第一次就在level 0中最新的文件中找到了这个 ...

  9. nginx反向代理同一主机多个网站域名

    nginx反向代理同一ip多个域名,给header加上host就可以了 proxy_set_header   Host             $host; nginx.conf例子 upstream ...

  10. 05-了解activiti目录结构

    数据库底层支持的23张表的增删改查 如果你不会用activiti的API,可以看一下开发文档 流程引擎ProcessEngines,最重要是这个玩意. libs就更重要了,我们使用一个框架主要是使用它 ...