Cow Contest

POJ-3660

1.本题考察的是最短路,用的算法是Floyd算法

2.如果一个结点和剩余的n-1个结点都有关系,那么可以确定其排名

3.需要注意的是,判断是否有关系时,反向关系也要考虑

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int map[101][101];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
int a,b;
memset(map,0,sizeof(map));
for(int i=0;i<m;i++){
cin>>a>>b;
map[a][b]=1;
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!map[i][j])
if(map[i][k]&&map[k][j]){
map[i][j]=1;
}
}
}
}
int cnt=0;
for(int i=1;i<=n;i++){
int ans=0;
for(int j=1;j<=n;j++){
if(i!=j){
if(map[i][j]||map[j][i])
ans++;
}
}
if(ans==n-1)
cnt++;
}
cout<<cnt<<endl;
//system("pause");
return 0;
}

java

package POJ;
import java.util.*;
public class POJ_3660 {
static int n,m;
static int [][]map;//用于判断i,j两个顶点之间是否存在联系,如果一个点和所有其他的点都有联系则说明这个点的排名确定了
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
m=cin.nextInt();
map=new int [n+1][n+1];
for(int i=0;i<n+1;i++)
for(int j=0;j<n+1;j++)
map[i][j]=0;
for(int i=0;i<m;i++) {
int a,b;
a=cin.nextInt();
b=cin.nextInt();
map[a][b]=1;
}
for(int k=1;k<=n;k++) {
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
if(map[i][j]==0) {
if(map[i][k]>0&&map[k][j]>0)
map[i][j]=1;
}
}
}
}
int cnt=0;
for(int i=1;i<=n;i++) {
int ans=0;
for(int j=1;j<=n;j++) {
if(i!=j)
if(map[i][j]>0||map[j][i]>0)
ans++;
}
if(ans==n-1)
cnt++;
}
System.out.println(cnt);
} }

POJ-3660(Floyd算法)的更多相关文章

  1. 图论---POJ 3660 floyd 算法(模板题)

    是一道floyd变形的题目.题目让确定有几个人的位置是确定的,如果一个点有x个点能到达此点,从该点出发能到达y个点,若x+y=n-1,则该点的位置是确定的.用floyd算发出每两个点之间的距离,最后统 ...

  2. POJ 3660 Floyd传递闭包

    题意:牛有强弱,给出一些牛的强弱的胜负关系,问可以确定几头牛的排名. 思路: Floyd传递闭包 // by SiriusRen #include <bitset> #include &l ...

  3. poj 2240 floyd算法

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17349   Accepted: 7304 Descri ...

  4. Cow Contest POJ - 3660 floyd传递闭包

    #include<iostream> #include<cstring> using namespace std; ,INF=0x3f3f3f3f; int f[N][N]; ...

  5. (poj 3660) Cow Contest (floyd算法+传递闭包)

    题目链接:http://poj.org/problem?id=3660 Description N ( ≤ N ≤ ) cows, conveniently numbered ..N, are par ...

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

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

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

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

  8. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

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

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

  10. POJ 3660 Cow Contest (floyd求联通关系)

    Cow Contest 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/H Description N (1 ≤ N ≤ 100) ...

随机推荐

  1. Codeforces Round #649 (Div. 2) A. XXXXX

    题目链接:https://codeforces.com/contest/1364/problem/A 题意 找出大小为 $n$ 的数组 $a$ 的最长连续子数组,其元素和不被 $x$ 整除. 题解 如 ...

  2. B. Modular Equations

    Last week, Hamed learned about a new type of equations in his math class called Modular Equations. L ...

  3. Codeforces Round #681 (Div. 1, based on VK Cup 2019-2020 - Final) B. Identify the Operations (模拟,双向链表)

    题意:给你一组不重复的序列\(a\),每次可以选择一个数删除它左边或右边的一个数,并将选择的数append到数组\(b\)中,现在给你数组\(b\),问有多少种方案数得到\(b\). 题解:我们可以记 ...

  4. dict与set -- Python

    dict(字典):用空间换取时间,占据空间大,但查询速度快,键值对(key:value),key唯一 d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} 由于一个k ...

  5. kafka——集群安裝部署(自带zookeeper)

    kafka系列文章 第一章 linux单机安装kafka 第二章 kafka--集群安裝部署(自带zookeeper) 一.kafka简介 kafka官网:http://kafka.apache.or ...

  6. Python——处理CSV、PDF文件

    一.CSV文件处理 (1)读取 import csv filename = "E:/GitHub/Python-Learning/LIVE_PYTHON/2018-06-05/学位英语成绩合 ...

  7. 北京网络赛G BOXES 大模拟+BFS

    题目描述 Description There is a strange storehouse in PKU. In this storehouse there are n slots for boxe ...

  8. matplotlib 图标显示中文

    matplotlib 显示中文 Method_1: # 添上: plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcPara ...

  9. ASP.Net MVP Framework had been dead !

    ASP.Net MVP Framework Project Description A project to get you started with creating and designing w ...

  10. auto switch HTTP protocol Chrome Extension

    auto switch HTTP protocol Chrome Extension HTTPS auto switch to HTTP VPN https://chrome.google.com/w ...