POJ 3660 Cow Contest (floyd求联通关系)
2016-08-09 00:16
411 查看
Cow Contest
题目链接:
http://acm.hust.edu.cn/vjudge/contest/122685#problem/HDescription
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.
Input
Line 1: Two space-separated integers: N and MLines 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 determinedSample Input
5 54 3
4 2
3 2
1 2
2 5
Sample Output
2Hint
题意:
给出N个点,M个点对:每条点对 A B 意味着A点的权值大于B点.
现在要对这些点进行权值排名,求有多少个点的排名能够确定.
题解:
将样例画一遍就比较容易看出来:若某点跟其他n-1个点都联通,则这个点的排名可以确定. 否则不能.
问题就转换为了求n个点之间的联通关系.
而floyd算法正好可以求任意两点的联通关系,只需要把求最短路时的松弛操作修改一下即可.
dis[i][j] = dis[i][j] || (dis[i][k] && dis[k][j]);
代码:
#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; }
相关文章推荐
- 不同版本的Api控件显示可能不同
- 如何使用Lock接口来实现等待/通知机制
- 那么慢,那么小,那么美
- Android:Telephony
- 35 岁程序员的独家面试经历
- linux,shell,python小贴士
- Activity启动模式 Tasks和Back Stack
- UVA - 408 Uniform Generator
- HDU:1176 免费馅饼(动态规划DP+思维)
- eclipse新建自定义EL函数
- I/O复用系统调用之select()和poll()
- UVA 568 - Just the Facts
- win10下Visual Studio 2015,C++ x64编译zlib
- win10下Visual Studio 2015,C++ x64编译zlib
- leetcode题解日练--2016.8.8
- UVA - 227 Puzzle
- 噪声纹理
- 利用nginx搭建简单的web静态服务器
- JAVA二维数组
- 利用nginx搭建简单的web静态服务器