Submission #883950

#TimeUsernameProblemLanguageResultExecution timeMemory
883950OAleksaBitaro’s Party (JOI18_bitaro)C++14
0 / 100
4 ms12892 KiB
#include<bits/stdc++.h> using namespace std; #define f first #define s second #define int long long const int N = 1e5 + 69; const int B = 320; int n, m, q, deg[N], cnt[N], dp[N], deg1[N], d[N]; vector<int> g[N], in[N]; set<pair<int, int>> bst[N]; vector<int> dis[B]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt = 1; //cin >> tt; while (tt--) { cin >> n >> m >> q; for (int i = 1;i <= m;i++) { int a, b; cin >> a >> b; if (a > b) swap(a, b); g[a].push_back(b); in[b].push_back(a); deg[b]++; deg1[b]++; } queue<int> que; vector<int> p; for (int i = 1;i <= n;i++) { if (deg[i] == 0) { que.push(i); } } while (!que.empty()) { auto v = que.front(); que.pop(); for (auto u : g[v]) { --deg[u]; dp[u] = max(dp[u], dp[v] + 1); que.push(u); } } vector<int> t(n); iota(t.begin(), t.end(), 1); sort(t.begin(), t.end(), [&](int i, int j) { return dp[i] < dp[j]; }); for (int i = 0;i < min(2 * B, n);i++) { p.push_back(t[i]); } sort(p.begin(), p.end()); for (int j = 0;j < (int)p.size();j++) { for (int i = 1;i <= n;i++) dp[i] = 0, deg[i] = deg1[i]; que.push(p[j]); while (!que.empty()) { auto v = que.front(); que.pop(); for (auto u : g[v]) { --deg[u]; dp[u] = max(dp[u], dp[v] + 1); if (deg[u] == 0) que.push(u); } } for (int i = 1;i <= n;i++) dis[j].push_back(dp[i]); } while (q--) { int s, c; cin >> s >> c; int a[c]; for (int i = 0;i < c;i++) cin >> a[i]; int ans = 0; if (c < B) { sort(a, a + c); for (int i = 0;i < (int)p.size();i++) { auto u = lower_bound(a, a + c, p[i]) - a; if (u == c || a[u] != p[i]) ans = max(ans, dis[i][s - 1]); } } else { for (int i = 1;i <= n;i++) d[i] = cnt[i] = 0; for (int i = 1;i <= n;i++) cnt[a[i]] = 1; que.push(s); while (!que.empty()) { auto v = que.front(); que.pop(); for (auto u : in[v]) { if (d[u] >= d[v] + 1) continue; d[u] = d[v] + 1; que.push(v); } } for (int i = 1;i <= n;i++) { if (!cnt[i]) ans = max(ans, d[i]); } } cout << ans << '\n'; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...