HDU 1704 rank

Source: Internet
Author: User
Ranktime limit: 1000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 1704
64-bit integer Io format: % i64d Java class name: Main there are n acmers in HDU team.
Zjpcpc sunny Cup 2007 is coming, and lcy want to select some excellent acmers to attend the contest. there have been m matches since the last few days (no two acmers will meet each other at two matches, means between two acmers There will be at most one match ). lcy also asks "who is the winner between A and B? "But sometimes you can't answer lcy's query, for example, there are 3 people, named A, B, C. and 1 match was held between A and B, in the match a is the winner, then if lcy asks "who is the winner between A and B ", of course you can answer "A", but if lcy ask "who is the winner between A and C", you can't tell him the answer.
As lcy's assistant, you want to know how many queries at most you can't tell lcy (ask a B, and ask B A is the same; and lcy won't ask the same question twice ). inputthe input contains multiple test cases.
The first line has one integer, represent the number of test cases.
Each case first contains two integers n and M (n, m <= 500), n is the number of acmers in HDU team, and m is the number of matchs have been held. the following M lines, each line means a match and it contains two integers A and B, means a wins the match between A and B. and we define that if a wins B, and B wins C, then a WINS C. outputfor each test case, output a integer which represent the max possible number of queries that you can't tell lcy. sample Input
33 31 21 32 33 21 22 34 21 23 4
Sample output
004
HintIn the case3, if lcy ask (1 3 or 3 1) (1 4 or 4 1) (2 3 or 3 2) (2 4 or 4 2 ), then you can't tell him who is the winner. source2007 provincial training team exercises (1) Problem Solving: the number of relationships cannot be determined. The closure of a non-phase graph.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 510;18 int n,m;19 bool mp[maxn][maxn];20 void Floyd(){21     for(int k = 1; k <= n; k++){22         for(int i = 1; i <= n; i++){23             for(int j = 1; mp[i][k]&&j <= n; j++)24                 mp[i][j] |= mp[i][k]&mp[k][j];25         }26     }27 }28 int main() {29     int t,u,v,ans;30     scanf("%d",&t);31     while(t--){32         scanf("%d %d",&n,&m);33         memset(mp,false,sizeof(mp));34         for(int i = 0; i < m; i++){35             scanf("%d %d",&u,&v);36             mp[u][v] = true;37         }38         Floyd();39         ans = 0;40         for(int i = 1; i <= n; i++){41             for(int j = i+1; j <= n; j++)42                 if(mp[i][j] == 0 && mp[j][i] == 0) ans++;43         }44         printf("%d\n",ans);45     }46     return 0;47 }
View code

 

HDU 1704 rank

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.