↓ Skip to main content
  1. Posts/

BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛(floyd,传递闭包)

·993 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

1612: [Usaco2008 Jan]Cow Contest奶牛的比赛
#

Time Limit: 5 Sec Memory Limit: 64 MB Submit: 900 Solved: 597 [Submit][Status][Discuss]

Description
#

FJ的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:)。在赛场上,奶牛们按1..N依次编号。每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平不相上下,也就是说,奶牛们的编程能力有明确的排名。 整个比赛被分成了若干轮,每一轮是两头指定编号的奶牛的对决。如果编号为A的奶牛的编程能力强于编号为B的奶牛(1 <= A <= N; 1 <= B <= N; A != B) ,那么她们的对决中,编号为A的奶牛总是能胜出。 FJ想知道奶牛们编程能力的具体排名,于是他找来了奶牛们所有 M(1 <= M <= 4,500)轮比赛的结果,希望你能根据这些信息,推断出尽可能多的奶牛的编程能力排名。比赛结果保证不会自相矛盾。

Input
#

  • 第1行: 2个用空格隔开的整数:N 和 M

  • 第2..M+1行: 每行为2个用空格隔开的整数A、B,描述了参加某一轮比赛的奶 牛的编号,以及结果(编号为A,即为每行的第一个数的奶牛为 胜者)

Output
#

  • 第1行: 输出1个整数,表示排名可以确定的奶牛的数目

Sample Input
#

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

Sample Output
#

2

输出说明:

编号为2的奶牛输给了编号为1、3、4的奶牛,也就是说她的水平比这3头奶 牛都差。而编号为5的奶牛又输在了她的手下,也就是说,她的水平比编号为5的 奶牛强一些。于是,编号为2的奶牛的排名必然为第4,编号为5的奶牛的水平必 然最差。其他3头奶牛的排名仍无法确定。

题意:给出m场比赛结果,问能确定多少头奶牛的排名。

思路:容易联想到拓扑排序。。。然而并不对。。其实这题之前做过。。。能确定一头奶牛的条件是,知道它和其他n-1头奶牛的关系,被打败或者打败都可以…由于n才100,直接floyd,传递闭包。

同poj 3660

poj3660解题报告

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月02日 星期六 15时20分50秒
 4File Name :code/bzoj/1612.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34int d[N][N];
35int n,m;
36
37int main()
38{
39	#ifndef  ONLINE_JUDGE
40	freopen("code/in.txt","r",stdin);
41  #endif
42
43	ios::sync_with_stdio(false);
44	cin>>n>>m;
45	ms(d,0);
46	for ( int i = 1 ;i  <= m ; i++)
47	{
48	    int u,v;
49	    cin>>u>>v;
50	    d[u][v]=1;
51
52	}
53
54	for ( int k = 1 ; k <= n ; k++)
55	    for ( int i = 1 ; i <= n ; i++)
56		for ( int j = 1 ; j <= n ; j++)
57		    d[i][j] = d[i][j]|(d[i][k]&&d[k][j]);
58
59	int ans = 0 ;
60	for ( int i = 1 ;i <= n ; i++)
61	{
62	    int cnt = 0;
63	    for ( int j = 1 ; j <= n ; j++)
64	    {
65		if (i==j) continue;
66		if (d[i][j]||d[j][i]) cnt++;
67	    }
68	    if (cnt==n-1) ans++;
69	}
70	printf("%d\n",ans);
71
72  #ifndef ONLINE_JUDGE
73  fclose(stdin);
74  #endif
75    return 0;
76}

Related

poj 3660 Cow Contest (floyd,传递闭包)

·492 words·1 min
http://poj.org/problem?id=3660 题意:给定n个奶牛,m个奶牛的关系,a,b表示a比b强…问能确定多少个奶牛的排名。 思路:最重要的一点是。。能确定奶牛i的排名的条件是。。知道奶牛i和其他n-1个奶牛的关系。。不管是能打败奶牛i也好。。会被奶牛i打败也好。。只要不是不确定就行。。所以我们跑一遍floyd做传递闭包。得到任何两个点之间的联系。然后对于每一个点。看其他n-1个点是否和他有关系。

bc #74 div1 1001 || hdu 5636 Shortest Path (floyd?)

·574 words·2 mins
题目链接 题意:有一条n个节点的链,节点i和节点j的距离为abs(i-j) 现在新增加三条边,距离也都为1,然后给出m个询问,每组询问给出两个点s,t,问s,t之间的最短距离。 思路:比赛的时候没搞出来。 观察特点,对于大多数点来说,都是没有直接的改变,只是增加了三条边。总的思路是:之前s到t的距离为abs(s-t),通过枚举中间经过的特殊点,观察是否能使得距离减小。

codeforces 500 B. New Year Permutation

·499 words·1 min
http://codeforces.com/contest/500/problem/B 题意:给定一个1至n的数的一种排列。给定一个n*n的矩阵,a[i][j]==0代表pi,pj不可以交换,a[i][j]为1代表p[i],p[j]可以交换。 问字典序最小的排列。。

POJ 2253 - Frogger (floyd)

·622 words·2 mins
A - Frogger **Time Limit:**1000MS **Memory Limit:**65536KB 64bit IO Format:%I64d & %I64u Submit Status Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping. Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps. To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence. The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.