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. static修饰内部类

    创建内容类的方式通过外部类的实例对象来创建 public class AA { int a =1; class BB { int b=3 ; } public static void main(Str ...

  2. 将到来的战略转变:移动 Web 还是移动 Apps?

    目前来看,移动应用比移动网站的易用性更高,但变化即将发生,移动网站最终将优于Apps,成为更好的策略选择. 一家公司制定移动策略时,最重要的问题是:是否需要考虑为移动设备特别做点什么.一些公司永远都不 ...

  3. java多线程那些事之中的一个

    1.  Callable 接口 获取线程运行状态(get.get(long timeout)),取消线程(cancel(boolean  mayinterruptifrunning)).isCance ...

  4. KEIL下分散加载文件的使用(zt)

    KEIL下分散加载文件的使用   对于分散加载的概念,在<ARM体系结构与编程>书中第11章有明确介绍. 分散加载文件(即scatter file 后缀为.scf)是一个文本文件,通过编写 ...

  5. Android 超高仿微信图片选择器 图片该这么载入

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39943731,本文出自:[张鸿洋的博客] 1.概述 关于手机图片载入器,在当今像 ...

  6. MySQL 优化1

    系统在应用时间很长的情况下会慢慢变得很慢,无论是人还是机器为了更好的工作和学习都需要适当学习.数据库也是一样的用久了, 自然就会产生空间碎片,需要我们都i数据库中的数据块进行维护和整理.下面以实例来说 ...

  7. MySQL mysqlbinlog

    MySQL binlog日志记录了MySQL数据库从启用日志以来所有对当前数据库的变更.binlog日志属于二进制文件,我们可以从binlog提取出来生成可读的文本或者SQL语句来重建当前数据库以及根 ...

  8. Dominant Resource Fairness: Fair Allocation of Multiple Resource Types

    Dominant Resource Fairness: Fair Allocation of Multiple Resource Types

  9. thinkphp5 (最棒的php开源框架)

    tp5的唯一可访问目录是public,即项目根目录: http://localhost/tp5/public/ 开发规范: 类库.函数文件统一以.php为后缀 类(命名和路径)和命名空间保持一致 类文 ...

  10. Webpack探索【8】--- 模块热替换详解

    本文主要讲模块热替换相关内容.