Submission #8518182


Source Code Expand

#include "bits/stdc++.h"
using namespace std;

class UnionFind {
private:
	long long Nodes;
	vector<long long> Parent, Rank, Size;
public:
	UnionFind(long long N) : Nodes(N) {
		Parent.resize(N);
		Rank.assign(N, 0);
		Size.assign(N, 1);
		for (int i = 0; i < N; i++) {
			Parent[i] = i;
		}
	}
	void reset(long long N) {
		if (Nodes == N) {
			for (int i = 0; i < N; i++) {
				Parent[i] = i;
				Rank[i] = 0;
				Size[i] = 1;
			}
		}
		else {
			Nodes = N;
			Parent.resize(N);
			Rank.assign(N, 0);
			Size.assign(N, 1);
			for (int i = 0; i < N; i++) {
				Parent[i] = i;
			}
		}
		return;
	}
	long long Root(long long N) {
		return N == Parent[N] ? N : Parent[N] = Root(Parent[N]);
	}
	bool Same(long long A, long long B) {
		return Root(A) == Root(B);
	}
	long long SizeF(long long A) {
		return Size[Root(A)];
	}
	void Merge(long long A, long long B) {
		long long RA = Root(A), RB = Root(B);
		if (RA == RB) return;
		if (Rank[RA] < Rank[RB]) {
			Size[RB] += Size[RA];
			Parent[RA] = RB;
		}
		else {
			Size[RA] += Size[RB];
			Parent[RB] = RA;
			if (Rank[RA] == Rank[RB]) Rank[RA]++;
		}
	}
};

struct Query {
	long long X, Y, Z, L, R, N;
};

int main() {
	long long N, M, Q;
	vector<pair<long long, long long> > E;
	vector<Query> VQ;
	cin >> N >> M;
	E.resize(M);
	UnionFind UF(N);
	for (int i = 0; i < M; i++) {
		cin >> E[i].first >> E[i].second;
		E[i].first--, E[i].second--;
	}
	cin >> Q;
	VQ.resize(Q);
	for (int i = 0; i < Q; i++) {
		cin >> VQ[i].X >> VQ[i].Y >> VQ[i].Z;
		VQ[i].X--, VQ[i].Y--;
		VQ[i].L = -1, VQ[i].R = M, VQ[i].N = i;
	}
	while (1) {
		bool check = true;
		sort(VQ.begin(), VQ.end(), [](Query Q1, Query Q2) {return (Q1.L + Q1.R) < (Q2.L + Q2.R); });
		UF.reset(N);
		long long NOW = 0;
		for (int i = 0; i < M; i++) {
			while (NOW < Q && (VQ[NOW].L + VQ[NOW].R + 1) / 2 == i) {
				if (VQ[NOW].R - VQ[NOW].L <= 1) {
					NOW++;
					if (NOW == Q) break;
					continue;
				}
				long long S = 0;
				if (UF.Same(VQ[NOW].X, VQ[NOW].Y)) S = UF.SizeF(VQ[NOW].X);
				else S = UF.SizeF(VQ[NOW].X) + UF.SizeF(VQ[NOW].Y);
				if (S >= VQ[NOW].Z) VQ[NOW].R = i;
				else VQ[NOW].L = i;
				check = false;
				NOW++;
				if (NOW == Q) break;
			}
			if (NOW == Q) break;
			UF.Merge(E[i].first, E[i].second);
		}
		if (check) break;
	}
	sort(VQ.begin(), VQ.end(), [](Query Q1, Query Q2) {return Q1.N < Q2.N; });
	for (int i = 0; i < Q; i++) {
		cout << VQ[i].R << endl;
	}
}

Submission Info

Submission Time
Task D - Stamp Rally
User yuma220284
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 2522 Byte
Status AC
Exec Time 496 ms
Memory 9472 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 1
AC × 33
Set Name Test Cases
Sample 0_00.txt
All 0_00.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt
Case Name Status Exec Time Memory
0_00.txt AC 1 ms 256 KB
1_00.txt AC 382 ms 9472 KB
1_01.txt AC 424 ms 9472 KB
1_02.txt AC 397 ms 9472 KB
1_03.txt AC 423 ms 9472 KB
1_04.txt AC 401 ms 9472 KB
1_05.txt AC 426 ms 9472 KB
1_06.txt AC 403 ms 9472 KB
1_07.txt AC 427 ms 9472 KB
1_08.txt AC 411 ms 9472 KB
1_09.txt AC 428 ms 9472 KB
1_10.txt AC 422 ms 9472 KB
1_11.txt AC 441 ms 9472 KB
1_12.txt AC 435 ms 9472 KB
1_13.txt AC 446 ms 9472 KB
1_14.txt AC 416 ms 9472 KB
1_15.txt AC 426 ms 9472 KB
1_16.txt AC 487 ms 9344 KB
1_17.txt AC 486 ms 9344 KB
1_18.txt AC 487 ms 9344 KB
1_19.txt AC 496 ms 9472 KB
1_20.txt AC 473 ms 9088 KB
1_21.txt AC 489 ms 9344 KB
1_22.txt AC 485 ms 9344 KB
1_23.txt AC 483 ms 9344 KB
1_24.txt AC 479 ms 9216 KB
1_25.txt AC 487 ms 9344 KB
1_26.txt AC 476 ms 9216 KB
1_27.txt AC 483 ms 9344 KB
1_28.txt AC 486 ms 9344 KB
1_29.txt AC 481 ms 9216 KB
1_30.txt AC 484 ms 9344 KB
1_31.txt AC 476 ms 9088 KB