POJ-2186-Popular Cows 解题报告
来源:互联网 发布:js判断div是否隐藏 编辑:程序博客网 时间:2024/06/06 15:44
强连通分量。题意:每一个牛的梦想都是成为一个最受欢迎的牛。假如A认为B是受欢迎的且B认为C是受欢迎的,那么A也认为C是受欢迎的。现在告诉你每个认为受欢迎的关系,你的任务是计算出被除了自己以外的所有牛认为受欢迎的牛的个数。
我的解题思路:这是一个需要缩点的强连通分量题。缩点后假如整个图会变成强连通分量图,假如有不止一个出度为0的强连通分量说明不会有任何牛受除了自己以外的所有牛欢迎,换而言之有被除了自己以外的所有牛认为受欢迎的牛的前提条件是缩点以后的强连通分量图里面出度为0的强连通分量只有1个。最后,答案就是这个出度为0的强连通分量的顶点数了。
我的解题代码:强连通分量Tarjan算法+缩点
#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;#define N 10002vector <int> e[N];int dfn[N], low[N];int vis[N];int belong[N]; //存储每个顶点属于哪个强连通分量int stack[N], stop; //手写栈以及栈顶指针int timer; //时间戳int outdeg[N]; //存储强连通分量的出度int sccn; //强连通分量个数int n, m;void InitRead();void DataProcess();void Tarjan(int x);int main(){ while (~scanf("%d %d", &n, &m)) { InitRead(); DataProcess(); } return 0;}void InitRead(){ stop = sccn = timer = 0; memset(vis, 0, sizeof(vis)); memset(outdeg, 0, sizeof(outdeg)); for (int i=1; i<=n; ++i) e[i].clear(); int a, b; for (int i=0; i<m; ++i) { scanf("%d %d", &a, &b); e[a].push_back(b); } return;}void DataProcess(){ for (int i=1; i<=n; ++i) { if (!vis[i]) { Tarjan(i); } } for (int i=1; i<=n; ++i) { int size = e[i].size(); for (int j=0; j<size; ++j) { if (belong[i] != belong[e[i][j]]) { outdeg[belong[i]]++; } } } int outscc = 0; //存储出度为0的强连通分量个数 int tmp; for (int i=1; i<=sccn; ++i) { if (outdeg[i] == 0) { outscc++; tmp = i; } } int ans = 0; if (outscc == 1) //只有一个出度为0的强连通分量 { for (int i=1; i<=n; ++i) //计算这个出度为0的强连通分量的顶点数 { if (belong[i] == tmp) { ans++; } } printf("%d\n", ans); return; } printf("0\n"); return;}void Tarjan(int x){ int y; dfn[x] = low[x] = ++timer; vis[x] = 2; stack[stop++] = x; int size = e[x].size(); for (int i=0; i<size; ++i) { if (!vis[e[x][i]]) { Tarjan(e[x][i]); low[x] = min(low[x], low[e[x][i]]); } else if (vis[e[x][i]] == 2) { low[x] = min(low[x], dfn[e[x][i]]); } } if (dfn[x] == low[x]) { sccn++; do { y = stack[--stop]; vis[y] = 1; belong[y] = sccn; } while (x != y); } return;}
0 0
- POJ-2186-Popular Cows 解题报告
- 有向图的汇点 -- 兼 ACM PKU 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
- Android Touch事件的传递机制
- 内核启动yaffs2文件系统出现Partially written xxx block detected
- ubuntu 中如何查看正在运行的进程以及结束进程
- 完美实现溢出文本省略
- eclipse 更改编码utf-8
- POJ-2186-Popular Cows 解题报告
- mongo客户端mongo VUE增删改查
- Eclipse “this compilation unit is not on the build path of a java project”
- ios CocoaLumberjack——带颜色的Log
- Ubuntu 14.04中文输入法的安装
- Java IO流分析整理
- 数据库作业开发二
- Android adb logcat的使用
- Oracle数据闪回