Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2569 | Accepted: 1203 |
Description
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to these rates from the fastest milk producer to the slowest.
FJ has already compared the milk output rate for M (1 ≤ M ≤ 10,000) pairs of cows. He wants to make a list of C additional pairs of cows such that, if he now compares those C pairs, he will definitely be able to deduce the correct ordering of all N cows. Please help him determine the minimum value of C for which such a list is possible.
Input
Lines 2.. M+1: Two space-separated integers, respectively: X and Y. Both X and Y are in the range 1... N and describe a comparison where cow X was ranked higher than cow Y.
Output
Sample Input
5 5 2 1 1 5 2 3 1 4 3 4
Sample Output
3
Hint
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
using namespace std;
vector<int> from[1001],to[1001];
int mp[1001][1001];
int n,m;
int main()
{
int ans=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x][y]=1;
from[x].push_back(y);
to[y].push_back(x);
}
for(int k=1;k<=n;k++)
{
for(int i=0;i<to[k].size();i++)
{
for(int j=0;j<from[k].size();j++)
{
int x,y;
x=to[k][i],y=from[k][j];
if(!mp[x][y])
{
mp[x][y]=1;
from[x].push_back(y);
to[y].push_back(x);
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i!=j&&mp[i][j])
ans++;
}
}
printf("%d\n",n*(n-1)/2-ans);
return 0;
}