POJ 3660Cow Contest (query set + topological sorting)

Source: Internet
Author: User

POJ 3660Cow Contest (query set + topological sorting)
Cow Contest

Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:7567   Accepted:4206

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 conducting CTED in several head-to-head rounds, each between two cows. If cowAHas a greater skill level than cowB(1 ≤AN; 1 ≤BN;A=B), Then cowAWill always beat cowB.

Farmer John is trying to rank the cows by skill level. Given a list the resultsM(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 contradic.pdf.

Input

* Line 1: Two space-separated integers:NAndM
* 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:AAndB

Output

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

Sample Input

5 54 34 23 21 22 5

Sample Output

2

Source

Question: There are n cows, numbered 1 ~ N. M relationships are given: a B, which means A is better than B. The question is how many cows can be uniquely identified (that is, the relationship between this ox and n-1 ox is uniquely identified ).

Problem-solving: If a cow can be uniquely identified, then all points must be a connected block, so it may be used to query the set for determination. The next step is the topological sorting. For more information, see the code.

 

# Include
 
  
# Include
  
   
Const int n= 105; bool mapt [N] [N], path [N] [N]; int N, in [n], father [N]; void init () {for (int I = 1; I <= n; I ++) {father [I] = I; in [I] = 0; for (int j = 1; j <= n; j ++) mapt [I] [j] = path [I] [j] = 0; path [I] [I] = 1 ;}} int findroot (int x) {if (x! = Father [x]) father [x] = findroot (father [x]); return father [x];} void setroot (int x, int y) {x = findroot (x); y = findroot (y); father [x] = y;} int tope () {int a [N], k = 0, l = 0, ans = 0; for (int I = 1; I <= n; I ++) if (in [I] = 0) a [k ++] = I; while (l
   
    
0) {init (); while (m --) {scanf ("% d", & a, & B); setroot (a, B ); if (mapt [a] [B] = 0) mapt [a] [B] = 1, in [B] ++;} int k = 0; for (int I = 1; I <= n; I ++) if (father [I] = I) k ++; if (k> 1) // It indicates that all vertices are not in a connected block and all vertices cannot be determined printf ("0 \ n"); else printf ("% d \ n ", tope ());}}
   
  
 


 

 

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.