题意:

有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表2欢迎1,但是如果2也欢迎3那么1也欢迎3.给出N,M和M个欢迎关系,求被所有牛都欢迎的牛的数量。

/*
tarjan缩点后,统计每个所点的出度,当有且只有一个出度为0的缩点是,
答案即为这个缩点所包含的点的数量,否则答案为0
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#define M 10010
using namespace std;
int low[M],num[M],belong[M],vis[M],instack[M],in[M],out[M],sum[M],indexx,cnt,n,m;
vector<int> grap[M];
stack<int> s;
void tarjan(int v)
{
low[v]=num[v]=++indexx;
s.push(v);
vis[v]=;
instack[v]=;
for(int i=;i<grap[v].size();i++)
{
int w=grap[v][i];
if(!vis[w])
{
tarjan(w);
low[v]=min(low[v],low[w]);
}
else if(instack[w])
low[v]=min(low[v],num[w]);
}
int u;
if(low[v]==num[v])
{
++cnt;
do
{
u=s.top();
s.pop();
belong[u]=cnt;
sum[cnt]++;
instack[u]=;
}
while(u!=v);
}
}
void work()
{
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
grap[y].push_back(x);
}
for(int i=;i<=n;i++)
if(!vis[i])
tarjan(i);
for(int i=;i<=n;i++)
for(int j=;j<grap[i].size();j++)
if(belong[i]!=belong[grap[i][j]])
{
in[belong[i]]++;
out[belong[grap[i][j]]]++;
}
int tot=,p;
for(int i=;i<=cnt;i++)
if(!out[i])
{
tot++;
p=i;
}
if(tot==)printf("%d\n",sum[p]);
else printf("0\n");
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
work();
memset(low,,sizeof(low));
memset(num,,sizeof(num));
memset(belong,,sizeof(belong));
memset(vis,,sizeof(vis));
memset(instack,,sizeof(instack));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)grap[i].clear();
while(!s.empty())s.pop();
indexx=;
cnt=;
}
}

Popular Cows(codevs 2186)的更多相关文章

  1. POJ 2186 Popular Cows(强连通)

                                                                  Popular Cows Time Limit: 2000MS   Memo ...

  2. Popular Cows(POJ 2186)

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

  3. (连通图 缩点 强联通分支)Popular Cows -- poj --2186

    http://poj.org/problem?id=2186 Description Every cow's dream is to become the most popular cow in th ...

  4. POJ 2186 Popular Cows(Tarjan)

    http://poj.org/problem?id=2186 题意 :给你n头牛,m对关系,每对关系由两个编号组成,u和v代表着u认为v是受欢迎的,如果1认为2是受欢迎的,2认为3是受欢迎的,那1认为 ...

  5. POJ 2186.Popular Cows (强连通)

    强连通缩点,统计入度为1的缩点后的点的个数 个数1的话输出这个强连通分量的点的数量 否则输出0: code /* Kosaraju算法,无向图的强连通分量,时间复杂度O(n+m) 思路: 按照图G的深 ...

  6. 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, ...

  7. poj2186 Popular Cows(强连通)

    崇拜有传递性.求所有牛都崇拜的牛tarjan算法求强连通. 如果不连通就不存在.如果联通,缩点后唯一一个出度为零的点就是答案,有多个则不存在. #include <vector> #inc ...

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

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

  9. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

随机推荐

  1. The prefix "mvc" for element "mvc:annotation-driven" is not bound 的解决方法

    添加 xmlns:mvc="http://www.springframework.org/schema/mvc" http://www.springframework.org/sc ...

  2. String类的常用方法

    package stringUse; public class StringUse { public static void main(String[] args) { //获取 //indexOf, ...

  3. Extjs Window用法详解

    今天我们来介绍一下Extjs中一个常用的控件Window.Window的作用是在页面中创建一个窗口,这个窗口作为容器,可以在它里面加入grid.form等控件,从而来实现更加复杂的界面逻辑. 本文的示 ...

  4. static 类也可以有static构造函数

    public static class A { static A() { } } static构造函数不能是public,也不可能被主动调用,所以public没有意义

  5. LESSCSS的几点摘要

    字符串插值 变量可以用像 @{name} 这样的结构,以类似 ruby 和 php 的方式嵌入到字符串中: @base-url: "http://assets.fnord.com" ...

  6. 不要在初始化方法和dealloc方法中使用Accessor Methods

    苹果在<Advanced Memory Management Programming Guide>指出: Don’t Use Accessor Methods in Initializer ...

  7. PHP文件包含漏洞剖析

    一. 什么才是”远程文件包含漏洞”?回答是:服务器通过php的特性(函数)去包含任意文件时,由于要包含的这个文件来源过滤不严,从而可以去包含一个恶意文件,而我们可以构造这个恶意文件来达到邪恶的目的. ...

  8. linux init.d脚本编写模板

    #!/bin/bash ### BEGIN INIT INFO # # Provides: location_server # Required-Start: $local_fs $remote_fs ...

  9. Ioc注解

    注解: 添加注解时,需要添加context的相关 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  10. iOS-(kCFStreamErrorDomainSSL, -9802)

    kCFStreamErrorDomainSSL, -9802 我是微博授权时get页面时候碰到的 其实就是http安全问题 在info.plist里添加并设置Allow Arbitrary Loads ...