Submission #400371

#TimeUsernameProblemLanguageResultExecution timeMemory
400371nikatamlianiBitaro’s Party (JOI18_bitaro)C++14
100 / 100
1906 ms220352 KiB
#include <bits/stdc++.h>
using namespace std;
void maxi(int &x, int y) {
	if(x < y) x = y;
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	const int C = 150;
	int n, m, q;
	cin >> n >> m >> q;
	vector<vector<int>> g(n+1);
	vector<vector<int>> r(n+1);
	for(int i = 1; i <= m; ++i) {
		int u, v; cin >> u >> v;
		g[u].push_back(v);
		r[v].push_back(u);
	}
	vector<int> dst(n+1);
	vector<vector<pair<int, int>>> list(n+1); 
	for(int i = 1; i <= n; ++i) {
		for(int x : r[i]) {
			for(auto p : list[x]) maxi(dst[p.second], p.first);
		}
		for(int x : r[i]) {
			for(auto p : list[x]) {
				if(dst[p.second] == p.first)  {
					list[i].push_back({p.first+1, p.second});
					dst[p.second] = -1;
				}
			}
		}
		list[i].push_back({0, i});
		sort(list[i].rbegin(), list[i].rend());
		while((int)list[i].size() > C) list[i].pop_back();
		for(int x : r[i]) {
			for(auto p : list[x]) dst[p.second] = 0;
		}
	}
	vector<bool> blocked(n+1);
	while(q--) {
		int t, x; cin >> t >> x;
		vector<int> v(x);
		for(int i = 0; i < x; ++i) {
			cin >> v[i]; 
			blocked[v[i]] = 1;
		}
		int ans = -1;
		if(x < C) {
			for(pair<int, int> p : list[t]) {
				if(!blocked[p.second]) {
					ans = p.first;
					break;
				}
			}
		} else {
			vector<int> dp(n+1, -1e6);
			for(int i = 1; i <= t; ++i) {
				if(!blocked[i]) {
					dp[i] = 0;
				}
				for(int x : r[i]) {
					maxi(dp[i], dp[x]+1);
				}
			}
			ans = dp[t];
		}
		if(ans < 0) ans = -1;
		cout << ans << '\n';
		for(int i : v) blocked[i] = 0;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...