Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9308 | Accepted: 5240 |
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 conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), 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
Source
题意:告诉你总共有n头牛和m组战斗力强弱的关系,并且当两头牛的战斗力关系确定以后他们的胜负一定确定,问给定这些关系以后让他们打架,有多少头牛的排名是我们可以算出来的。
弄一个邻接矩阵,一组数据相当于一条边,然后我们用floyd求一下传递闭包,最后扫一遍有几头牛能打败他的和他能打败的是全部即可。
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define foreach(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define fur(i,a,b) for(int i=(a);i<=(b);i++)
#define furr(i,a,b) for(int i=(a);i>=(b);i--)
#define cl(a) memset((a),0,sizeof(a))
#define sc(x) scanf("%d",&x)
#define dbg(x) cout << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
const int mod=1000000007;
const double pi=acos(-1);
inline void gn(long long&x){
int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
int gcd(int a,int b){return a? gcd(b%a,a):b;}
ll powmod(ll a,ll x,ll mod){ll t=1ll;while(x){if(x&1)t=t*a%mod;a=a*a%mod;x>>=1;}return t;}
// (づ°ω°)づe★
const int maxn = 105;
int mp[maxn][maxn];
int n,m,ans;
void init(){
cl(mp);
fur(i,0,n)mp[i][i] = 1;
}
void froyd(){
fur(k,1,n)
fur(i,1,n)
fur(j,1,n)
if(mp[i][k]&&mp[k][j])
mp[i][j] = 1;
}
void check(){
fur(i,1,n){
int tot = 0;
fur(j,1,n)
if(mp[i][j]|mp[j][i])
tot++;
if(tot == n)
ans++;
}
}
int main(){
//freopen("E:\\data.in","r",stdin);
//freopen("E:\\data.out","w",stdout);
while(scanf("%d %d",&n,&m) != EOF){
int a,b;
init();
fur(i,1,m){
gn(a);gn(b);
mp[a][b] = 1;
}
froyd();
ans = 0;
check();
printf("%d\n",ans);
}
return 0;
}