P1113-杂务

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define _for(i,a,b) for(int i = (a);i < (b);i ++)
#define INF 100000003
#define ll long long
inline ll read()
{
    ll ans = 0;
    char ch = getchar(), last = ' ';
    while(!isdigit(ch)) last = ch, ch = getchar();
    while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    if(last == '-') ans = -ans;
    return ans;
}
inline void write(ll x)
{
    if(x < 0) x = -x, putchar('-');
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}

vector<int> G[10003];
int T[10003];
int con[10003];
int n; 
int main()
{
    n = read();
    _for(i,1,n+1)
    {
        read();
        T[i] = read();
        int t;
        while(t = read())
        {
            if(!t)
                break;
            else
                G[t].pb(i);
            con[i] ++;
        }
    }

    int t = 0;
    queue<int> q;
    q.push(1);
    q.push(0);
    while(q.size()!=1)
    {
        while(q.front())
        {
            int x = q.front();
            q.pop();
            T[x]--;
            if(T[x])
                q.push(x);
            else
                _for(i,0,G[x].size())
                {
                    con[G[x][i]] --;
                    if(!con[G[x][i]])
                        q.push(G[x][i]);
                }
        }
        q.pop();
        q.push(0);
        t ++;
    }
    write(t);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/11526774.html