Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 28379   Accepted: 11488

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
题意:A认为B受欢迎,B认为C受欢迎,那么A就认为C受欢迎。给定N头牛,M个认为受欢迎情况。确定受所有牛欢迎的这些牛的数目。
思路:首先判断有向图是否连通,若不连通则答案为0。若连通则进行缩点(注意有向图与无向图的缩点的不同之处),缩点之后得到一个有向树。若树中存在多个叶子结点,则答案为0,否则答案为 缩成叶子结点的那个连通分量重结点的数目。
下面是第二种有向图缩点方法,一次dfs,边反向后再进行一次rdfs.解释见代码
#include"cstdio"
#include"cstring"
#include"vector"
using namespace std;
const int MAXN=;
int V,E;
vector<int> G[MAXN];
vector<int> rG[MAXN];
vector<int> vs;
bool used[MAXN];
int cmp[MAXN];
void add_edge(int u,int v)
{
G[u].push_back(v);
rG[v].push_back(u);
}
void dfs(int u)
{
used[u]=true;
for(int i=;i<G[u].size();i++)
if(!used[G[u][i]]) dfs(G[u][i]);//假设u所能访问的结点都处于同一连通分量
vs.push_back(u); //后续遍历
}
void rdfs(int u,int k)
{
used[u]=true;
cmp[u]=k;
for(int i=;i<rG[u].size();i++)
if(!used[rG[u][i]]) rdfs(rG[u][i],k);
}
int scc()
{
memset(used,false,sizeof(used));
for(int i=;i<=V;i++)
if(!used[i]) dfs(i);
memset(used,false,sizeof(used));
int k=;
for(int i=vs.size()-;i>=;i--)//倒着访问,保证边反向后各个连通分量互不影响
if(!used[vs[i]]) rdfs(vs[i],k++);//对应后续遍历,若边反向后,起点u仍能遍历整个连通分量,那么它们处以同一连通分量中.否则处于不同的连通分量
return k;
}
int deg[MAXN];
int seek()
{
memset(deg,,sizeof(deg));
int k=scc();
for(int i=;i<=V;i++)
for(int j=;j<G[i].size();j++)
{
int to=G[i][j];
if(cmp[i]!=cmp[to])
{
deg[cmp[i]]++;
}
} int v=-;
int flag=;
int ans=;
for(int i=;i<=V;i++)
if(deg[cmp[i]]==)
{
ans++;
if(cmp[i]!=v)
{
v=cmp[i];
flag++;
}
if(flag>) return ;
}
return ans;
}
int main()
{
while(scanf("%d%d",&V,&E)!=EOF)
{
for(int i=;i<=V;i++)
{
vs.clear();
G[i].clear();
rG[i].clear();
cmp[i]=;
}
for(int i=;i<E;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
}
int ans=seek();
printf("%d\n",ans);
} return ;
}
标准tarjan算法对有向图缩点。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=;
vector<int> mp[MAXN];
int n,m;
int dfn[MAXN],low[MAXN],time;
int stack[MAXN],top;
bool ins[MAXN];
int belong[MAXN],cnt;
void dfs(int u)
{
dfn[u]=low[u]=++time;
stack[top++]=u;
ins[u]=true;
for(int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
cnt++;
int v;
do{
v=stack[--top];
belong[v]=cnt;
ins[v]=false;
}while(u!=v);
}
}
int deg[MAXN];
void cal()
{
memset(deg,,sizeof(deg));
int res=;
for(int i=;i<=n;i++)
for(int j=;j<mp[i].size();j++)
{
int v=mp[i][j];
if(belong[i]!=belong[v])
{
deg[belong[i]]++;
}
}
int mark;
for(int i=;i<=cnt;i++)
if(deg[i]==)
{
mark=i;
res++;
}
if(res!=)
{
printf("0\n");
return;
}
res=;
for(int i=;i<=n;i++)
if(belong[i]==mark)
res++;
printf("%d\n",res);
}
int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
mp[i].clear();
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
time=;
top=;
cnt=;
memset(ins,false,sizeof(ins));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u].push_back(v);
} for(int i=;i<=n;i++)
if(!dfn[i])
dfs(i);
cal();
}
return ;
}
												

POJ2186(有向图缩点)的更多相关文章

  1. hdu 3072 有向图缩点成最小树形图计算最小权

    题意,从0点出发,遍历所有点,遍历边时候要付出代价,在一个SCC中的边不要付费.求最小费用. 有向图缩点(无需建立新图,,n<=50000,建则超时),遍历边,若不在一个SCC中,用一个数组更新 ...

  2. HDU1269(有向图缩点模板题)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. POJ2553( 有向图缩点)

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9779   Accepted:  ...

  4. POJ1904(有向图缩点+输入输出挂参考)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 8311   Accepted: 3017 Cas ...

  5. hdu 1827 有向图缩点看度数

    题意:给一个有向图,选最少的点(同时最小价值),从这些点出发可以遍历所有. 思路:先有向图缩点,成有向树,找入度为0的点即可. 下面给出有向图缩点方法: 用一个数组SCC记录即可,重新编号,1.... ...

  6. HDU 4635 (完全图 和 有向图缩点)

    题目链接:HDU  4635 题目大意: 给你一个有向图,加有向边,使得这个图是简单有向图.问你最多加多少条有向边. 简单有向图: 1.不存在有向重边. 2.不存在图循环.(注意是不存在 “图” 循环 ...

  7. 对Tarjan——有向图缩点算法的理解

    开始学tarjan的时候,有关无向图的割点.桥.点双边双缩点都比较容易地理解了,唯独对有向图的缩点操作不甚明了.通过对luoguP2656_采蘑菇一题的解决,大致搞清了tarjan算法的正确性. 首先 ...

  8. hdu 3639 有向图缩点+建反向图+搜索

    题意:给个有向图,每个人可以投票(可以投很多人,一次一票),但是一个人只能支持一人一次,支持可以传递,自己支持自己不算,被投支持最多的人. 开始想到缩点,然后搜索,问题是有一点想错了!以为支持按票数计 ...

  9. poj2553 有向图缩点,强连通分量。

    //求这样的sink点:它能达到的点,那个点必能达到他,即(G)={v∈V|任意w∈V:(v→w)推出(w→v)} //我法:tarjan缩点后,遍历点,如果该点到达的点不在同一个强连通中,该点排除, ...

随机推荐

  1. HDFS源码分析心跳汇报之周期性心跳

    HDFS源码分析心跳汇报之周期性心跳,近期推出!

  2. JQ动态获取数据

    转:JQUERY获取浏览器窗口的高度和宽度 June 27, 2012 <script type="text/javascript"> $(document).read ...

  3. PowerBuilder -- 键盘对应的枚举值

    KeyCode values for keyboard keys Type of key KeyCode values and descriptions Mouse buttons KeyLeftBu ...

  4. Touch ID和Passcode框架,Apple Watch风格的应用布局

    本文转载至 http://www.cocoachina.com/ios/20141031/10110.html 水平滚动条(artwalk) 测试环境:Xcode 6.0,iOS 8.0     VE ...

  5. async & await (转载)

    async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们 编程埋下了一些 ...

  6. 【题解】At2370 Piling Up

    [题解]At2370 Piling Up \[ dp(i,j,0/1) \\ 正在进行i项操作并且此时黑球剩下j个,黑球[0/1]数量曾经到过0 \\ 为什么加第二位,判重.怎么想到的? \] 非常神 ...

  7. 爬虫之重要的requests模块

    一 . requests模块 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半 ...

  8. swift中反向循环

    First of all, protocol extensions change how reverse is used: for i in (1...5).reverse() { print(i) ...

  9. Database: index

    The whole point of having an index is to speed up search queries by essentially cutting down the num ...

  10. spring-boot4代码

    App.java package com.kfit; import org.springframework.boot.SpringApplication; import org.springframe ...