POJ2186 (强连通分量缩点后出度为0的分量内点个数)
2015-10-23 22:58
309 查看
Popular Cows
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.
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27820 | Accepted: 11208 |
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 ispopular, 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 12 3
Sample Output
1
Hint
Cow 3 is the only cow of high popularity.Source
USACO 2003 Fall 题意:A喜欢B,B喜欢C,则A喜欢C,求被所有人喜欢的人的个数 思路:将每个强连通分量缩点,然后求每个点的出度,如果有多个无出度的点则无解,否则答案就是出度为0的强连通分量内点的个数
/* ID: LinKArftc PROG: 2186.cpp LANG: C++ */ #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; #define eps 1e-8 #define randin srand((unsigned int)time(NULL)) #define input freopen("input.txt","r",stdin) #define debug(s) cout << "s = " << s << endl; #define outstars cout << "*************" << endl; const double PI = acos(-1.0); const int inf = 0x3f3f3f3f; const int INF = 0x7fffffff; typedef long long ll; const int maxn = 10010; const int maxm = 50010; int n, m; struct Edge { int v, next; } edge[maxm]; int head[maxn], tot; void init() { tot = 0; memset(head, -1, sizeof(head)); } void addedge(int u, int v) { edge[tot].v = v; edge[tot].next = head[u]; head[u] = tot ++; } int dfn[maxn], low[maxn], belong[maxn];//每个点属于哪个连通分量 int scc;//连通分量总数,下标 1~scc int Time; int ins[maxn]; stack <int> s; vector <int> vec[maxn];//记录每个连通分量内的点 void tarjan(int u) { int v; low[u] = dfn[u] = ++ Time; s.push(u); ins[u] = true; for (int i = head[u]; i + 1; i = edge[i].next) { v = edge[i].v; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (ins[v]) low[u] = min(low[u], dfn[v]);//写出low[u] = min(low[u], low[v]);也可以 } if (low[u] == dfn[u]) { scc ++; do { v = s.top(); s.pop(); ins[v] = false; belong[v] = scc; vec[scc].push_back(v); } while (u != v); } } int outdeg[maxn]; int main() { //input; while (~scanf("%d %d", &n, &m)) { init(); int u, v; for (int i = 1; i <= m; i ++) { scanf("%d %d", &u, &v); addedge(u, v); } while (!s.empty()) s.pop(); for (int i = 1; i <= maxn; i ++) vec[i].clear(); memset(dfn, 0, sizeof(dfn)); memset(ins, 0, sizeof(ins)); Time = 0; scc = 0; for (int i = 1; i <= n; i ++) { if (!dfn[i]) tarjan(i); } memset(outdeg, 0, sizeof(outdeg)); for (int u = 1; u <= n; u ++) { for (int i = head[u]; i + 1; i = edge[i].next) { int v = edge[i].v; if (belong[u] == belong[v]) continue; outdeg[belong[u]] ++; } } int ans = 0; bool has = false; bool flag = false; int ii; for (int i = 1; i <= scc; i ++) { if (outdeg[i] == 0) { ii = i; if (has) { flag = false; break; } else { has = true; flag = true; } } } if (flag) printf("%d\n", vec[ii].size()); else printf("0\n"); } return 0; }
相关文章推荐
- 九度OJ 1118:数制转换 (进制转换)
- LPC43xx SGPIO Pattern Match Mode
- ogg实现oracle到sql server 2005的同步
- appium for hybrid app 处理webview
- 九度OJ 1118:数制转换 (进制转换)
- 【鸟哥的linux私房菜-学习笔记】首次使用相关知识、在线求助 man page
- 剑指Offer--二叉树的深度
- 【翻译】Ext JS最新技巧——2015-10-21
- C#winform窗体用户控件自定义事件
- 【翻译】Ext JS最新技巧——2015-10-21
- 九度OJ 1117:整数奇偶排序 (排序)
- 【翻译】Ext JS最新技巧——2015-10-21
- Java设计模式5:原型模式
- 九度OJ 1117:整数奇偶排序 (排序)
- iOS学习笔记之文件的压缩和解压(zip)
- 数组
- Java异常之try,catch,finally,throw,throws
- PHP的缓冲区
- iOS巅峰之Swift结构体和类, 枚举, 构造器, 继承, 协议, 扩展(完结篇)
- LiteHttp引言:智能的android网络通信框架