Submission #827096


Source Code Expand

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;

immutable int NA = -1;

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto u = new int [m];
		auto v = new int [m];
		foreach (i; 0..m)
		{
			readf (" %s %s", &u[i], &v[i]);
		}
		u[] -= 1;
		v[] -= 1;

		alias Record = Tuple !(int, q{p}, int, q{r}, int, q{t});
		auto a = new Record [] [n];
		foreach (i; 0..n)
		{
			a[i] = [Record (i, 1, 0)];
		}

		int place (int w, int t)
		{
			int lo = 0;
			int hi = cast (int) (a[w].length) - 1;
			while (lo < hi)
			{
				int me = (lo + hi + 1) >> 1;
				if (a[w][me].t > t)
				{
					hi = me - 1;
				}
				else
				{
					lo = me;
				}
			}
			return lo;
		}

		int root (int w, int t)
		{
			int i = place (w, t);
			while (a[w][i].t > t)
			{
				i -= 1;
			}
			if (w == a[w][i].p)
			{
				return w;
			}
			return root (a[w][i].p, t);
		}

		void unite (int d, int e, int t)
		{
			d = root (d, t);
			e = root (e, t);
			if (d == e)
			{
				return;
			}
			int id = place (d, t);
			int ie = place (e, t);
			if (a[d][id].r < a[e][ie].r)
			{
				swap (d, e);
				swap (id, ie);
			}
			a[d] ~= Record (d, a[d][id].r + a[e][ie].r, t);
			a[e] ~= Record (d, a[e][ie].r, t);
		}

		foreach (int i; 0..m)
		{
			unite (u[i], v[i], i + 1);
		}

		int q;
		readf (" %s", &q);
		auto x = new int [q];
		auto y = new int [q];
		auto z = new int [q];
		foreach (j; 0..q)
		{
			readf (" %s %s %s", &x[j], &y[j], &z[j]);
		}
		x[] -= 1;
		y[] -= 1;

		auto res = new int [q];
		res[] = NA;
		int ilo = 0;
		foreach (j; 0..q)
		{
			int lo = 0;
			int hi = m;
			while (lo < hi)
			{
				int me = (lo + hi) >> 1;
				int cx = root (x[j], me);
				int cy = root (y[j], me);
				int cz = a[cx][place (cx, me)].r;
				if (cy != cx)
				{
					cz += a[cy][place (cy, me)].r;
				}
				if (cz >= z[j])
				{
					hi = me;
				}
				else
				{
					lo = me + 1;
				}
			}
			res[j] = lo;
		}

		writefln ("%(%s\n%)", res);
	}
}

Submission Info

Submission Time
Task D - Stamp Rally
User Gassa
Language D (DMD64 v2.070.1)
Score 1000
Code Size 2102 Byte
Status AC
Exec Time 1447 ms
Memory 13692 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 4 ms 256 KB
1_00.txt AC 1109 ms 10876 KB
1_01.txt AC 967 ms 10876 KB
1_02.txt AC 1438 ms 13052 KB
1_03.txt AC 1125 ms 13052 KB
1_04.txt AC 1447 ms 12796 KB
1_05.txt AC 1150 ms 12924 KB
1_06.txt AC 1441 ms 13180 KB
1_07.txt AC 1167 ms 13436 KB
1_08.txt AC 1220 ms 12412 KB
1_09.txt AC 1041 ms 12540 KB
1_10.txt AC 952 ms 13692 KB
1_11.txt AC 873 ms 13692 KB
1_12.txt AC 715 ms 11900 KB
1_13.txt AC 691 ms 11900 KB
1_14.txt AC 501 ms 13436 KB
1_15.txt AC 530 ms 13436 KB
1_16.txt AC 1331 ms 13052 KB
1_17.txt AC 1272 ms 12796 KB
1_18.txt AC 1266 ms 12796 KB
1_19.txt AC 1331 ms 13308 KB
1_20.txt AC 1215 ms 12284 KB
1_21.txt AC 1287 ms 13180 KB
1_22.txt AC 1241 ms 12668 KB
1_23.txt AC 1243 ms 12668 KB
1_24.txt AC 1222 ms 12028 KB
1_25.txt AC 1270 ms 12796 KB
1_26.txt AC 1252 ms 12540 KB
1_27.txt AC 1258 ms 12924 KB
1_28.txt AC 1276 ms 12924 KB
1_29.txt AC 1250 ms 12028 KB
1_30.txt AC 1262 ms 12796 KB
1_31.txt AC 1265 ms 12284 KB