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
Inputcopy | Outputcopy |
---|---|
5 5 4 3 4 2 3 2 1 2 2 5 | 2 |
N(1≤N≤100)头奶牛,方便地编号为1...N,正在参加一个编程比赛。我们都知道,有些奶牛的代码比其他奶牛好。每头牛都有一定的恒定技能等级,在竞争对手中是独一无二的。
比赛分几轮正面交锋进行,每轮在两头牛之间进行。如果奶牛A的技能等级比奶牛B高(1≤A≤N;1≤B≤N;A≠B),那么奶牛A将永远击败奶牛B。
农场主约翰正试图按技能水平对奶牛进行排名。给出一个M(1≤M≤4,500)个两头奶牛回合的结果列表,确定可以从结果中精确确定其等级的奶牛数量。保证各轮比赛的结果不会相互矛盾。
输入
*第1行。两个隔开空间的整数。N和M
* 第2...M+1行:每行包含两个空间分隔的整数,描述单轮比赛的竞争者和结果(第一个整数,A,是赢家)。A和B
输出
*第1行。一个单一的整数,代表可以确定其等级的奶牛数量
样本
输入副本 输出副本
5 5
4 3
4 2
3 2
1 2
2 5
2
题目有点抽象其实只要当一头牛被打败和能打败的都知道之后,那么打败这头牛的牛一定也能打败被这头牛打败的,这样层层递推就能推算出哪个牛是king哪个牛一直挨打(等级不会超过n)
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<map>
#include<cstring>
#include<queue>
#include<set>
#include<stdlib.h>
using namespace std;
typedef long long ll;
//#define dbug cout<<"hear!"<<erndl;
const int N = 3e5 + 10, INF = 0x3f3f3f3f;
ll n, m,k,a;
ll h[N], w[N], e[N], ne[N], idx;//h数组存储的永远都是a的第一条边的编号;ne数组存储的是编号为i边的下一条编号;邻接表类似于链表;
ll arr[200][200];
ll dis[200][200];
bool book[N];
ll brr[N];
void add(ll a, ll b, ll c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int main()
{
cin >> n >> m;
memset(arr, 0, sizeof arr);
while(m--)
{
int a, b;
cin >> a >> b;
arr[a][b] = 1;
}
for (int k = 1;k <= n;k++)
{
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
if (arr[i][k] == 1 && arr[k][j] == 1)
{
arr[i][j] = 1;
}
}
}
}
int cnt = 0;
for (int i = 1;i <= n;i++)
{
int flag = 0;
for (int j = 1;j <= n;j++)
{
if (arr[i][j] == 1 || arr[j][i] == 1)
{
flag++;
}
}
if (flag == n - 1)
{
cnt++;
}
}
cout << cnt << endl;
}