POJ 3660 Cow Contest (floyd求联通关系)
Cow Contest
题目链接:
http://acm.hust.edu.cn/vjudge/contest/122685#problem/H
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 ≤ 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 (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.
</big>
##Input
<big>
* 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
</big>
##Output
<big>
* Line 1: A single integer representing the number of cows whose ranks can be determined
</big>
##Sample Input
<big>
5 5
4 3
4 2
3 2
1 2
2 5
</big>
##Sample Output
<big>
2
</big>
##Hint
<big>
</big>
<br/>
##题意:
<big>
给出N个点,M个点对:
每条点对 A B 意味着A点的权值大于B点.
现在要对这些点进行权值排名,求有多少个点的排名能够确定.
</big>
<br/>
##题解:
<big>
将样例画一遍就比较容易看出来:
若某点跟其他n-1个点都联通,则这个点的排名可以确定. 否则不能.
问题就转换为了求n个点之间的联通关系.
而floyd算法正好可以求任意两点的联通关系,只需要把求最短路时的松弛操作修改一下即可.
dis[i][j] = dis[i][j] || (dis[i][k] && dis[k][j]);
</big>
<br/>
##代码:
``` cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 110
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n, m;
bool dis[maxn][maxn];
void floyd() {
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dis[i][j] = dis[i][j] || (dis[i][k] && dis[k][j]);
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d %d", &n,&m) != EOF)
{
memset(dis, 0, sizeof(dis));
for(int i=1; i<=n; i++) dis[i][i] = 1;
for(int i=1; i<=m; i++) {
int u,v; scanf("%d %d", &u,&v);
dis[u][v] = 1;
}
floyd();
int ans = 0;
for(int i=1; i<=n; i++) {
int cnt1=0, cnt2=0;
for(int j=1; j<=n; j++) {
if(i == j) continue;
if(dis[i][j]) cnt1++;
if(dis[j][i]) cnt2++;
}
if(cnt1+cnt2 == n-1) ans++;
}
printf("%d\n", ans);
}
return 0;
}
POJ 3660 Cow Contest (floyd求联通关系)的更多相关文章
- ACM: POJ 3660 Cow Contest - Floyd算法
链接 Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Descri ...
- POJ 3660 Cow Contest (Floyd)
题目链接:http://poj.org/problem?id=3660 题意是给你n头牛,给你m条关系,每条关系是a牛比b牛厉害,问可以确定多少头牛的排名. 要是a比b厉害,a到b上就建一条有向边.. ...
- POJ 3660 Cow Contest(求图的传递性)
题意: 给定n头牛, 然后有m个比较, 求出有多少头牛能确定自己的排名. 分析: 假设有一头牛a, 有ki头牛强于自己, kj头牛弱于自己, ki + kj == n-1时, 那么这头牛的排名就确定了 ...
- 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 ...
- POJ 3660 Cow Contest 传递闭包+Floyd
原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ 3660 Cow Contest
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 9146 Desc ...
- POJ 3660—— Cow Contest——————【Floyd传递闭包】
Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3660 Cow Contest【Floyd 传递闭包】
传送门:http://poj.org/problem?id=3660 题意:有n头牛, 给你m对关系.(a, b)表示牛a能打败牛b, 求在给出的这些关系下, 能确定多少头牛的排名. 传递闭包: 关系 ...
随机推荐
- Windows 7下配置JDK环境变量
安装jdk1.8版本(下载链接:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) ...
- 面试题_ Java EE 相关的面试题
为了做 Java EE 的朋友,这里列出了一些 web 开发的特定问题,你们可以用来准备 JEE 部分的面试: 10 大 Spring 框架面试题及答案(参见)10 个非常好的 XML 面试问题(Ja ...
- bzoj2436
不难发现两边的活动是交替进行的,我们可以dp 先对时间离散化,设f[i,j]到时间i一个会场选j个活动,另一个会场最多有多少活动,那么f[i,j]=max(f[k,j]+s[k,i],f[k,j-s[ ...
- max-height,min-height在IE下不支持的解决方法
max-height,min-height在IE下不支持的解决方法 max-width:160px; max-height:160px; _width:expression(this.width &g ...
- java分层架构概念
转自:http://www.cnblogs.com/bdqnbenet/p/4924778.html service是业务层 DAO (Data Access Object) 数据访问 1.JAVA中 ...
- ZigZag Conversion1
问题描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- Java [Leetcode 198]House Robber
题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- LeetCode: 3SumClosest
Title : Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- 门户网站架构Nginx+Apache+MySQL+PHP+Memcached+Squid
服务器的大用户量的承载方案 一.前言二.编译安装三. 安装MySQL.memcache四. 安装Apache.PHP.eAccelerator.php-memcache五. 安装Squid六.后记 一 ...
- 仿it快播顶部button点击背景滑动切换的效果
最近在it快播中看见它顶部的几个button可以点击后 背景会滑动到相应的button后面 就得很好看 就想办法实现了那效果 思路 大概就是通过view的叠加 把3个button通过RelativeL ...