Submission #369660

#TimeUsernameProblemLanguageResultExecution timeMemory
3696608e7Bitaro’s Party (JOI18_bitaro)C++14
14 / 100
2093 ms219116 KiB
//Challenge: Accepted #include <iostream> #include <algorithm> #include <utility> #include <vector> #define ll long long #define maxn 100005 #define pii pair<int, int> #define ff first #define ss second using namespace std; const int siz = 140; vector<int> adj[maxn], from[maxn]; vector<pii> pnt[maxn], tmp; inline bool cmp (pii a, pii b) { return a.ss > b.ss; } int dp[maxn]; bool part[maxn], found[maxn]; void getdp(int n, int id) { for (int i = 1;i <= id;i++) dp[i] = 0; for (int i = 1;i <= id;i++) { for (int v:adj[i]) { dp[v] = max(dp[v], (dp[i] == 0 ? (part[i] ? -1 : 0) : dp[i]) + 1); } } } vector<pii> merge(vector<pii> a, vector<pii> b) { vector<pii> ret; for (auto &i:b) i.ss++; int n = a.size(), m = b.size(), ind = 0; for (int i = 0;i < n;i++) { while (ind < m && b[ind].ss > a[i].ss) { if (!found[b[ind].ff]) { found[b[ind].ff] = 1; ret.push_back(b[ind]); } ind++; if (ret.size() > siz) break; } if (!found[a[i].ff]) { found[a[i].ff] = 1; ret.push_back(a[i]); } if (ret.size() > siz) break; } while (ind < m) { if (!found[b[ind].ff]) { found[b[ind].ff] = 1; ret.push_back(b[ind]); } ind++; if (ret.size() > siz) break; } for (auto i:ret) found[i.ff] = 0; while (ret.size() > siz) ret.pop_back(); return ret; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); int n, m, q; cin >> n >> m >> q; for (int i = 0;i < m;i++) { int a, b; cin >> a >> b; adj[a].push_back(b); from[b].push_back(a); } for (int i = 1;i <= n;i++) { pnt[i].push_back(make_pair(i, 0)); for (int v:from[i]) { pnt[i] = merge(pnt[i], pnt[v]); } } getdp(n, n); while (q--) { int ind, yi; cin >> ind >> yi; vector<int> val; for (int i = 0;i < yi;i++) { int x; cin >> x; part[x] = 1; val.push_back(x); } if (yi >= siz) { getdp(n, ind); if (part[ind] && dp[ind] == 0) cout << -1 << "\n"; else cout << dp[ind] << "\n"; } else { int ans = -1; for (auto v:pnt[ind]) { if (!part[v.ff]) ans = max(ans, v.ss); } cout << ans << "\n"; } for (int i:val) part[i] = 0; } } /* 5 6 3 1 2 2 4 3 4 1 3 3 5 4 5 4 1 1 5 2 2 3 2 3 1 4 5 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...