Cow Contest
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16341   Accepted: 9146

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 ≤ AN; 1 ≤ BN; AB), 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

 
分析;给你n头牛,m组关系,每组关系形式为A B,代表A比B厉害
问你根据这些关系可以推出有几头牛的名次是确定的
如果一头牛和其他的所有牛的关系都确定了的话,那么该牛的名次也是确定的
关系的确定分两种情况:
比如A和C
1.直接确定 题目直接告诉你了A比C厉害
2.间接确定 题目告诉你A比B厉害,B又比C厉害,那么我们可以推出A比C厉害
所有我们必须考虑间接确定的情况,其实这种间接确定就是一个传递闭包
要求传递闭包的话我们必须确定可达矩阵
可达矩阵:G[i][j]=1的话,代表i和j之间是可以到达的(直接或者间接到达)
可达矩阵可以使用floyd算法确定,虽然Floyd是求最短路的,但是也是可以求传递闭包(可达矩阵的)
 
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 105
int G[max_v][max_v];
int n,m; void floyd()//求可达矩阵
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)
continue;
if(G[i][k]==&&G[k][j]==)
G[i][j]=;
}
}
}
}
int main()
{
int x,y;
while(~scanf("%d %d",&n,&m))
{
memset(G,,sizeof(G));
for(int i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
G[x][y]=;
}
floyd();
int sum=;
int cnt=;
for(int i=;i<=n;i++)
{
cnt=;
for(int j=;j<=n;j++)
{
//printf("%d ",G[i][j]);
if(G[i][j]||G[j][i])
cnt++;
}
// printf("\n");
if(cnt==n-)//存在某牛和其他n-1头牛的关系直接或间接确定,那么该牛名次确定
sum++;
}
printf("%d\n",sum);
}
return ;
}
/*
分析;给你n头牛,m组关系,每组关系形式为A B,代表A比B厉害
问你根据这些关系可以推出有几头牛的名次是确定的 如果一头牛和其他的所有牛的关系都确定了的话,那么该牛的名次也是确定的
关系的确定分两种情况:
比如A和C
1.直接确定 题目直接告诉你了A比C厉害
2.间接确定 题目告诉你A比B厉害,B又比C厉害,那么我们可以推出A比C厉害 所有我们必须考虑间接确定的情况,其实这种间接确定就是一个传递闭包
要求传递闭包的话我们必须确定可达矩阵
可达矩阵:G[i][j]=1的话,代表i和j之间是可以到达的(直接或者间接到达) 可达矩阵可以使用floyd算法确定,虽然Floyd是求最短路的,但是也是可以求传递闭包(可达矩阵的) */

POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))的更多相关文章

  1. ACM: POJ 3660 Cow Contest - Floyd算法

    链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Descri ...

  2. POJ 3660 Cow Contest (Floyd)

    题目链接:http://poj.org/problem?id=3660 题意是给你n头牛,给你m条关系,每条关系是a牛比b牛厉害,问可以确定多少头牛的排名. 要是a比b厉害,a到b上就建一条有向边.. ...

  3. POJ 3660 Cow Contest(求图的传递性)

    题意: 给定n头牛, 然后有m个比较, 求出有多少头牛能确定自己的排名. 分析: 假设有一头牛a, 有ki头牛强于自己, kj头牛弱于自己, ki + kj == n-1时, 那么这头牛的排名就确定了 ...

  4. POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)

    POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...

  5. POJ 3660 Cow Contest 传递闭包+Floyd

    原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  6. POJ 3660 Cow Contest

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  7. POJ 3660 Cow Contest ( 最短路松弛思想应用 && Floyd求传递闭包 )

    题意 : 给出 N 头奶牛在比赛的结果,问你最多的能根据给出结果确定其名次的奶牛头数.结果给出的形式为 A  B 代表在比赛当中 A 战胜了 B 分析 : 对于一头奶牛来说,如果我们能确定其他 N - ...

  8. POJ 3660—— Cow Contest——————【Floyd传递闭包】

    Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. POJ 3660 Cow Contest【Floyd 传递闭包】

    传送门:http://poj.org/problem?id=3660 题意:有n头牛, 给你m对关系.(a, b)表示牛a能打败牛b, 求在给出的这些关系下, 能确定多少头牛的排名. 传递闭包: 关系 ...

随机推荐

  1. (二)收集的MongoDB命令集合

    一:针对整个数据库的 1."show dbs" 命令可以显示所有数据的列表. 2. "db" 命令可以显示当前数据库对象或集合. 3."use&quo ...

  2. Windows上PostGIS(压缩版)安装

    PostGIS安装 1.软件下载 postgresql-9.6.1-1-windows-x64-binaries.zip https://www.postgresql.org/download/win ...

  3. nmon 及nmon analyser工具使用简介

    nmon及nmon analyser工具使用简介 by:授客 QQ:1033553122 下载地址 http://nmon.sourceforge.net/pmwiki.php?n=Site.Down ...

  4. Linux 多个vi、vim进程编辑同一文件时的临时文件问题

    多个vi.vim进程编辑同一文件时的临时文件问题 by:授客 QQ:1033553122   使用vi.vim编辑文件,实际是先copy一份临时文件并映射到内存里进行编辑,所以你编辑的是临时文件,不是 ...

  5. 安卓基础之Get方式发送http请求

    本文参考作者:超超boy 链接:https://www.cnblogs.com/jycboy/p/post01.html 一.在android用Get方式发送http请求,使用的是java标准类. 主 ...

  6. 类与接口(五)java多态、方法重写、隐藏

    一.Java多态性 面向对象的三大特性:封装.继承.多态. 多态的类型,分为以下两种: 编译时多态: 指的是 方法重载.编译时多态是在编译时确定调用处选择那个重载方法,所以也叫 静态多态,算不上真正的 ...

  7. python中pip

    经常在安装软件过程中用pip 安装,当时的我总觉得pip是给linux还有mac用的,所以就从没有仔细研究过pip,后来用了python才知道pip这么好用 今天总结一下pip的用法 我的电脑是win ...

  8. 多媒体指令(AVX加速数组求和)

    #include <stdio.h> #include <intrin.h> #include <iostream> #include <ctime> ...

  9. Bootstrap源码分析系列之核心CSS

    本节主要介绍核心CSS,从整体架构中的7个Less文件对应的源码分别进行分析 scaffolding.less 这个文件编译后的css文件(886~989行)其作用就像定义全局样式. //调整css盒 ...

  10. 基于MSMQ绑定的WCF服务实现总结

    一. 创建消息队列    1 1) 创建一个非事物性的私有队列    1 2)设置消息队列访问权限    2 二.创建WCF服务并绑定消息队列    4 1)创建HelloService服务    4 ...