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. c# 爬虫(三) 文件上传

    在上一篇中,我们说了模拟登录, 下面我们说说附件上传. 据说,最早的http协议是不支持附件上传的,后来有添加了一个RFC 2045 协议,才支持附件上传,关于附件上传,请参见 http://www. ...

  2. GCC参数详解 二

    1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...

  3. ThinkJava-File类

    1.1目录列表器: package com.java.io; import java.io.File; import java.io.FilenameFilter; import java.util. ...

  4. 汇编_指令_SUB

    SUB是减法运算. 比如mov ax,2mov bx,1sub ax,bx 其中sub ax,bx就是ax中的值减bx中的值,等于1,然后把结果,也就是1,放入ax中.

  5. 深入理解 Express.js

    本文针对那些对Node.js有一定了解的读者.假设你已经知道如何运行Node代码,使用npm安装依赖模块.但我保证,你并不需要是这方面的专家.本文针对的是Express 3.2.5版本,以介绍相关概念 ...

  6. jsp获取请求头信息

    <%@ page language="java" import="java.util.*" contentType="text/html; ch ...

  7. Vue 安装环境创建项目

    vue 是一个单页面框架,基于模块化组件化的开发模式. 搭建开发环境之前必须要安装node.js,然后安装vue的脚手架工具(命令行工具)win + R 输入npm install  --global ...

  8. 安装face_recognition

    Ubuntu安装face_recognition需要先安装dlib 1.安装dlib的依赖 sudo apt-get install build-essential cmake sudo apt-ge ...

  9. Django学习---信号

    Django学习之信号 如果我想对所有在数据库创建数据的时候记录一条日志. 比如我们在django中往数据库中增加一条数据,希望生成一条操作日志,或者在数据保存和数据保存之后都保存一条操作日志,那我们 ...

  10. lombok 的使用

    参考:https://blog.csdn.net/motui/article/details/79012846