poj 2186 Popular Cows
来源:互联网 发布:谭浩强c语言电子书pdf 编辑:程序博客网 时间:2024/06/01 09:52
题目链接:Popular Cows
题目大意:有n个奶牛,m个关系,如果a认为b是受欢迎的,b认为c是受欢迎的,那么a认为c是受欢迎的,问有几个奶牛被其他所有的奶牛都认为是受欢迎的
题目思路:先建有向图,那么问题可以转化为有多少个点被其他所有点访问到,我们可以先缩点,然后看出度为0的连通块有多少个,只能有1个,否则不可能存在这样的奶牛,然后看这个出度为0的连通块里面有多少个点,输出就好
#include <map>#include <set>#include <cmath>#include <stack>#include <queue>#include <vector>#include <cstdio>#include <string>#include <utility>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 20010;vector<int>Edge[maxn];stack<int>S;int Dfn[maxn],Low[maxn],sccno[maxn],tclock,sccnt;int OutDeg[maxn],numscc[maxn],popu;void tarjan(int u){ Dfn[u] = Low[u] = ++tclock; S.push(u); for(int i = 0;i < Edge[u].size();i++){ int v = Edge[u][i]; if(!Dfn[v]){ tarjan(v); Low[u] = min(Low[u],Low[v]); } else if(!sccno[v]){ Low[u] = min(Low[u],Dfn[v]); } } if(Low[u] == Dfn[u]){ sccnt ++; int v = -1; while(v != u){ v = S.top(); S.pop(); sccno[v] = sccnt; numscc[sccnt]++; } }}void findscc(int n){ tclock = sccnt = 0; memset(Dfn,0,sizeof(Dfn)); memset(Low,0,sizeof(Low)); memset(sccno,0,sizeof(sccno)); memset(numscc,0,sizeof(numscc)); for(int i = 1;i <= n;i++) if(!Dfn[i]) tarjan(i);}int solve(int n){ memset(OutDeg,0,sizeof(OutDeg)); popu = 0; for(int u = 1;u <= n;u++){ for(int i = 0;i < Edge[u].size();i++){ int v = Edge[u][i]; if(sccno[u] != sccno[v]){ OutDeg[sccno[u]]++; } } } int c = 0; for(int i = 1;i <= sccnt;i++){ if(OutDeg[i] == 0){ if(c == 0) {popu = numscc[i];c++;} else popu = 0; } } return popu;}int main(){ int T,n,m; while(~scanf("%d%d",&n,&m)){ for(int i = 1;i <= n;i++) Edge[i].clear(); while(m--){ int u,v; scanf("%d%d",&u,&v); Edge[u].push_back(v); } findscc(n); printf("%d\n",solve(n)); } return 0;}
阅读全文
0 0
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186(Popular Cows)
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- POJ 2186: Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- Linux、ubuntu下pip安装aiohttp失败,修改python默认版本
- Mysql常见知识点
- Weekly(学员端、导师端)项目总结
- loadrunner Web_类函数之web_reg_save_param_xpath()
- 学生信息管理系统之优化问题
- poj 2186 Popular Cows
- Linux日记(3)——掌握常用的Linux命令
- 素数表,区间素数筛
- JavaScript面向对象编程之多态
- Selemium基础知识点
- Hbase rowKey 最佳实践 和 mysql id 对比
- 操作系统
- 机器学习笔记(二)模型评估与选择
- 任务调度学习-----Quartz和Timer