Submission #1349445

#TimeUsernameProblemLanguageResultExecution timeMemory
1349445boropotoBitaro’s Party (JOI18_bitaro)C++20
0 / 100
3 ms836 KiB
#include<iostream>
#include<vector>
#include<algorithm>

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC target("fma")
#pragma GCC target("sse")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("ssse3")
#pragma GCC target("sse4")
#pragma GCC target("popcnt")

inline void speed()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
}

const int maxn = 1e5 + 10, sq = 50;

int n, m, q, x, y;
int dp[maxn], mx[maxn];
std::vector<int> v[maxn], v1[maxn];
std::vector<std::pair<int,int>> s1[maxn];
bool used[maxn], banned[maxn], can[maxn];

bool cmp1(const std::pair<int,int>& p1, const std::pair<int,int>& p2)
{
    if(p1.second == p2.second) return p1.first < p2.first;
    return p1.second < p2.second;
}

inline void read()
{
    std::cin >> n >> m >> q;
    int x1, y1;
    for(int i = 1; i <= m; i++)
    {
        std::cin >> x1 >> y1;
        v[x1].push_back(y1);
        v1[y1].push_back(x1);
    }
}

inline void dfs(const int i)
{
    used[i] = 1;
    for(const int& nb : v[i])
    {
        if(banned[nb]) continue;
        if(!used[nb]) dfs(nb);
        if(can[nb])
        {
            dp[i] = std::max(dp[i], dp[nb] + 1);
            can[i] = 1;
        }
    }
}

inline int small(const int x, const int y)
{
    for(int i = 1; i <= n; i++)
    {
        used[i] = 0;
        banned[i] = 0;
        dp[i] = 0;
        can[i] = 0;
    }

    int node;
    for(int i = 1; i <= y; i++)
    {
        std::cin >> node;
        banned[node] = 1;
    }

    if(banned[x]) return -1;

    can[x] = 1;

    for(int i = x; i >= 1; i--)
    {
        if(!banned[i] && !used[i])
        {
            dfs(i);
        }
    }

    int ans = -1;
    for(int i = 1; i <= x; i++)
    {
        if(banned[i] || !can[i]) continue;
        ans = std::max(ans, dp[i]);
    }

    return ans;
}

void dfs1(int i)
{
    std::vector<int> nodes = {i};
    mx[i] = 0;
    used[i] = 1;

    for(const int& nb : v1[i])
    {
        if(!used[nb]) dfs1(nb);
        for(const auto& j : s1[nb])
        {
            nodes.push_back(j.second);
            mx[j.second] = std::max(mx[j.second], j.first + 1);
        }
    }

    s1[i].clear();

    std::sort(nodes.begin(), nodes.end());

    s1[i].push_back({mx[nodes[0]], nodes[0]});
    mx[nodes[0]] = -1;

    for(int j = 1; j < (int)nodes.size(); j++)
    {
        if(nodes[j] != nodes[j - 1])
        {
            s1[i].push_back({mx[nodes[j]], nodes[j]});
            mx[nodes[j]] = -1;
        }
    }

    std::sort(s1[i].rbegin(), s1[i].rend());

    while((int)s1[i].size() > sq)
    {
        s1[i].pop_back();
    }
}

inline int large(const int x, const int y)
{
    for(int i = 1; i <= n; i++)
    {
        banned[i] = 0;
    }

    int node;
    for(int i = 1; i <= y; i++)
    {
        std::cin >> node;
        banned[node] = 1;
    }

    for(const std::pair<int,int>& j : s1[x])
    {
        if(!banned[j.second]) return j.first;
    }

    return -1;
}

int main()
{
    speed();
    read();

    for(int i = 1; i <= n; i++)
    {
        if(!used[i]) dfs1(i);
    }

    for(int i = 1; i <= q; i++)
    {
        std::cin >> x >> y;
        if(y > sq)
        {
            std::cout << small(x, y) << '\n';
        }
        else
        {
            std::cout << large(x, y) << '\n';
        }
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...