poj--2186--Popular Cows (scc+缩点)
Popular Cows
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 1
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.
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
3 3 1 2 2 1 2 3
1 /*找出最受欢迎的牛*/ #include<stdio.h> #include<string.h> #include<queue> #include<stack> #include<vector> #include<algorithm> using namespace std; #define MAX 50010 struct node { int u,v; int next; }edge[MAX]; int low[MAX],dfn[MAX]; int sum,sumin,sumout; int sccno[MAX],scc_cnt; int head[MAX],dfs_clock,cnt; bool Instack[MAX]; vector<int>G[MAX]; vector<int>scc[MAX]; stack<int>s; int in[MAX],out[MAX]; int m,n; void init() { memset(head,-1,sizeof(head)); cnt=0; } void add(int u,int v) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } void getmap() { int a,b; while(m--) { scanf("%d%d",&a,&b); add(a,b); } } void tarjan(int u,int fa) { int v; low[u]=dfn[u]=++dfs_clock; s.push(u); Instack[u]=true; for(int i=head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); } else if(Instack[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { scc_cnt++; scc[scc_cnt].clear(); for(;;) { v=s.top(); s.pop(); Instack[v]=false; scc[scc_cnt].push_back(v); sccno[v]=scc_cnt; if(u==v) break; } } } void find(int l,int r) { memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(sccno,0,sizeof(sccno)); memset(Instack,false,sizeof(Instack)); scc_cnt=dfs_clock=0; for(int i=l;i<=r;i++) if(!dfn[i]) tarjan(i,-1); } void suodian() { for(int i=1;i<=scc_cnt;i++) G[i].clear(),in[i]=0,out[i]=0; for(int i=0;i<cnt;i++) { int u=sccno[edge[i].u]; int v=sccno[edge[i].v]; if(v!=u) { G[u].push_back(v); out[u]++,in[v]++; } } } void solve() { int sum=0; int ans=0; for(int i=1;i<=scc_cnt;i++) { if(out[i]==0) //出度为零说明在新图里他是叶子节点,现在统计叶子节点个数 { sum++; ans+=scc[i].size(); } } if(sum>1) printf("0\n"); if(sum==0) printf("%d\n",n); //如果只有1个scc,说明全部的牛都是最受欢迎的 if(sum==1) printf("%d\n",ans); //如果sum==0说明在新图里叶子节点只有一个 //那么当前的scc中所有的牛都是最受欢迎的 } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); getmap(); find(1,n); suodian(); solve(); } return 0;}
poj--2186--Popular Cows (scc+缩点)的更多相关文章
- POJ 2186 Popular cows(SCC 缩点)
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...
- POJ 2186 Popular Cows tarjan缩点算法
题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27698 Accepted: 11148 De ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
随机推荐
- [转]ORACLE EXECUTE IMMEDIATE 小结
转自:http://www.cnblogs.com/huanghai223/archive/2011/06/29/2093660.html EXECUTE IMMEDIATE 代替了以前Oracl ...
- jquery中的left和top
left 和 top /*1. 获取元素基于定位容器的位置*/ /*返回的是对象 属性 left top */ var position = $('.inner').position(); conso ...
- Node.js文件系统Api总结
//公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile(filename,[options ...
- Android 存储路径选择
Android能用来存储的地方有两个,一个是手机内置的存储空间,一个是外置的SD卡,内置的存储空间一般比较小,所以应用的缓存建议存储在外置的SD卡中. 在Android系统中如何获得存储的路径呢? p ...
- SSIS SQL Server配置自动作业
目录: 一. 用SSMS配置作业,自助调度: 二.用SSMS调SSIS包: 一. 用SSMS配置作业,自助调度: 为验证数据,先创建一个表: CREATE TABLE test_table (id I ...
- 向properties文件中写入信息(针对获取properties文件失败的总结)
前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下 ...
- C++程序设计实验安排
2016-2017第二学期C++程序设计的实验时间与地点安排如下表,请大家根据时间按时来上机实验.另外,因为原来安排在4.1的实验因为调休补周一的课,因此挪至周五.另外第4次周六的课,考虑有一些同学有 ...
- RecyclerView 悬浮/粘性头部效果3种方式
但是以上两种方式onDrawOver()方法实现逻辑对初次查看该段代码要花时间理解.下面代码逻辑(原理一样,同样参考大神代码)相对清晰,易理解 public class StickyDecoratio ...
- XML的解析方式
//解析和输出XML public void showXml() { string filepath = Application.dataPath + @"/my.xml"; if ...
- BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数
Description The cows have once again tried to form a startup company, failing to remember from past ...