当前位置: 首页 > news >正文

Box and Ball

题目描述

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≤10 5
1≤M≤10 5
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=0;
    int red=0;
}s[100005];
int main()
{
    ios::sync_with_stdio(false);
    int n,m,i;
    cin>>n>>m;
    s[1].sum=s[1].red=1;
    for(i=2;i<=n;i++) s[i].sum=1;
    int x,y,ans=0;
    while(m--){
        cin>>x>>y;
        if(s[x].red==1&&s[x].sum==1){
            s[y].red=1;
            s[x].sum=s[x].red=0;
            s[y].sum++;
        }
        else if(s[x].red==0&&s[x].sum!=0){
            s[y].sum++;
            s[x].sum--;
        }
        else if(s[x].red==1&&s[x].sum>1){
            s[x].sum--;
            s[y].red=1;
            s[y].sum++;
        }
    }
    for(i=1;i<=n;i++){
        if(s[i].red==1) ans++;
    }
    cout<<ans<<endl;
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/smallocean/p/8715367.html

相关文章:

  • jsp中的el表达式没有解析
  • android解决AVD中文路径无法启动问题
  • TP5 中引入第三方类库
  • [BZOJ5250][九省联考2018]秘密袭击(DP)
  • 数据结构中的查找
  • maven之pom.xml
  • tomcat的安装以及环境配置
  • Vue入门干货,以及遇到的坑
  • 茶馆小人书 (AFO)
  • Exp3 免杀原理与实践 20151220刘与生
  • jmeter分布式压力测试
  • 通过Nginx反向代理实现IP分流
  • 20172314 2017-2018-2 《程序设计与数据结构》第5周学习总结
  • matlab小记(四)
  • CQOI2018 游记 再见OI,既是反思,也是祝福
  • 345-反转字符串中的元音字母
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • bootstrap创建登录注册页面
  • Node项目之评分系统(二)- 数据库设计
  • Otto开发初探——微服务依赖管理新利器
  • React-redux的原理以及使用
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • SQLServer之索引简介
  • 聊聊hikari连接池的leakDetectionThreshold
  • 让你的分享飞起来——极光推出社会化分享组件
  • 如何合理的规划jvm性能调优
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 【干货分享】dos命令大全
  • PostgreSQL之连接数修改
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • #NOIP 2014#Day.2 T3 解方程
  • #QT(一种朴素的计算器实现方法)
  • #单片机(TB6600驱动42步进电机)
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (12)Linux 常见的三种进程状态
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (三)Hyperledger Fabric 1.1安装部署-chaincode测试
  • (四)Controller接口控制器详解(三)
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .NET Core WebAPI中封装Swagger配置
  • .Net Redis的秒杀Dome和异步执行
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args
  • .NETCORE 开发登录接口MFA谷歌多因子身份验证
  • .NET开源项目介绍及资源推荐:数据持久层
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • @ConfigurationProperties注解对数据的自动封装
  • [] 与 [[]], -gt 与 > 的比较
  • [145] 二叉树的后序遍历 js
  • [Angular] 笔记 6:ngStyle