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. linux查看某个时间段的log

    若想在linux下查询某个时间段的log,用sed命令示例如下: $ sed -n '/2017-01-04 11:00:00/,/2017-01-04 11:20:55/p'  ejabberd.l ...

  2. 再说WCF Data Contract KnownTypeAttribute

    WCF 中的序列化是用DataContractSerializer,所有被[DataContract]和[DataMemeber]标记的类和属性会被DataContractSerializer序列化. ...

  3. lodash (js实用工具库)

    是什么? 它提供了一整套函数式编程的实用功能, 并且支持模块化, 比underscore更优秀. 文档? http://lodashjs.com/docs/ 引用? <script src=&q ...

  4. ubuntu 下解决sublime v3 中文输入法时 退格键删除不了拼音的问题

    ubuntu下,sulime想要支持中文需要这样设置: 1.安装中文输入解决的github git clone https://github.com/lyfeyaj/sublime-text-imfi ...

  5. JavaScript 如何创建search字段

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. python cookbook第三版学习笔记十三:类和对象(四)描述器

    __get__以及__set__:假设T是一个类,t是他的实例,d是它的一个描述器属性.读取属性的时候T.d返回的是d.__get__(None,T),t.d返回的是d.__get__(t,T).说法 ...

  7. java 解析excle

    jjava解析excle或者csv文件并导出到web界面: 创建ExcelShower.java package com.ssm.controller; import java.io.File; im ...

  8. LightOJ - 1395 A Dangerous Maze (II) —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1395 1395 - A Dangerous Maze (II)    PDF (English) Statistic ...

  9. UEditor上传文件的默认地址修改

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text.Re ...

  10. c语言学习的第11天 指针

    #include<stdio.h> int main(void) { int * p; int i=3; int j; p=&i; j=*p; printf("i=%d, ...