POJ 3660 Cow Contest (Warshall algorithm)

Source: Internet
Author: User
Tags rounds

POJ 3660 Cow Contest

Description

N (1≤n≤100) cows, conveniently numbered 1..N, is participating in a programming contest. As we all know, some cows code better than others. Each cow have a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds and each between. If Cow A has a greater skill level than cow B (1≤a≤n; 1≤b≤n; A≠B), then cow A would always beat Cow B.

Farmer John was trying to rank the cows by skill. Given A list the results of M (1≤m≤4,500) Two-cow rounds, determine the number of cows whose ranks can be precisely de Termined from the results. It is guaranteed that the results of the rounds would not be contradictory.

Input

    • Line 1:two space-separated integers:n and M
    • Lines 2..m+1:each line contains, space-separated integers that describe the competitors and results (the first integer , A, is the winner) of a 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
5 S
3 2
1 2
2 5

Sample Output

2

The main topic: A group of cattle race, each game two cattle to fight, and to distinguish the outcome, now ask you can determine the number of cattle combat effectiveness rankings. Problem solving idea: Warshall algorithm.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace STD;typedef Long LongllConst intN = the;intG[n][n];intN, M, T;voidFloyd () { for(intK =1; K <= N; k++) { for(inti =1; I <= N; i++) { for(intj =1; J <= N; J + +) {G[i][j] = G[i][j] | |               (G[i][k] & G[k][j]); }           }       }}intMain () { while(scanf("%d%d", &n, &m)! = EOF) {memsetG0,sizeof(G));intA, B; for(inti =0; I < m; i++) {scanf("%d%d", &a, &b); G[A][B] =1; } Floyd ();intAns =0; for(inti =1; I <= N; i++) {intCNT =0; for(intj =1; J <= N; J + +) {if(G[i][j]) cnt++;if(G[i][j]) cnt++; }if(cnt = = N-1) ans++; }printf("%d\n", ans); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.

POJ 3660 Cow Contest (Warshall algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.