4269. 【NOIP2015模拟10.27】挑竹签 
(File IO): input:mikado.in output:mikado.out

Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limits  

Goto ProblemSet

Description

挑竹签——小时候的游戏
夏夜,早苗和诹访子在月光下玩起了挑竹签这一经典的游戏。
挑竹签,就是在桌上摆上一把竹签,每次从最上层挑走一根竹签。如果动了其他的竹签,就要换对手来挑。在所有的竹签都被挑走之后,谁挑走的竹签总数多,谁就胜了。
身为神明的诹访子自然会让早苗先手。为了获胜,早苗现在的问题是,在诹访子出手之前最多能挑走多少竹签呢?
为了简化问题,我们假设当且仅当挑最上层的竹签不会动到其他竹签。
 

Input

输入文件mikado.in。
第一行输入两个整数n,m, 表示竹签的根数和竹签之间相压关系数。
第二行到m+1 行每行两个整数u,v,表示第u 根竹签压住了第v 根竹签。

Output

输出文件mikado.out。
一共一行,一个整数sum,表示最多能拿走sum 根竹签。
 

Sample Input

6 6
1 2
2 3
3 1
4 3
4 5
6 5

Sample Output

3
样例解释:
一共有6 根竹签,其中1 压住2,2 压住3,3 压住1,4 压住3 和5,6 压住5。最优方案中,我们可以依次挑走4、6、5 三根竹签。而剩下的三根相互压住,都无法挑走。所以最多能挑走3 根竹签。
 

Data Constraint

对于20% 的数据,有1<= n,m<= 20。
对于40% 的数据,有1 <= n,m <= 1 000。
对于100% 的数据,有1 <= n,m <= 1 000 000。
 
做法:不难发现,对于a 压住 b 这种情况,可以由 a 向 b 连一条边,然后拓扑排序找最长链就好了。
 
代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define N 1000007
using namespace std;
struct edge
{
int to, next;
}e[N * ];
int rd[N], n, m, ls[N], tot, list[N];
bool v[N]; int read()
{
int s = ;
char c = getchar();
while (c < '' || c > '') c = getchar();
while (c >= '' && c <= '') s = s * + c - '', c = getchar();
return s;
} int main()
{
freopen("mikado.in", "r", stdin);
freopen("mikado.out", "w", stdout);
n = read(), m = read();
for (int i = ; i <= m; i++)
{
int x, y;
x = read(), y = read();
rd[y]++;
e[++tot].to = y;
e[tot].next = ls[x];
ls[x] = tot;
}
int head = , tail = ;
for (int i = ; i <= n; i++)
if (rd[i] == ) list[++tail] = i, v[i] = ;
while (head <= tail)
{
head++;
for (int i = ls[list[head]]; i; i = e[i].next)
{
rd[e[i].to]--;
if (rd[e[i].to] == && !v[e[i].to])
{
v[e[i].to] = ;
list[++tail] = e[i].to;
}
}
}
printf("%d", tail);
}

最新文章

  1. With(ReadPast)就不会被阻塞吗?
  2. LR测试登陆后进行的操作时 绕过登录
  3. Ubuntu 安装字体
  4. CERT_KEY_CONTEXT_PROP_ID,CERT_KEY_PROV_INFO_PROP_ID,CERT_KEY_PROV_HANDLE_PROP_ID和CERT_KEY_SPEC_PROP_ID
  5. c++学习-链表
  6. backup site collection
  7. Maven浅析-3 Ant Vs Maven
  8. mysql、添加和删除用户、添加权限
  9. Sql Server数据库快照初探
  10. 介绍 - OC中的代理模式
  11. 新发现的Cyberduck(映射网盘)和zsuncloud(硬件产品很新潮),群辉nas的确好用(购买链接)
  12. 读书时间《JavaScript高级程序设计》一:基础篇
  13. asp:cookies的属性
  14. flask-mail发送QQ邮件代码示例(亲测可行)
  15. python常用模块(2)
  16. java进程/线程;堆和栈;多线程
  17. css21规范学习
  18. php操作redis数据库方法总结
  19. angularjs 下滑线滑动
  20. AWS DevOps – 配合Jenkins和CodeDeploy实现代码自动化部署

热门文章

  1. C# 读写XML文件的方法
  2. 【密码学】SSL双向认证以及证书的制作和使用
  3. c++11 多线程入门教程(一)
  4. android 开发DatePickerDialog/TimePickerDialog对话框的实现
  5. Object与封装、继承和多态性
  6. js之BOM和DOM
  7. strlen()与mb_strlen()的作用分别是什么
  8. Vue.js(2.x)之Class 与 Style 绑定
  9. HTTPS与SSL(二)
  10. uvm_sqr_ifs——TLM1事务级建模方法(四)