Submission #1030164

#TimeUsernameProblemLanguageResultExecution timeMemory
1030164SamuellH12Bitaro’s Party (JOI18_bitaro)C++17
7 / 100
981 ms524288 KiB
#include <bits/stdc++.h>
#define optimize ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ALL(x) x.begin(), x.end()
#define endl "\n"
#define vi vector<int>
#define pii pair<int,int>
const int MAXN = 1e5 + 5;
const int SQRT = 200;
using namespace std;
#define int long long
 
int block[MAXN], dp[MAXN], tempo;
vi trafo[MAXN], order;
vector<pii> best[MAXN];
 
void get_order(int n){
	queue<int> fila;
 
	for(int i=1; i<=n; i++)	
		if(dp[i] == 0)
			fila.emplace(i);
 
	while(!fila.empty())
	{
		auto u = fila.front(); fila.pop();
		order.emplace_back(u);
 
		for(auto v : trafo[u])
			if(--dp[v] == 0)
				fila.emplace(v);
	}
 
	reverse(ALL(order));
}
 
void precalc(){
	for(auto u : order)
	{
        best[u].emplace_back(0, u);
		for(auto v : trafo[u])
		{
			// best[u].emplace_back(-1, v);
 
			for(auto [x, w] : best[v])
				if(best[u].back() != pii(x-1, w))
					best[u].emplace_back(x-1, w);	
 
			if(best[u].size() > 2*SQRT)
			{
				sort(ALL(best[u]));
				best[u].resize(SQRT);
			}
		}
 
		sort(ALL(best[u]));
		if(best[u].size() > SQRT) best[u].resize(SQRT);
	}
}
 
int solve(int w){
	for(auto u : order)
	{
		dp[u] = (block[u] == tempo ? -1 : 0);
 
		for(auto v : trafo[u])
			if(~dp[v]) 
				dp[u] = max(dp[u], dp[v]+1);
 
		if(u == w) return dp[u];
	}
	
	assert(false);
	return dp[w];
}
 
int32_t main(){
	optimize;
	int n, m, q;
	cin >> n >> m >> q;
	
	for(int i=0, u, v; i<m; i++)
	{
		cin >> u >> v;
		trafo[v].emplace_back(u);
		dp[u]++;
	}
 
	get_order(n);
	precalc();
 
	int t, y;
	while(q--)
	{
		tempo++;
		cin >> t >> y;
 
		for(int i=0, x; i<y; i++)
			cin >> x, block[x] = tempo;
 
		int ans = -100;
 
		for(auto [x, w] : best[t]) 
			if(block[w] != tempo)
			{
				ans = -x;
				break;
			}
 
		if(ans == -100) ans = solve(t);
		
		cout << ans << endl;
	}
 
	return 0;	
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...