博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Popular Cows (POJ No.2186)
阅读量:5911 次
发布时间:2019-06-19

本文共 2866 字,大约阅读时间需要 9 分钟。

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 31 22 12 3

Sample Output

1 题解: 考虑以牛为顶点的有向图,对每个需对(A,B)连一条从A到B的边。我们不妨假设两头牛A,B都被其他牛认为是红牛。那么就知道A,B一定同属一个,即存在一个包含A,B两个顶点的圈。反之,如果一个牛被其他牛认为是红牛,那么他所属的强连通分量中的牛一定全部是红牛。所以我们只需要找出拓扑序最大的强连通分量的个数就可以了。 AC代码:
1 #include
2 #include
3 using namespace std; 4 const int MAXN=500000+10; 5 //------------------------- 6 void read(int &x){ 7 x=0;char ch=getchar();int f=1; 8 for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; 9 for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';10 x*=f;11 }12 //-------------------------13 int n,m,tmp;14 int topo[MAXN],cmp[MAXN];15 bool vis[MAXN];16 int first[MAXN],next[MAXN],v[MAXN],e;17 void AddEdge(int a,int b){18 v[++e]=b;19 next[e]=first[a];20 first[a]=e;21 }22 23 int rfirst[MAXN],rnext[MAXN],rv[MAXN],re;24 void rAddEdge(int a,int b){25 rv[++re]=b;26 rnext[re]=rfirst[a];27 rfirst[a]=re;28 }29 //-------------------------30 void dfs(int x){31 vis[x]=1;32 for(int i=first[x];i;i=next[i])33 if(!vis[v[i]])dfs(v[i]);34 topo[++tmp]=x;35 }36 37 void rdfs(int x,int k){38 vis[x]=1;39 cmp[x]=k;40 for(int i=rfirst[x];i;i=rnext[i])41 if(!vis[rv[i]])rdfs(rv[i],k);42 }43 //---------------------------44 int k=1;45 int scc(){46 memset(vis,0,sizeof(vis));47 memset(topo,0,sizeof(topo));48 for(int i=1;i<=n;i++){49 if(!vis[i])dfs(i);50 }51 memset(vis,0,sizeof(vis));52 for(int i=n;i>=1;i--)if(!vis[topo[i]])rdfs(topo[i],k++);53 return k-1;54 }55 //---------------------------56 int main(){57 read(n);read(m);58 for(int i=1;i<=m;i++){59 int x,y;60 read(x);read(y);61 AddEdge(x,y);62 rAddEdge(y,x);63 }64 int nn=scc();65 66 int u=0,num=0;67 for(int i=1;i<=n;i++)68 if(cmp[i]==nn){u=i;num++;}69 memset(vis,0,sizeof(vis));70 rdfs(u,0);71 for(int i=1;i<=n;i++)72 if(!vis[i]){73 num=0;74 break;75 }76 printf("%d\n",num);77 }
View Code
 

 

 

转载于:https://www.cnblogs.com/543Studio/p/5183515.html

你可能感兴趣的文章
oschina程序开发
查看>>
《从零开始学Swift》学习笔记(Day 40)——析构函数
查看>>
CDN相关
查看>>
查找(AVL平衡二叉树)
查看>>
POJ2406 Power Strings(KMP)
查看>>
极值问题(acms)
查看>>
SVN Hooks的介绍及使用
查看>>
JAVA核心编程教学
查看>>
APP的广告模式
查看>>
判断一个字符是否为数字的两种方法(C/C++)
查看>>
600. Non-negative Integers without Consecutive Ones
查看>>
axios 拦截 , 页面跳转, token 验证(自己摸索了一天搞出来的)
查看>>
区块链初始化与实现POW工作量证明
查看>>
C++的Json解析库:jsoncpp和boost .
查看>>
如何将经纬度利用Google Map API显示C# VS2005 Sample Code
查看>>
基于html5 canvas和js实现的水果忍者网页版
查看>>
Android 知识梳理
查看>>
【反射】使用反射来获取注解原数据信息-类信息-方法信息等
查看>>
如何合理的规划jvm性能调优
查看>>
莫比乌斯反演初步与实际应用
查看>>