题目链接:http://poj.org/problem?id=3660

Description

N ( ≤ N ≤ ) cows, conveniently numbered ..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 ( ≤ A ≤ N;  ≤ B ≤ N; A ≠ 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 ( ≤ M ≤ ,) 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 : Two space-separated integers: N and M
* Lines ..M+: 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 : A single integer representing the number of cows whose ranks can be determined
  Sample Input Sample Output Source
USACO January Silver

题目大意:有N头牛,每头牛都有个特技。有M场比赛,比赛形式是A B 意味着牛A一定能赢牛B,问在M场比赛后,有几头牛的名次是确定的(不互相矛盾)?

方法:这是最短路练习,但是以最短路的形式无法求出来,看了一下题解发现是最短路的floyd算法写的,这个算法的时间复杂度为O(n3),自己百度了一下这个算法,再求每个点的出度入度的和,如果等于n(算上自己),就能确定

 #include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define N 500
int a[N][N],G[N][N];
int main()
{
int n,m,e,v;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
a[i][j]=i==j?:;
}
for(int i=;i<m;i++)
{
scanf("%d %d",&e,&v);
a[e][v]=;
}
///floyd算法 五行代码 三层for循环 时间复杂度为O(n3)
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(a[i][j]||(a[i][k] && a[k][j]))
a[i][j]=;
}
}
}
///求每个点的出度入度之和
int ans=,sum=;
for(int i=;i<=n;i++)
{
sum=;
for(int j=;j<=n;j++)
{
if(a[i][j] || a[j][i])
sum++;
}
if(sum==n)
ans++;
}
printf("%d\n",ans);
}
return ;
}
/*
5 5
4 3
4 2
3 2
1 2
2 5
*/

(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 / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)

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

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

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

  5. POJ 3660 Cow Contest

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

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

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

  7. POJ 3660 Cow Contest(传递闭包floyed算法)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5989   Accepted: 3234 Descr ...

  8. POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))

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

  9. POJ - 3660 Cow Contest 传递闭包floyed算法

    Cow Contest POJ - 3660 :http://poj.org/problem?id=3660   参考:https://www.cnblogs.com/kuangbin/p/31408 ...

  10. poj 3660 Cow Contest(传递闭包 Floyd)

    链接:poj 3660 题意:给定n头牛,以及某些牛之间的强弱关系.按强弱排序.求能确定名次的牛的数量 思路:对于某头牛,若比它强和比它弱的牛的数量为 n-1,则他的名次能够确定 #include&l ...

随机推荐

  1. hdu_4521_小明系列问题——小明序列(LIS)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4521 题意:中文题,不解释 题解:这题就是LIS的加强版,可以用二分的nlogn来做,也可以用线段树的 ...

  2. hdu_2141_Can you find it?(二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题意:叫你找三个数,使得这三个数的值为X,如果找得到就输出YES否则输出NO,注意两个32位数相 ...

  3. LeetCode OJ 27. Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  4. 如何创建自定义ASP.NET MVC5脚手架模板?

    I'm using ASP.NET MVC5 and VS2013 I've tried to copy CodeTemplates folder from C:\Program Files (x86 ...

  5. IE11中的F12无效的问题

    最近做的一个项目中,h5中的video标签IE11在有的机器上兼容,有的机器上不兼容,很是让人头疼.将IE卸载后重装又发现最新的IE11中F12开发者工具失效.面对F12失效的问题,具体解决办法如下: ...

  6. Shell脚本,自动化发布tomcat项目【转载】

    Shell脚本,自动化发布tomcat项目脚本. 1. vko2c_auto_build_by_scp.sh 文件内容: #---------------------start------------ ...

  7. 安卓srcCompat弄死我了

    <ImageView android:layout_width="150dp" android:layout_height="120dp" app:src ...

  8. php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证及缺点

    在php中,可以使用Header函数做一些有趣的事情,用户验证就是其中一个很有意思的功能.具体用法: Header("WWW-Authenticate: Basic realm=" ...

  9. Java多态(一)

    父类: public class Parent { public String name; private String pass; public void say1(AA aa){ System.o ...

  10. VC2010编译错误

    1. cannot convert parameter 1 from 'const char [43]' to 'LPCWSTR' 我是看了这个之后解决问题的~ http://blog.163.com ...