• Box and Ball


    5582: Box and Ball

    时间限制: 2 Sec  内存限制: 256 MB
    提交: 276  解决: 93
    [提交][状态][讨论版][命题人:admin]

    题目描述

    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 <iostream>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    struct box
    {
        int num;
        int flag;
    }A[100005];
    
    int main()
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            if(i==1)
            {
                A[i].flag = 1;
                A[i].num = 1;
            }
            else
            {
                A[i].flag = 0;
                A[i].num = 1;
            }
        }
        int x,y;
        for(int i=0;i<m;i++)
        {
            cin>>x>>y;
            if(A[x].flag==1)
            {
                if(A[x].num==1)
                {
                    A[y].flag = 1;
                    A[y].num++;
                    A[x].flag = 0;
                    A[x].num--;
                }
                else
                {
                    A[x].num--;
                    A[y].num++;
                    A[y].flag = 1;
                }
            }
            else
            {
                A[x].num--;
                A[y].num++;
            }
        }
        int ans = 0;
        for(int i=1;i<=n;i++)
        {
            if(A[i].flag==1)
            {
                ans++;
            }
        }
        cout<<ans;
    }
  • 相关阅读:
    Scrapy的安装和基本使用方法
    使用BERT词向量
    主动学习方法实践:让模型变“主动”
    WSAI2020
    mac电脑的图片存储的位置
    问答系统-不能直接根据问题寻找答案?
    谷歌飞马PEGASUS
    通过强化学习协作多代理对话模型训练
    学习笔记(55)- 深度学习工具及平台
    学习笔记(54)- NLU
  • 原文地址:https://www.cnblogs.com/hao-tian/p/9037980.html
Copyright © 2020-2023  润新知