ACM: 图论题 poj 2186 强连通分量
来源:互联网 发布:win7如何安装linux系统 编辑:程序博客网 时间:2024/05/19 10:40
PopularCows
Description
Every cow's dream is to becomethe most popular cow in the herd. In a herd of N (1<= N <= 10,000) cows, you are givenup to M (1 <= M <= 50,000) orderedpairs of the form (A, B) that tell you that cow A thinks that cow Bis popular. Since popularity is transitive, if A thinks B ispopular and B thinks C is popular, then A will also think that Cis
popular, even if this is not explicitly specified by an orderedpair in the input. Your task is to compute the number of cows thatare considered popular by every other cow.
popular, even if this is not explicitly specified by an orderedpair in the input. Your task is to compute the number of cows thatare considered popular by every other cow.
Input
* Line 1: Two space-separatedintegers, N and M
* Lines 2..1+M: Two space-separated numbers A and B, meaning that Athinks B is popular.
* Lines 2..1+M: Two space-separated numbers A and B, meaning that Athinks B is popular.
Output
* Line 1: A single integer thatis the number of cows who are considered popular by every othercow.
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Cow 3 is the only cow of highpopularity.
题意: 给出每头牛u觉得牛v是受欢迎的, 欢迎是具有传递性的,u->v, v->e ==>u->e
解题思路:
代码:
#include<cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 50005
#define MAXSIZE 10005
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 50005
#define MAXSIZE 10005
struct node
{
int v;
int next;
}edges[MAX];
{
}edges[MAX];
int n, m;
int first[MAX], num;
int stack[MAXSIZE], top, bcc[MAXSIZE], count;
int dfn[MAXSIZE], low[MAXSIZE];
bool vis[MAXSIZE], flag[MAXSIZE];
int temp[MAXSIZE];
int first[MAX], num;
int stack[MAXSIZE], top, bcc[MAXSIZE], count;
int dfn[MAXSIZE], low[MAXSIZE];
bool vis[MAXSIZE], flag[MAXSIZE];
int temp[MAXSIZE];
inline int min(int a, intb)
{
return a < b ? a : b;
}
{
}
inline void add(int u, intv)
{
edges[num].v =v;
edges[num].next = first[u];
first[u] = num++;
}
{
}
void readGraph()
{
num = top = count = 0;
memset(first, -1, sizeof(first));
memset(vis, false, sizeof(vis));
memset(bcc, 0, sizeof(bcc));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(temp, 0, sizeof(temp));
memset(flag, true, sizeof(flag));
int u, v;
for(int i = 0; i < m; ++i)
{
scanf("%d %d",&u, &v);
add(u, v);
}
}
{
}
void tarjan(int u, intdeep)
{
dfn[u] = low[u] = deep+1;
vis[u] = true;
stack[top++] = u;
for(int e = first[u]; e != -1; e =edges[e].next)
{
int v = edges[e].v;
if( !low[v] )
{
tarjan(v,deep+1);
low[u] =min(low[u], low[v]);
}
else if( vis[v] )
low[u] =min(low[u], dfn[v]);
}
{
}
int solve()
{
int i;
int result = 0;
int number = 0;
for(i = 1; i <= n; ++i)
{
if( !low[i] )
tarjan(i, 1);//i, deep
}
{
}
int main()
{
//freopen("input.txt", "r", stdin);
while(scanf("%d %d", &n,&m) != EOF)
{
readGraph();
int result = solve();
printf("%d\n", result);
}
return 0;
}
{
//
}
0 0
- ACM: 图论题 poj 2186 强连通分量
- ACM: 图论题 poj 1236 强连通
- ACM: 无向图的强连通分量 图论题 p…
- ACM: 图论题 poj 2240 (floyd + ST…
- ACM: 图论题 poj 1135
- ACM: 图论题 poj 3…
- ACM: 图论题 poj 1…
- ACM: 图论题 poj 1…
- ACM: 二分法 数论题 poj 1905
- ACM: 匈牙利算法 图论题 poj 3041 …
- ACM: 差分约束 图论题 poj 2983 sp…
- ACM: 有向欧拉图 图论题 poj 1386 …
- ACM: 图论题 poj 3177 同 3352
- ACM: 图论题 poj 2253 (poj上的dou…
- ACM: polay定理 数论题 poj 1286 …
- ACM: 二分图最佳匹配 图论题 poj 3…
- ACM: 二分图最大匹配 图论题 poj 2…
- ACM: 图论题 poj 1161 (把图重建成…
- 兴趣学习: opencv -- 视频读写, 鼠…
- 兴趣学习: opencv 实现画图程序
- ACM: 图论题 poj 3…
- ACM: 动态规划题 poj&nb…
- ACM: 博弈题 poj 1143 状态压缩
- ACM: 图论题 poj 2186 强连通分量
- ACM: 图论题 poj 3352 双连通题
- ACM: 图论题 poj 3177 同 3352
- Android的自定义控件起步
- dos批处理中%~dp0%的说明
- ACM: 图论题 poj 3308 最大流问题
- ACM: 搜索题 poj 1020
- ACM: 图论题 poj 1201 差分约束
- ACM: 图论题 poj 1149 网络流问题