zoukankan      html  css  js  c++  java
  • POJ 3660 传递闭包问题

    Cow Contest

    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 ≤ AN; 1 ≤ BN; AB), 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

    题意概述

      有n头牛比赛,给出m个胜负关系,最后问你一共有多少头牛的排名被确定了。特别的,其中如果a战胜b,b战胜c,则也可以说a战胜c,就是说胜负是可以传递的。求能确定排名的牛的数目。

    题解

      一个牛的排名被确定了,当且仅当它被战胜的次数和它战胜别人的次数之和为n-1。所以这题就转化成了求传递闭包问题,很H2O的啊QAQ

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 const int M = 105;
     7 int map[M][M];
     8 int n, m;
     9 int main()
    10 {
    11     cin>>n>>m;
    12     int a, b;
    13     for(int i=0; i<m; ++i)
    14     {
    15         cin>>a>>b;
    16         map[a][b] = 1;
    17     }
    18     for(int k=1; k<=n; ++k)
    19         for(int i=1; i<=n; ++i)
    20             if(map[i][k] == 1)
    21                 for(int j=1; j<=n; ++j)
    22                     if(map[k][j] == 1)
    23                         map[i][j] = 1;
    24     int cnt, ans = 0;
    25     for(int i=1; i<=n; ++i)
    26     {
    27         cnt = 0;
    28         for(int j=1; j<=n; j++)
    29             if(map[i][j] == 1 || map[j][i] == 1) cnt++;
    30         if(cnt == (n-1)) ans++;
    31     }
    32     cout<<ans<<endl;
    33     return 0;
    34 } 
  • 相关阅读:
    think in java
    TASLock TTASLock
    多线程
    jenkins unable to delete file
    ubuntu sun-jdk
    py2exe
    memcached安装
    redis安装
    ubuntu安装ssh
    mysql远程访问
  • 原文地址:https://www.cnblogs.com/Droyal/p/7168289.html
Copyright © 2011-2022 走看看