제출 #599663

#제출 시각아이디문제언어결과실행 시간메모리
599663GusterGoose27Bitaro’s Party (JOI18_bitaro)C++11
7 / 100
2081 ms210392 KiB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int MAXN = 1e5;
const int MAXrtn = 317;
int n, m, q;
bool inactive[MAXN];
pii precomp[MAXN][MAXrtn]; // dist, node
int rtn;
vector<int> edges[MAXN];
int dp[MAXN];
int posses[MAXN];

void make() {
	for (int i = 0; i < n; i++) {
		unordered_set<int> vis;
		fill(precomp[i], precomp[i]+rtn, pii(-1, -1));
		priority_queue<pii> pq; // push the index where it came from not the original index
		pq.push(pii(0, i));
		for (int in: edges[i]) {
			posses[in] = 0;
			pq.push(pii(precomp[in][0].first+1, in));
		}
		for (int t = 0; !pq.empty() && t < rtn; t++) {
			pii top = pq.top();
			pq.pop();
			int cnode = top.second;
			if (top.first == -1) break;
			if (cnode == i) {
				precomp[i][t] = top;
				break;
			}
			if (vis.find(precomp[cnode][posses[cnode]].second) == vis.end()) {
				precomp[i][t] = pii(top.first, precomp[cnode][posses[cnode]].second);
				vis.insert(precomp[cnode][posses[cnode]].second);
			}
			else {
				t--;
			}
			posses[cnode]++;
			if (posses[cnode] < rtn && precomp[cnode][posses[cnode]].first >= 0) pq.push(pii(precomp[cnode][posses[cnode]].first+1, cnode));
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> m >> q;
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y; x--; y--;
		if (y < x) {
			int temp = x; x = y; y = temp;
		}
		edges[y].push_back(x);
	}
	rtn = max(1, (int) (sqrt(n)));
	make();
	for (int t = 0; t < q; t++) {
		int pos, num;
		cin >> pos >> num;
		pos--;
		if (num >= rtn) {
			memset(inactive, 0, sizeof(bool)*n);
			for (int i = 0; i < num; i++) {
				int x; cin >> x; x--;
				inactive[x] = 1;
			}	
			for (int i = 0; i <= pos; i++) {
				if (inactive[i]) {
					dp[i] = -1;
				}
				else dp[i] = 0;
				for (int in: edges[i]) {
					if (dp[in] >= 0) dp[i] = max(dp[i], 1+dp[in]);
				}
			}
			cout << dp[pos] << "\n";
			continue;
		}
		unordered_set<int> curvals;
		for (int i = 0; i < num; i++) {
			int x; cin >> x; x--;
			curvals.insert(x);
		}
		for (int i = 0; i < rtn; i++) {
			if (curvals.find(precomp[pos][i].second) == curvals.end()) {
				cout << precomp[pos][i].first << "\n";
				break;
			}
		}
		//assert(false);
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...