题意:

有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. 洛谷P1121 环状最大两段子段和

    题目描述 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 输入文件maxsum2.in的第一行是一个正整数N,表示了序列 ...

  2. OPENSSL编程入门学习

    相关学习资料 http://bbs.pediy.com/showthread.php?t=92649 https://www.openssl.org https://www.google.com.hk ...

  3. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  4. json 数据交换格式与java

    http://wiki.mbalib.com/wiki/数据交换 数据交换是指为了满足不同信息系统之间数据资源的共享需要,依据一定的原则,采取相应的技术,实现不同信息系统之间数据资源共享的过程. 数据 ...

  5. WPF中嵌入Office编辑器(支持Word、Excel、PPT、Visio等)

    现在有一个项目,需要使用wpf做一个简单的客户端,用来生成word.excel.ppt.visio等文档,这就需要能够在wpf中嵌入office的编辑器,并对office文档进行编辑. 在网上搜索了一 ...

  6. php中单例模式的解析说明

    <?php //单例模式 class Dbconn{ private static $_instance=null; protected static $_counter=0; protecte ...

  7. eclipse错误:Unable to read workbench state. Workbench UI layout will be reset.XML document structures

    Unable to read workbench state. Workbench UI layout will be reset.XML document structures must start ...

  8. hdu 1205 吃糖果

    思路: 仔细想想,想要不重复吃一种糖果, 把所有糖果吃完,只要所有糖果的和,减去最多的糖果+1>=最多糖果的数量即可不重复吃完. #include <stdio.h> int mai ...

  9. php-5.3 zend opcache 的设置

    故障现象,修改了代码上传到生产服务器之后,需要等待60秒才生效. 细查了一下,是opcache引起的,默认是60秒.于是我给关了,之前是ea加速,现在新版本的php好像用这个opcache了:; 2s ...

  10. c++ 的vector

    使用例子:std::vector<std::string> xmlNodeList; 下面介绍-- vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组 ...