POJ - 3660 Cow Contest(emmmm-spfa)

Cow Contest

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15022   Accepted: 8392

Description

N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ NA ≠ B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

Output

* Line 1: A single integer representing the number of cows whose ranks can be determined
 

Sample Input

5 5
4 3
4 2
3 2
1 2
2 5

Sample Output

2

Source

USACO 2008 January Silver

                给你一些比赛结果,问你能确定名次的牛的数是多少~~;

                能够确定名次,就说明这头牛有间接打败关系的牛的数量+有间接被打败的牛的数量=n-1就好了;

               网上很多人用的是floyd算法直接n*3暴力求,确实代码简短,我也不再贴了,这里主要将领一个思路,用的是最短路的spfa的放法;时间复杂度低一些;

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
int n, m, cnt, ucnt;
int head[105];
int uhead[105];
int vis[105];
int d[105];
int ud[105];
struct ***
{
	int to, ne;
}ed[10005],ued[10005];
void init()
{
	for (int s = 1; s <= n; s++)
	{
		head[s] = -1;
		uhead[s] = -1;
	}
	cnt = 0;
}
void add(int from, int to)
{
	ed[cnt].to = to;
	ed[cnt].ne = head[from];
	head[from] = cnt++;
}
void uadd(int from, int to)
{
	ued[ucnt].to = to;
	ued[ucnt].ne = uhead[from];
	uhead[from] = ucnt++;
}
int spfa(int st, int d[],*** ed[],int head[])
{
	for (int s = 1; s <= n; s++)
	{
		d[s] = inf;
		vis[s] = 0;
	}
	queue<int>q;
	q.push(st);
	vis[st] = 1;
	d[st] = 0;
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		vis[t] = 0;
		for (int s = head[t]; ~s; s = ed[s].ne)
		{
			if (d[ed[s].to] != 0)
			{
				d[ed[s].to] = 0;
				if (!vis[ed[s].to])
				{
					vis[ed[s].to] = 1;
					q.push(ed[s].to);
				}
			}
		}
	}
	int sum = 0;
	for (int s = 1; s <= n; s++)
	{
		if (d[s] == 0)
		{
			sum++;
		}
	}
	return sum - 1;
}
int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		init();
		while (m--)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			add(a, b);
			uadd(b, a);
		}
		int sum = 0;
		for (int s = 1; s <= n; s++)
		{
			if (spfa(s, d, ed, head) + spfa(s, ud, ued, uhead) == n - 1)
			{
				sum++;
			}
		}
		printf("%d\n", sum);
	}
}

 

全部评论

相关推荐

赛博小保安:你这简历没啥大问题的,经历技能也足够了,问题应该就是出在出身了,学院本就是这样,HR忙着跟92的勾搭呢,哪有心思看我们这些双非😿😭
点赞 评论 收藏
分享
头像 会员标识
09-26 19:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 你的mentor是什么样的人? #
4503次浏览 33人参与
# 你觉得mentor喜欢什么样的实习生 #
10715次浏览 297人参与
# 智慧芽求职进展汇总 #
25936次浏览 110人参与
# 帮我看看,领导说这话什么意思? #
6647次浏览 26人参与
# 26届秋招公司红黑榜 #
13171次浏览 44人参与
# 怎么给家人解释你的工作? #
1646次浏览 17人参与
# 未岚大陆求职进展汇总 #
38145次浏览 114人参与
# 没有家庭托举的我是怎么找工作的 #
12676次浏览 160人参与
# 求职低谷期你是怎么度过的 #
5423次浏览 96人参与
# 实习必须要去大厂吗? #
146833次浏览 1542人参与
# 从哪些方向判断这个offer值不值得去? #
6777次浏览 95人参与
# 同bg的你秋招战况如何? #
158879次浏览 927人参与
# 度小满求职进展汇总 #
10221次浏览 53人参与
# 校招泡的最久的公司是哪家? #
4840次浏览 23人参与
# 面试紧张时你会有什么表现? #
1802次浏览 21人参与
# 你有哪些缓解焦虑的方法? #
37205次浏览 835人参与
# 你喜欢工作还是上学 #
77620次浏览 860人参与
# 入职第一天,你准备什么时候下班 #
85519次浏览 467人参与
# 秋招想进国企该如何准备 #
97750次浏览 487人参与
# 简历无回复,你会继续海投还是优化再投? #
103620次浏览 819人参与
# 机械人的工作环境真的很差吗 #
25086次浏览 119人参与
# 独居后,你的生活是更好了还是更差了? #
28154次浏览 263人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务