Cow Contest
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11129   Accepted: 6183

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 ≤ AN; 1 ≤ BN; AB), 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

【分析】给你一些关系,例如输入u,v,表示u排在v的前面,然后问你有哪些人的排名是可以确定的。
 首先可以看出这些关系最终组成了一个有向无环图,也就是说输入u,v,u-->v;我们对于每个人查找确定排在他前面的人的个数和确定排在他后面的人的个数,
如果加起来连同他自己==n,那么说明这个人的排名确定了,那么怎么确定有多少人一定排在他前面呢?我们建两个图,一个顺序,一个逆序,然后分开找就行了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 10000000
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int in[N],vis[N];
int n,m,k;
vector<int>vec[N],edg[N];
int dfs1(int x)
{
vis[x]=;
int ans=;
for(int i=;i<vec[x].size();i++){
int v=vec[x][i];
if(!vis[v])ans+=dfs1(v);
}
return ans;
}
int dfs2(int x)
{
int ans=;
vis[x]=;
for(int i=;i<edg[x].size();i++){
int v=edg[x][i];
if(!vis[v])ans+=dfs2(v);
}
return ans;
}
int main()
{
int u,v,ans=;;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&u,&v);
vec[u].push_back(v);
edg[v].push_back(u);
}
for(int i=;i<=n;i++){
memset(vis,,sizeof vis);
int ret1=dfs1(i);
memset(vis,,sizeof vis);
int ret2=dfs2(i);
//printf("!!%d %d\n",ret1,ret2);
if(ret1+ret2==n+)ans++;
}
printf("%d\n",ans);
return ;
}

POJ 3660 Cow Contest (dfs)的更多相关文章

  1. POJ 3660 Cow Contest (Floyd)

    http://poj.org/problem?id=3660 题目大意:n头牛两两比赛经过m场比赛后能判断名次的有几头可转 化为路径问题,用Floyd将能够到达的路径标记为1,如果一个点能 够到达剩余 ...

  2. poj 3660 Cow Contest (传递闭包)

    /* floyd 传递闭包 开始Floyd 之后统计每个点能到的或能到这个点的 也就是他能和几个人确定胜负关系 第一批要有n-1个 然后每次减掉上一批的人数 麻烦的很 复杂度上天了.... 正难则反 ...

  3. POJ - 3660 Cow Contest(传递闭包)

    题意: n个点,m条边. 若A 到 B的边存在,则证明 A 的排名一定在 B 前. 最后求所有点中,排名可以确定的点的个数. n <= 100, m <= 4500 刚开始还在想是不是拓扑 ...

  4. POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 9146 Desc ...

  5. POJ 3660 Cow Contest(传递闭包floyed算法)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5989   Accepted: 3234 Descr ...

  6. poj 3660 Cow Contest(传递闭包 Floyd)

    链接:poj 3660 题意:给定n头牛,以及某些牛之间的强弱关系.按强弱排序.求能确定名次的牛的数量 思路:对于某头牛,若比它强和比它弱的牛的数量为 n-1,则他的名次能够确定 #include&l ...

  7. POJ 3660 Cow Contest (floyd求联通关系)

    Cow Contest 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/H Description N (1 ≤ N ≤ 100) ...

  8. POJ 3660 cow contest (Folyed 求传递闭包)

    N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we ...

  9. POJ 3660 Cow Contest(floyed运用)

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

随机推荐

  1. 启动hbase输出ignoring option PermSize=128m; support was removed in 8.0告警信息

    ./start-hbase.sh starting master, logging to /home/hadoop/hbase-1.2.4/bin/../logs/hbase-hadoop-maste ...

  2. Activity与Service的回收

    Android开发中,一个Application,运行在一个进程中.这个Application的各种组件(四种组件),通常是运行在同一个进程中的.但是,并不是绝对的.由于某种需求,比如,你可以设置Ap ...

  3. Remmarguts' Date(POJ2449+最短路+A*算法)

    题目链接:http://poj.org/problem?id=2449 题目: 题意:求有向图两点间的k短路. 思路:最短路+A*算法 代码实现如下: #include <set> #in ...

  4. 大聊Python----多线程

    Python里的多线程是假的多线程,不管有多少核,同一时间只能在一个核中进行操作!利用Python的多线程,只是利用CPU上下文切换的优势,看上去像是并发,其实只是个单线程,所以说他是假的单线程. 那 ...

  5. eclipse+EGIT+GitHub

    下载EGIT:http://wiki.eclipse.org/EGit/FAQ#Where_can_I_find_older_releases_of_EGit.3F 1.下载eclipse版本对应的E ...

  6. vim 以16进制进行文件编辑

    用 vim中二进制文件的编辑是先通过外部程序xxd来把文件dump成其二进制的文本形式,然后就可以按通常的编辑方式对文件进行编辑,编辑完成后再用xxd 转化为原来的形式即可. 可分如下几步进行: (1 ...

  7. CTF两个经典的文件包含案例

    案例一URL:http://120.24.86.145:8003/代码 <?php include "waf.php"; include "flag.php&quo ...

  8. Python模块学习 - ConfigParser

    配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...

  9. 网络知识===wireshark抓包,三次握手分析

    TCP需要三次握手建立连接: 网上的三次握手讲解的太复杂抽象,尝试着使用wireshark抓包分析,得到如下数据: 整个过程分析如下: step1 client给server发送:[SYN] Seq ...

  10. vsftp 服务的启动与问题

    一般系统用户是可以直接登入的如果不可以可能是selinux的原因 执行一下: 更改selinux的配置文件将其设为disable,可我不想重启服务器,有以下解决办法:执行命令:setenforce 0 ...