Submission #916878

#TimeUsernameProblemLanguageResultExecution timeMemory
916878MateiKing80Shell (info1cup18_shell)C++14
100 / 100
330 ms36740 KiB
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

int n, m, p;
vector<int> o, ptop, top, nrt, dp;
vector<vector<int>> v, vi;

void maketop()
{
    queue<int> q;
    for(int i = 1; i <= n; i ++)
        if(!nrt[i])
            q.push(i);
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        for(auto i : v[x])
        {
            nrt[i] --;
            if(!nrt[i])
                q.push(i);
        }
        top.push_back(x);

    }
    for(int i = 1; i <= n; i ++)
        ptop[top[i]] = i;
}

bool cmp(int x, int y)
{
    return ptop[x] < ptop[y];
}

void rsz()
{
    o.resize(p + 1);
    ptop.resize(n + 1);
    dp.resize(n + 1);
    nrt.resize(n + 1);
    top.push_back(-1);
    v.resize(n + 1);
    vi.resize(n + 1);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m >> p;
    rsz();
    for(int i = 1; i <= p; i ++)
        cin >> o[i];
    for(int i = 1; i <= m; i ++)
    {
        int u, w;
        cin >> u >> w;
        v[u].push_back(w);
        vi[w].push_back(u);
        nrt[w] ++;
    }
    maketop();
    int maxx = 0;
    for(int i = 1; i <= p; i ++)
        maxx = max(maxx, ptop[o[i]]);
    if(maxx > ptop[n])
    {
        cout << 0;
        return 0;
    }
    sort(o.begin() + 1, o.end(), cmp);
    o.push_back(n);
    int pos = 1;
    dp[1] ++;
    for(int i = 1; i <= n; i ++)
    {
        if(top[i] == o[pos])
            pos ++;
        for(auto j : v[top[i]])
        {
            if(ptop[j] <= ptop[o[pos]])
                dp[j] += dp[top[i]], dp[j] %= mod;
        }
    }
    cout << dp[n];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...