IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> H - Cow Contest(Floyd传递闭包) -> 正文阅读

[C++知识库]H - Cow Contest(Floyd传递闭包)

Cow Contest
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13189 Accepted: 7337
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.

Input

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
Output

Line 1: A single integer representing the number of cows whose ranks can be determined
 
Sample Input

5 5
4 3
4 2
3 2
1 2
2 5
Sample Output

2
Source

USACO 2008 January Silver

大致题意: 有n头牛, 以及m组胜负关系, 问能通过这m组关系判断出多少头牛的排名

思路: 拿到这道题时, 我想的是对n进行一个拓补排序, 可排序后就不知到该怎么做来进一步判断排名, 然后又想通过判断一个点度数来判断, 发现当1个点与其他n-1个点的关系确定时, 就能确定这个点的具体排名,不知具体怎么实现, 于是去网上搜题解发现有个叫Floyd传递闭包的东西, 这题就是简单例题.

知识点 : Floyd传递闭包:

这题是利用Floyd的传递闭包, 来确定一个点与其他所有点是否有关

代码如下:

#include <iostream>
#include <cstring>
#include <string>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <iomanip>
using namespace std;

stringstream ss;
typedef pair<int, int> PII;
typedef long long ll;
map<string,string> mp;
const int N = 110;
const int INF = 0x3f3f3f3f;

int f,n,m,k;
bool st[N];
int g[N][N],d[N],cnt[N];

void floyd()
{
    for(int k = 1; k<=n; k++)
    {
        for(int i = 1; i<=n; i++)
        {
            for(int j = 1; j<=n; j++)
            {
                if(g[i][k] && g[k][j]) g[i][j] = 1;
            }
        }
    }
}
int main()
{
    cin>>n>>m;
    while(m -- )
    {
        int a,b;
        cin>>a>>b;
        g[a][b] = 1;
    }
    floyd();
    int res = 0;
    for(int i = 1; i<=n; i++)
    {
        int cnt = 0;
        for(int j = 1; j<=n; j++)
        {
            if(i != j)
            {
                if(g[i][j] || g[j][i]) cnt++;
            }
        }
        if(cnt == n-1) res++;
    }
    cout<<res;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-28 11:37:29  更:2022-04-28 11:38:00 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 22:53:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码