题目描述

We have N boxes, numbered 1 through N. At first, box 1 contains one red ball, and each of the other boxes contains one white ball.

Snuke will perform the following M operations, one by one. In the i-th operation, he randomly picks one ball from box xi, then he puts it into box yi.

Find the number of boxes that may contain the red ball after all operations are performed.

Constraints
2≤N≤105
1≤M≤105
1≤xi,yi≤N
xi≠yi
Just before the i-th operation is performed, box xi contains at least 1 ball.

输入

The input is given from Standard Input in the following format:

N M
x1 y1
:
xM yM

输出

Print the number of boxes that may contain the red ball after all operations are performed.

样例输入

3 2
1 2
2 3

样例输出

2

提示

Just after the first operation, box 1 is empty, box 2 contains one red ball and one white ball, and box 3 contains one white ball.

Now, consider the second operation. If Snuke picks the red ball from box 2, the red ball will go into box 3. If he picks the white ball instead, the red ball will stay in box 2. Thus, the number of boxes that may contain the red ball after all operations, is 2.

这是我的代码。我的队友刚开始并没有先标记再遍历

#include <bits/stdc++.h>

using namespace std;
struct data
{
int sum=;
int red=;
}s[];
int main()
{
ios::sync_with_stdio(false);
int n,m,i;
cin>>n>>m;
s[].sum=s[].red=;
for(i=;i<=n;i++) s[i].sum=;
int x,y,ans=;
while(m--){
cin>>x>>y;
if(s[x].red==&&s[x].sum==){
s[y].red=;
s[x].sum=s[x].red=;
s[y].sum++;
}
else if(s[x].red==&&s[x].sum!=){
s[y].sum++;
s[x].sum--;
}
else if(s[x].red==&&s[x].sum>){
s[x].sum--;
s[y].red=;
s[y].sum++;
}
}
for(i=;i<=n;i++){
if(s[i].red==) ans++;
}
cout<<ans<<endl;
return ;
}

Box and Ball的更多相关文章

  1. box2d中的物理世界

    box2d中的物理世界,即b2World类就是一个包含了各种物体(body,物理体,或者叫刚体),固定附着物(fixture,形状与物理体的绑定物)以及各种约束体(比如关节),并使其在当中完成各种交互 ...

  2. 用HTML5+原生js实现的推箱子游戏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. AtCoder Grand Contest 002

    AtCoder Grand Contest 002 A - Range Product 翻译 告诉你\(a,b\),求\(\prod_{i=a}^b i\)是正数还是负数还是零. 题解 什么鬼玩意. ...

  4. RE:从零开始的AGC被虐(到)生活(不能自理)

    RE:从零开始的AGC被虐(到)生活(不能自理) 「一直注视着你,似近似远,总是触碰不到.」 --来自风平浪静的明天 AtCoder Grand Contest 001 B: Mysterious L ...

  5. AtCoder AGC #2 Virtual Participation

    在知乎上听zzx大佬说AGC练智商...于是试了一下 A.Range Product 给$a$,$b$,求$\prod^{b}_{i=a}i$是正数,负数还是$0$ ...不写了 B.Box and ...

  6. Atcoder/Topcoder 口胡记录

    Atcoder/Topcoder 理论 AC Atcoder的❌游戏示范 兴致勃勃地打开一场 AGC 看 A 题,先 WA 一发,然后花了一年时间 Fix. 看 B 题,啥玩意?这能求? 睡觉觉. e ...

  7. A*G`C002

    AGC002 A Range Product 不会,弃疗了/kk https://agc002.contest.atcoder.jp/submissions/7908938 B Box and Bal ...

  8. 【AtCoder】AGC002

    AGC002 A - Range Product #include <bits/stdc++.h> #define fi first #define se second #define p ...

  9. AGC002[BCDEF]题解

    F是计数于是就做(kan ti jie)了= = B - Box and Ball 模拟一下 每个盒子开一个d表示有的球数 可能存在红球的打个标记 传递一下就行了 #include<cstdio ...

随机推荐

  1. FFmpeg的基本使用

    1.FFmpeg理解 (1)FFmpeg是一个视屏.音频编码工具 (2)x项目名称mpeg来源mpeg编码标准,但不局限只能使用mpeg编码标准.FF 表示fast forward (3)被广泛使用. ...

  2. django的model字段在保存的时候做预处理怎么办?

    django的model字段在保存的时候做预处理怎么办? 比如这个model: class Book(Model): publish_date = DateField() 但是在保存时,用户输入数据是 ...

  3. Mycat简介及适用场景

    官网:http://www.mycat.io/ 一.Mycat是什么 Mycat是一个开源的分布式数据库系统,是一个实现了 MySQL 协议的的 Server,前端用户可以把它看作是一个数据库代理,用 ...

  4. 模拟jenkins通过shell给ansible传入变量

    jenkins.sh #!/bin/bash name1='robin h h li' age1='11' declare -A dic dic=( [name1]="${name1}&qu ...

  5. day68-CSS-float浮动,clear清除浮动,overflow溢出

    1. float 浮动 1.1 在 CSS 中,任何元素都可以浮动. 1.2 浮动元素会生成一个块级框,而不论它本身是何种元素.内联标签设置浮动,就变成了块级标签. 1.3 关于浮动的两个特点: 浮动 ...

  6. CentOS7下MySQL8的二进制基本安装配置

    前言 基于本地Centos7.6虚拟机Mysql8的配置(亲测有效) 一.安装前的准备 1.到官网下载mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz 2.通过Xs ...

  7. leetcode 994.腐烂的橘子

    题目: 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂 ...

  8. 设x,y是概率空间(Ω,F,P)上的拟可积随机变量,证明:X=Y a.e 当且仅当 xdp = ydp 对每个A∈F成立。Q: X=Y almost surely iff ∀A∈G∫AXdP=∫AYdP

    E{XE{Y|C}}=E{YE{X|C}} 现在有没有适合大学生用的搜题软件呢?  https://www.zhihu.com/question/51935291/answer/514312093   ...

  9. 用户交互Scanner

    用户交互Scanner java.util.Scanner Scanner类可以获取用户的输入. Java 5 通过Scanner类的next()和nextLine()方法获取输入的字符串 在读取前我 ...

  10. 写一个读取Excel表格的接口

    # -*- coding: gbk -*-import xlrd class Canshu: def __init__(self,filepath): """ 创建文件对 ...