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对牛比赛结果,问有多少头牛的排名可以被确定
思路:这道题有两种做法,一种是flody,一种是并查集+拓扑序,核心思想都是一样的:一头牛如果和其他牛的关系都确定了,那排名就确定了
这是我flody的代码
#include <iostream>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<stdio.h>
#include<queue>
#include<string>
#include<cstring>
#define maxn 1005
#define pi acos(-1.0)
#define esp 1e-7
typedef long long ll;
using namespace std;
#include<vector>
vector<int>mp[maxn];
int a[maxn],in[maxn],num=0,vis[maxn];
char c[maxn],ans[maxn];
void init()
{
for(int i=0;i<maxn;i++) mp[i].clear();
memset(in,0,sizeof(in));num=0;
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
}
void dfs(int n)
{
//printf("1\n");
/*for(int i=1;ans[i]!='\0';i++)
{
printf("%c",ans[i]);
}
printf("\n");
*/if(n==num)
{
printf("%s\n",ans+1);
return;
}
for(int i=1;i<=num;i++)
{
//printf("in[%c]=%d vis[%c]=%d\n",a[i]+'a',in[a[i]],a[i]+'a',vis[a[i]]);
if(in[a[i]]==0&&vis[a[i]]==0)
{
//printf("n=%d a[%d]=%c vis[%c]=%d\n",n,i,a[i]+'a',a[i]+'a',vis[a[i]]);
vis[a[i]]=1;
ans[n+1]=a[i]+'a';
for(int j=0;j<(int)mp[a[i]].size();j++)
{
//printf("a[i]=%c v=%c\n",a[i]+'a',mp[a[i]][j]+'a');
in[mp[a[i]][j]]--;
}
dfs(n+1);
ans[n+1]='\0';
vis[a[i]]=0;
for(int j=0;j<(int)mp[a[i]].size();j++) in[mp[a[i]][j]]++;
}
}
}
int main()
{
while(gets(c+1))
{
init();
for(int i=1;c[i]!='\0';i++)
{
if(c[i]==' ')continue;
a[++num]=c[i]-'a';
}
sort(a+1,a+1+num);
/*for(int i=1;i<=num;i++)
{
printf("%c",a[i]+'a');
}
printf("\n");
*/gets(c+1);
int u=-1,v=-1;
for(int i=1;c[i]!='\0';i++)
{
if(c[i]==' ')continue;
if(u==-1)
{
u=c[i]-'a';
}
else
{
v=c[i]-'a';
//printf("u=%c v=%c\n",u+'a',v+'a');
mp[u].push_back(v);
in[v]++;
u=v=-1;
}
}
/*for(int i=1;i<=num;i++)
{
printf("u=%c ",a[i]+'a');
for(int j=0;j<(int)mp[a[i]].size();j++)
{
printf("v=%c\n",mp[a[i]][j])
}
}*/
dfs(0);
printf("\n");
}
}