(连通图 缩点 强联通分支)Popular Cows -- poj --2186
http://poj.org/problem?id=2186
Description
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
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
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的更多相关文章
- Popular Cows(POJ 2186)
原题如下: Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40746 Accepted: 16 ...
- 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, ...
- 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 <= ...
- Popular Cows(codevs 2186)
题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表 ...
- 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 ...
- 【2186】Popular Cows(强连通分支及其缩点)
id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS Memory Limit: 65536 ...
- 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 <= ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- POJ 2186 Popular Cows(强联通分量)
题目链接:http://poj.org/problem?id=2186 题目大意: 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种 ...
随机推荐
- html页面转换成pdf
一般页面都是.jsp页面,所以要把.jsp转换成html,在生成pdf,在网上找了好多方法,只有用一个插件,wkhtmltopdf-0.8.3.exe,生成的pdf会相对的好看. 先附上我做的.jsp ...
- 国外接活网站Elance, Freelancer和ScriptLance的介绍和对比
国外接活网站Elance, Freelancer和ScriptLance的介绍和对比littleben 一年以前 (via WEB)http://www.geekpark.net/entity/vie ...
- javaweb消息中间件——rabbitmq入门
概念:RabbitMQ是一款开源的消息中间件系统,由erlang开发,是AMQP的实现. 架构图大概如上. broker是消息队列的服务器,比如在linux上,我们安装的rabbitmq就是一个bro ...
- OpenMP 旅行商问题,静态调度
▶ <并行程序设计导论>第六章中讨论了旅行商,分别使用了 MPI,Pthreads,OpenMP 来进行实现,这里是 OpenMP 的代码,分为静态调度(每个线程分分配等量的搜索人物)和动 ...
- YUM CentOS 7 64位下mysql5.7安装配置
配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/ #下载mysql源安装包 # wget http://dev ...
- C#实现图片文件到数据流再到图片文件的转换
//----引入必要的命名空间using System.IO;using System.Drawing.Imaging; //----代码部分----// private byte[] photo;/ ...
- curl常用设置-涉及超时相关
curl_easy_setopt( curl, CURLOPT_VERBOSE, 1L ); //在屏幕打印请求连接过程和返回http数据 curl_easy_setopt( curl, CURLOP ...
- 关于document.cookie的使用
设置cookie每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cookie="userId=828";如果要一次 ...
- ansible模块学习
ansible的功能: 模块化任务,调用特定的模块,完成特定的任务 基于python语言实现,由paramiko.pyyaml和jinja2三个模块构建 部署简单,agentless,ansible基 ...
- C#格式化数字
var t1 = Profiler.GetMonoHeapSize()/div; var t2 = Profiler.GetMonoUsedSize() / div; var t3 = Profile ...