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. java性能

    一.关于性能的基本知识  1.性能的定义  在我们讨论怎样提高Java的性能之前,我们需要明白“性能“的真正含义.我们一般定义如下五个方面作为评判性能的标准.  1) 运算的性能----哪一个算法的执 ...

  2. 通过Jquery中Ajax获取json文件数据

    1. JSON(JavaScript Object Notation): javaScript对象表示法: 是存储和交换文本信息的语法,比xml更小,更快,更易解析. 2. JSON基本书写格式 : ...

  3. Linux 命令 - grep: 正则搜索文本

    grep 搜索文本文件中与指定正则表达式匹配的行 命令格式 grep [OPTIONS] PATTERN [FILE...] 命令参数 Generic Program Information --he ...

  4. Linux 命令 - history: 显示或操作历史列表

    命令格式 history [-c] [-d offset] [n] history -anrw [filename] history -ps arg [arg...] 命令参数 -c 清除历史列表. ...

  5. CSS之边框属性

    border-style 属性用于设置元素所有边框的样式,或者单独地为各边设置边框样式. border-style:dotted solid double dashed; border-style的属 ...

  6. <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

    转自这里   出 错原因:设置app的状态栏样式的使用使用了旧的方式,在info.plist里面设置了View controller- based status bar appearance为NO,默 ...

  7. 记redis的一个测试

    现有020的系统架构走库存,取券通过Fetch前n条来实现买n张优惠券,但此做法在高并发时有严重的性能问题,性能问题主要体现在数据库. 为了优化此性能,系统改为redis,走队列模式,即生产者消费者. ...

  8. java坑之无法创建线程

    环境:linux 错误:java.lang.OutOfMemoryError: unable to create new native thread 原因:OS对线程是有限制 解决办法: 在Linux ...

  9. HTML标签语义化

    标签语义化 Web语义化是指使用语义恰当的标签,使页面有良好的结构,页面元素有含义,能够让人和搜索引擎都容易理解. 如果可以在合适的位置使用恰当的标签,那么写出来的页面语义明确,结构清晰,搜索引擎也可 ...

  10. 总是你 2008-3 (献给L之一)

    文/安然 总是你 是梦里的那份最温柔 轻轻碰触 不再敢轻易提及 总是你 是不经意的那份最感动 点点鲜活 从来就没有淡去 时间远走 我轻轻的收起 那段记忆 不再开启 但是 偶尔 偶尔 总是 总是在不经意 ...