(连通图 缩点 强联通分支)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受欢迎. 这 种 ...
随机推荐
- java 面向对象 — 继承
继承中的构造方法,先执行父类中的构造方法,然后执行子类中的构造方法 继承中的属性,最后执行的属性 覆盖前面的属性 因为是开辟了 两个内存空间,所以相比较是不同的. 如果想比较两个对象的值是否相同的话, ...
- Java File文件操作 创建文件\目录,删除文件\目录
Java手册 java.io 类 File java.lang.Object java.io.File 所有已实现的接口: Serializable, Comparable<File> p ...
- 认识Linux操作系统
Linux系统是一个类似UNIX的操作系统 认识Linux的来世与今生 1.Linux系统的历史 Linux系统是一个类似UNIX的操作系统,Linux系统是UNIX在微机上的完整实现,它的标志是一个 ...
- django2.0设置默认访问路由
搭建完毕后,打开域名,直接来个报错,如果没有访问的地址,是不合适的 在urls.py中定义,默认访问的路由 from django.contrib import admin from django.u ...
- TCAM CAM 说明 原理 结构 Verilog 硬件实现
TCAM 三态内容地址查找存储器,CAM内容地址查找存储器.区别在于TCAM多了一级掩码功能,也就是说可以指定某几位是dont care.匹配的时候0,1都行的意思. 广泛应用于数据流处理领域,本文简 ...
- WordPress部署
WordPress部署 WordPress是一个门户.博客网站的制作工具,php开发,自带后台,可以很简便的安装主题,还拥有一个庞大的主题网站生态. 软件下载:https://cn.wordpress ...
- 小学生福利web及APP原型展示
332熊哲琛 320刘佳 原型作业地址 https://edu.cnblogs.com/campus/fzzcxy/2016SE/home work/2180 原型设计链接 https://modao ...
- Linux运维基础入门(四):Linux中的网络知识04
一,虚拟机的安装 略 二,Linux系统下的网络配置(Linux虚拟机的网络设定为桥接模式) 桥接模式:虚拟机同主机一样,在网络中相当于一个真实存在的装有Linux系统的电脑.(我们先用这个模式) N ...
- Type Object——类型对象
clr会为应用程序使用的每个类型创建一个内部数据结构,这种数据结构称为类型对象. 具有泛型类型参数的类型称为开放类型(open type),CLR禁止构造开放类型的任何实例. 代码引用一个泛型类型时, ...
- $in 操作符
[$in 操作符] The $in operator selects the documents where the value of a field equals any value in the ...