BZOJ_1612_[Usaco2008_Jan]_Cow_Contest_奶牛的比赛_(dfs)

描述


http://www.lydsy.com/JudgeOnline/problem.php?id=1612

\(n\)头奶牛比赛,给出一些胜负情况,问可以确定多少头奶牛的排名.

 

分析


无论胜负,只要知道某一头奶牛和其他\(n-1\)头的关系就好了.

我们用dfs来求每一个奶牛赢了多少次,同时统计那些输了的.

 

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=100+5;
 5 int n,m,ect,ans;
 6 int win[maxn],los[maxn],head[maxn];
 7 bool vis[maxn];
 8 struct edge{
 9     int to,next;
10     edge(int to=0,int next=0):to(to),next(next){}
11 }g[maxn*maxn];
12 inline int read(int &x){ x=0;int k=1;char c;for(c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')k=-1;for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0';return x*=k; }
13 inline void add_edge(int u,int v){ g[++ect]=edge(v,head[u]); head[u]=ect; }
14 int dfs(int x){
15     los[x]++; vis[x]=true;
16     int s=1;
17     for(int i=head[x];i;i=g[i].next)if(!vis[g[i].to]) s+=dfs(g[i].to);
18     return s;
19 }
20 int main(){
21     read(n); read(m);
22     for(int i=1,u,v;i<=m;i++){
23         read(u); read(v);
24         add_edge(u,v);
25     }
26     for(int i=1;i<=n;i++){
27         memset(vis,false,sizeof vis);
28         los[i]--;
29         win[i]=dfs(i)-1;
30     }
31     for(int i=1;i<=n;i++)if(win[i]+los[i]==n-1) ans++;
32     printf("%d\n",ans);
33     return 0;
34 }
View Code

 

转载于:https://www.cnblogs.com/Sunnie69/p/5598906.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值