Submission #1034103

# Submission time Handle Problem Language Result Execution time Memory
1034103 2024-07-25T09:48:54 Z Boas Stations (IOI20_stations) C++17
100 / 100
611 ms 1280 KB
#include <bits/stdc++.h>
using namespace std;
#include "stations.h"

#define loop(x, i) for (int i = 0; i < x; i++)
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef set<int> si;
typedef vector<vi> vvi;

vvi adj;
vi nrs;
vi d;
vi nrMx;
int curNr = 0;

void DFS(int i, int prev = -1, int de = 0)
{
	nrs[i] = curNr;
	curNr++;
	d[i] = de;
	for (int j : adj[i])
	{
		if (prev == j)
			continue;
		DFS(j, i, de + 1);
	}
	nrMx[i] = curNr;
	curNr++;
}

vi label(int n, int k, vi u, vi v)
{
	assert(k >= n);
	curNr = 0;
	nrs = vi(n);
	nrMx = vi(n);
	d = vi(n);
	adj = vvi(n);
	loop(u.size(), i)
	{
		adj[u[i]].pb(v[i]);
		adj[v[i]].pb(u[i]);
	}
	DFS(0);
	si s;
	vi labels(n);
	for (int i = 0; i < n; i++)
	{
		if (d[i] % 2 == 0)
			labels[i] = nrs[i];
		else
			labels[i] = nrMx[i];
		s.insert(labels[i]);
	}
	map<int, int> ix;
	auto it = s.begin();
	for (int i = 0; i < s.size(); i++)
	{
		ix[*it] = i;
		it++;
	}
	for (int &i : labels)
		i = ix[i];
	return labels;
}

int find_next_station(int s, int t, vi c)
{
	if (c.size() == 1)
		return c[0];
	if (s > c[0])
	{
		int nrMax = s;
		int nrGoal = t;
		if (nrGoal > nrMax || nrGoal < c[1])
		{
			// smallest number, so to the root
			return c[0];
		}
		c.pb(s);
		for (int i = 1; i < c.size() - 1; i++)
		{
			int l = c[i];
			if (nrGoal < c[i + 1] && nrGoal >= l)
				return l;
		}
	}
	else if (s == 0)
	{
		c.pb(0);
		for (int i = 1; i < c.size(); i++)
		{
			int l = c[i];
			if (t <= l && t > c[i - 1])
				return l;
		}
	}
	else // s < c[0]
	{
		int nr = s;
		int nrGoal = t;
		if (nrGoal > c[c.size() - 2] || nrGoal < nr)
		{
			// to the root
			return c.back();
		}
		c.insert(c.begin(), s);
		for (int i = 1; i < c.size(); i++)
		{
			int l = c[i];
			if (t <= l && t > c[i - 1])
				return l;
		}
	}
	// throw;
	return c[0];
}

Compilation message

stations.cpp: In function 'vi label(int, int, vi, vi)':
stations.cpp:5:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define loop(x, i) for (int i = 0; i < x; i++)
......
   42 |  loop(u.size(), i)
      |                                       
stations.cpp:42:2: note: in expansion of macro 'loop'
   42 |  loop(u.size(), i)
      |  ^~~~
stations.cpp:60:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |  for (int i = 0; i < s.size(); i++)
      |                  ~~^~~~~~~~~~
stations.cpp: In function 'int find_next_station(int, int, vi)':
stations.cpp:84:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |   for (int i = 1; i < c.size() - 1; i++)
      |                   ~~^~~~~~~~~~~~~~
stations.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |   for (int i = 1; i < c.size(); i++)
      |                   ~~^~~~~~~~~~
stations.cpp:111:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |   for (int i = 1; i < c.size(); i++)
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 356 ms 772 KB Output is correct
2 Correct 327 ms 940 KB Output is correct
3 Correct 542 ms 684 KB Output is correct
4 Correct 401 ms 684 KB Output is correct
5 Correct 400 ms 688 KB Output is correct
6 Correct 310 ms 940 KB Output is correct
7 Correct 286 ms 940 KB Output is correct
8 Correct 1 ms 776 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 1 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 316 ms 940 KB Output is correct
2 Correct 340 ms 940 KB Output is correct
3 Correct 589 ms 684 KB Output is correct
4 Correct 451 ms 684 KB Output is correct
5 Correct 363 ms 684 KB Output is correct
6 Correct 279 ms 940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 339 ms 1104 KB Output is correct
2 Correct 297 ms 940 KB Output is correct
3 Correct 611 ms 688 KB Output is correct
4 Correct 439 ms 768 KB Output is correct
5 Correct 407 ms 684 KB Output is correct
6 Correct 298 ms 940 KB Output is correct
7 Correct 292 ms 940 KB Output is correct
8 Correct 2 ms 764 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 0 ms 776 KB Output is correct
11 Correct 377 ms 772 KB Output is correct
12 Correct 296 ms 1188 KB Output is correct
13 Correct 295 ms 772 KB Output is correct
14 Correct 309 ms 944 KB Output is correct
15 Correct 34 ms 768 KB Output is correct
16 Correct 40 ms 684 KB Output is correct
17 Correct 50 ms 940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 557 ms 680 KB Output is correct
2 Correct 439 ms 684 KB Output is correct
3 Correct 387 ms 684 KB Output is correct
4 Correct 1 ms 764 KB Output is correct
5 Correct 3 ms 768 KB Output is correct
6 Correct 0 ms 768 KB Output is correct
7 Correct 361 ms 768 KB Output is correct
8 Correct 571 ms 684 KB Output is correct
9 Correct 435 ms 864 KB Output is correct
10 Correct 446 ms 684 KB Output is correct
11 Correct 2 ms 768 KB Output is correct
12 Correct 3 ms 768 KB Output is correct
13 Correct 2 ms 776 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 2 ms 768 KB Output is correct
16 Correct 349 ms 684 KB Output is correct
17 Correct 327 ms 684 KB Output is correct
18 Correct 354 ms 684 KB Output is correct
19 Correct 340 ms 684 KB Output is correct
20 Correct 380 ms 684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 365 ms 952 KB Output is correct
2 Correct 333 ms 940 KB Output is correct
3 Correct 608 ms 936 KB Output is correct
4 Correct 472 ms 684 KB Output is correct
5 Correct 389 ms 768 KB Output is correct
6 Correct 294 ms 764 KB Output is correct
7 Correct 315 ms 940 KB Output is correct
8 Correct 1 ms 768 KB Output is correct
9 Correct 4 ms 764 KB Output is correct
10 Correct 0 ms 768 KB Output is correct
11 Correct 315 ms 940 KB Output is correct
12 Correct 372 ms 940 KB Output is correct
13 Correct 589 ms 684 KB Output is correct
14 Correct 419 ms 684 KB Output is correct
15 Correct 393 ms 684 KB Output is correct
16 Correct 307 ms 944 KB Output is correct
17 Correct 395 ms 684 KB Output is correct
18 Correct 263 ms 1280 KB Output is correct
19 Correct 283 ms 1096 KB Output is correct
20 Correct 285 ms 940 KB Output is correct
21 Correct 35 ms 764 KB Output is correct
22 Correct 38 ms 768 KB Output is correct
23 Correct 68 ms 1004 KB Output is correct
24 Correct 3 ms 760 KB Output is correct
25 Correct 2 ms 776 KB Output is correct
26 Correct 3 ms 768 KB Output is correct
27 Correct 2 ms 820 KB Output is correct
28 Correct 0 ms 768 KB Output is correct
29 Correct 365 ms 688 KB Output is correct
30 Correct 361 ms 684 KB Output is correct
31 Correct 377 ms 684 KB Output is correct
32 Correct 372 ms 684 KB Output is correct
33 Correct 329 ms 684 KB Output is correct
34 Correct 208 ms 940 KB Output is correct
35 Correct 265 ms 1048 KB Output is correct
36 Correct 314 ms 1028 KB Output is correct
37 Correct 300 ms 1016 KB Output is correct
38 Correct 315 ms 1036 KB Output is correct
39 Correct 258 ms 1028 KB Output is correct
40 Correct 309 ms 1024 KB Output is correct
41 Correct 326 ms 1276 KB Output is correct
42 Correct 36 ms 684 KB Output is correct
43 Correct 57 ms 1044 KB Output is correct
44 Correct 86 ms 960 KB Output is correct
45 Correct 105 ms 944 KB Output is correct
46 Correct 221 ms 684 KB Output is correct
47 Correct 192 ms 940 KB Output is correct
48 Correct 40 ms 1116 KB Output is correct
49 Correct 29 ms 940 KB Output is correct