AtCoder - Box and Ball

题目:Box and Ball
分析:模拟题,做M次操作,每次操作就是将第x号盒子中随机摸一个球放到y号盒子中。 现在想知道所有操作完成之后,有多少个盒子中可能有红球。
#include <iostream>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<queue>
#include<math.h>
#include<stack>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<iostream>
using namespace std;
#define MAX 99999999
typedef long long ll;
int n,m;
int a[100001];//盒子中球的个数
int red[100001];//表示该号球可能有红球,1为可能,0为不可能
int main()
{
    
    
    cin>>n>>m;
    int cnt = 0;
    red[1] = 1;
    for(int i = 1; i <= n;i++)
        a[i] = 1;
    for(int i = 0 ; i < m ;i++)
    {
    
    
        int x,y;
        cin>>x>>y;
        if(red[x] && a[x] == 1)
        {
    
    
            red[x] = 0;
            red[y] = 1;
        }
        else if(red[x] && a[x] > 1)
            red[y] = 1;      
        a[x]--;
        a[y]++;
    }
    for(int i = 1; i <= n;i++)
        if(red[i])
            cnt++;
    cout<<cnt;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43567222/article/details/114579953
Box