zoukankan      html  css  js  c++  java
  • 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≤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=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
    不要忘记努力,不要辜负自己 欢迎指正 QQ:1468580561
  • 相关阅读:
    BZOJ 3611: [Heoi2014]大工程 [虚树 DP]
    BZOJ 3991: [SDOI2015]寻宝游戏 [虚树 树链的并 set]
    BZOJ 2286: [Sdoi2011消耗战 [DP 虚树]
    BZOJ 4767: 两双手 [DP 组合数]
    BZOJ 1426: 收集邮票 [DP 期望 平方]
    转「服务器运维」如何解决服务器I/O过高的问题
    iostat查看linux硬盘IO性能
    Linux前台、后台、挂起、退出、查看命令汇总
    Linux虚拟内存的作用
    -bash: iostat: command not found解决办法
  • 原文地址:https://www.cnblogs.com/smallocean/p/8715367.html
Copyright © 2011-2022 走看看