http://poj.org/problem?id=2186

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20191   Accepted: 8193

Description

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. 
 
【题解】:
    

有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,则说明此有向图肯定不连通。因此直接输出0。
 
缩点的意思就是说把求得的强连通分量的点集看成一个点
 
 
【code】:
 /**
Judge Status:Accepted Memory:2404K
Time:532MS Language:G++
Code Length:1971B Author:cj
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm> #define N 10010
using namespace std; vector<int> G[N];
stack<int> stk;
int pre[N],lowlink[N],sccno[N],scc_cnt,dfn_clock,out[N],counter[N]; void DFN(int u) //tarjan算法
{
lowlink[u] = pre[u] = ++dfn_clock;
stk.push(u);
int i;
for(i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
DFN(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(lowlink[u]==pre[u])
{
scc_cnt++; //强连通图的个数标记
while()
{
int x = stk.top();
stk.pop();
sccno[x] = scc_cnt;
if(x==u) break;
}
}
} void findscc(int n)
{
int i;
scc_cnt = dfn_clock = ;
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
for(i=;i<=n;i++)
if(!pre[i])
DFN(i);
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
int i;
for(i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b); // 得到图
}
findscc(n); //查找强连通图
int j;
memset(out,,sizeof(out));
memset(counter,,sizeof(counter)); for(i=;i<=n;i++) //遍历一边图,查找统计个点缩点后的出度
{
// cout<<sccno[i]<<" ";
for(j=;j<G[i].size();j++)
{
int v = G[i][j];
if(sccno[i]!=sccno[v])
{
out[sccno[i]]++; //出度
}
}
} for(i=;i<=n;i++)
{
counter[sccno[i]]++; //统计各个强连通分量中的节点个数
} int cnt =,ans = ;
for(i=;i<=scc_cnt;i++)
{
if(!out[i]) //出度为0的强连通分量
{
cnt++;
ans = counter[i]; //答案即为其中的点集数
}
} if(cnt==) printf("%d\n",ans);
else puts(""); return ;
}

poj 2186 Popular Cows (强连通分量+缩点)的更多相关文章

  1. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  2. POJ 2186 Popular Cows --强连通分量

    题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...

  3. POJ 2186 Popular Cows 强连通分量模板

    题意 强连通分量,找独立的块 强连通分量裸题 #include <cstdio> #include <cstdlib> #include <cstring> #in ...

  4. POJ 2186 Popular Cows(Targin缩点)

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

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

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

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

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

  7. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

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

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

  9. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

随机推荐

  1. Linux下RPM软件包的安装及卸载

    http://os.51cto.com/art/201001/177866.htm 在 Linux 操作系统下,几乎所有的软件均通过RPM 进行安装.卸载及管理等操作.RPM 的全称为Redhat P ...

  2. 限制特定ip访问oracle

    在9i中真正起作用的是sqlnet.ora文件,我们修改sqlnet.ora其实是最好最快的方法. 在sqlnet.ora中增加如下部分 ----------------------------- # ...

  3. Redis 命令 - Keys

    DEL key [key ...] Delete a key 127.0.0.1:6379> SET foo hello OK 127.0.0.1:6379> DEL foo hello ...

  4. double的值太大,以及补0

    当double的值太大的时候,比如1000000000 用DecimalFormat: double d = 1.0E7; System.out.println(new DecimalFormat(& ...

  5. Android 线程Thread的2种实现方法

    在讲解之前有以下三点要说明: 1.在Android中有两种实现线程Thread的方法: ①扩展java.long.Thread类: ②实现Runnable()接口: 2.Thread类是线程类,它有两 ...

  6. JavaBean在JSP中显示时间

    创建DateTimeBean的类,将其放置于org.caiduping.bean的包中,实现时间,星期的封装. package org.caiduping.bean; import java.text ...

  7. poi-3.11-beta2-20140822.jar操作excel方法

    poi-3.11-beta2-20140822.jar操作excel方法 根据不同类型读取值的方法: // 获取单元格内不同类型的值 public String getValueByType(HSSF ...

  8. Cocos2d-x开发实例:使用Lambda 表达式

    在Cocos2d-x 3.0之后提供了对C++11标准[1]的支持,其中的Lambda[2]表达式使用起来非常简洁.我们可以使用Lambda表达式重构上一节的实例. 我们可以将下面的代码: liste ...

  9. 高性能CSS(四)

    移除无匹配的样式 移除无匹配的样式,有两个好处: 第一,删除无用的样式后可以缩减样式文件的体积,加快资源下载速度: 第二,对于浏览器而言,所有的样式规则的都会被解析后索引起来,即使是当前页面无匹配的规 ...

  10. Sicily 1068欢迎提出优化方案

    1608. Digit Counting 限制条件 时间限制: 1 秒, 内存限制: 32 兆 题目描述 Trung is bored with his mathematics homeworks. ...