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. 1561: (More) Multiplication

    Description Educators are always coming up with new ways to teach math to students. In 2011, an educ ...

  2. Is It A Tree? POJ - 1308

    题意: 题目给你一组单向边,当遇到输入0 0就证明这是一组边,当遇到-1 -1就要停止程序.让你判断这是不是一棵树 题解: 题目很简单,但是程序要考虑的很多 1.因为是一颗树,所以肯定不能出现环,这个 ...

  3. Distinct Substrings SPOJ - DISUBSTR 后缀数组

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  4. 过滤器,拦截器,aop区别与使用场景

    1. 什么是过滤器 过滤器,顾名思义就是起到过滤筛选作用的一种事物,只不过相较于现实生活中的过滤器,这里的过滤器过滤的对象是客户端访问的web资源,也可以理解为一种预处理手段,对资源进行拦截后,将其中 ...

  5. ELK Stack 介绍 & Logstash 日志收集

    ELK Stack 组成 Software Description Function E:Elasticsearch Java 程序 存储,查询日志 L:Logstash Java 程序 收集.过滤日 ...

  6. cin 与 getline

    cin空格截断 getline(cin,s) 换行结束 ....太愚蠢了

  7. Python Web Framework All In One

    Python Web Framework All In One Django and Flask are the top Python web frameworks so far. Django ht ...

  8. JavaScript interview Question - Create a Array with two papameters without using loop!

    JavaScript interview Question - Create a Array with two papameters without using loop! JavaScript - ...

  9. CSS & new class name

    CSS & new class name { test: /\.((s*)css|sass)$/, // test: /\.(css|scss|sass)$/, use: ExtractTex ...

  10. 阅文集团 招聘官网 bug

    阅文集团 招聘官网 bug https://join.yuewen.com/ 前端开发 zxx.jpg 张鑫旭 https://qidian.gtimg.com/yuewen/join/css/ima ...