来源poj2186

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

Hint

Cow 3 is the only cow of high popularity.

tarjan要找出受其他牛都欢迎的,强通量出度为0的只能有1个,否者就没有牛都受其他牛欢迎;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=10005;
struct Edge {
int v,next;
}edge[5*N];
int dfn[N],low[N];
int stack[N],node[N],visit[N],cnt,tot,index;
int belong[N],bcnt;
void add_edge(int x,int y)
{
edge[cnt].next=node[x];
edge[cnt].v = y;
node[x]=cnt++;
return ;
}
void tarjan(int x)//代表第几个点在处理。递归的是点。
{
dfn[x]=low[x]=++tot;// 新进点的初始化。
stack[++index]=x;//进站
visit[x]=1;//表示在栈里
for(int i=node[x];i!=-1;i=edge[i].next)
{
if(!dfn[edge[i].v]) {//如果没访问过
tarjan(edge[i].v);//往下进行延伸,开始递归
low[x]=min(low[x],low[edge[i].v]);//递归出来,比较谁是谁的儿子/父亲,就是树的对应关系,涉及到强连通分量子树最小根的事情。
}
else if(visit[edge[i].v ]){ //如果访问过,并且还在栈里。
low[x]=min(low[x],dfn[edge[i].v]);//比较谁是谁的儿子/父亲。就是链接对应关系
}
}
if(low[x]==dfn[x]) //发现是整个强连通分量子树里的最小根。
{
bcnt++;
do{
belong[stack[index]]=bcnt;
visit[stack[index]]=0;
index--; }while(x!=stack[index+1]);//出栈,并且输出。
}
return ;
}
int in[N],out[N],num[N];
void solve(int n)
{
tot=index=bcnt=0;
mm(dfn,0);
mm(low,0);
mm(belong,0);
mm(out,0);
mm(visit,0);
mm(num,0);
rep(i,1,n+1)
if(!dfn[i])
tarjan(i);
int ans1=0,ans2=0;
rep(i,1,n+1)
{
num[belong[i]]++;
for(int j=node[i];j!=-1;j=edge[j].next)
{
if(belong[i]!=belong[edge[j].v])
{
out[belong[i]]++;
}
}
}
int bits=0,ans;
rep(i,1,bcnt+1)
{
if(out[i]==0)
{
bits++;
ans=i;
}
}
if(bits>1)
{
pf("0\n");
}else
pf("%d\n",num[ans]);
}
int main()
{
int n,m;
while(~sf("%d%d",&n,&m))
{
mm(node,-1);
cnt=0;
rep(i,1,m+1)
{
int x,y;
sf("%d%d",&x,&y);
add_edge(x,y);
}
solve(n);
} return 0;
}

K - Popular Cows的更多相关文章

  1. poj2186 Popular Cows 题解——S.B.S.

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29642   Accepted: 11996 De ...

  2. 【图论】Popular Cows

    [POJ2186]Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34752   Accepted: ...

  3. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  4. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  5. POJ 2186 Popular Cows(Targin缩点)

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

  6. POJ2186 Popular Cows [强连通分量|缩点]

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 De ...

  7. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

  8. [强连通分量] POJ 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31815   Accepted: 12927 De ...

  9. POJ 2186 Popular Cows(强连通)

                                                                  Popular Cows Time Limit: 2000MS   Memo ...

随机推荐

  1. Deepin 15.4 个性化设置

    2017.10.03,开始使用 Deepin 15.4.1 桌面系统 Chrome 版本 60.0.3112.78(正式版本) (64 位) 1.开启 ls 别名: vim .bashrc 去掉以下代 ...

  2. java使用httpclient封装post请求和get的请求

    在我们程序员生涯中,经常要复用代码,所以我们应该养成时常整理代码的好习惯,以下是我之前封装的httpclient的post和get请求所用的代码: package com.marco.common; ...

  3. postman6 在Linux中,body和response字体显示不正常的解决方法

    在Linux中,postman的body和response使用的默认字体如果没有安装的话,会导致字体和光标的位置不一致,例如字体显示长度只有30,而光标在70的位置,导致编辑困难. 经查找css的定义 ...

  4. Nginx 反向代理504 Gateway Time-out

    location /ssfwpt { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_ ...

  5. github和gitlab并存

    原先在自己电脑中配置github的全局变量,只用它来玩github和码云的项目.现在用自己的电脑开发公司项目,公司项目使用的是gitlab,结果拉取代码时还需要手动输入用户名和密码,这还能忍受几次,但 ...

  6. Effective Java 第三版——80. EXECUTORS, TASKS, STREAMS 优于线程

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  7. Python实现下载文件的三种方法

    下面来看看三种方法是如何来下载zip文件的:方法一: import urllib print "downloading with urllib" url = 'http://www ...

  8. win下查找端口占用进程并定位到具体程序

    很多时候,我们会在服务器上运行多个java程序,但是在任务管理器里看到的都是一样的图标,想要找到我们想要的那个进程咋办呢? 列出所有端口进程 C:\Users\desp>netstat -ano ...

  9. Asp.Net WebApi上传图片

    webapi using System; using System.Collections; using System.Collections.Generic; using System.Diagno ...

  10. layui form表单自定义sm格式

    1. 新建以下sm样式,保存为layform_sm.css文件名,然后导入到layui.css的后面. .layui-input-sm,.layui-select-sm,.layui-textarea ...