Submission #836490

# Submission time Handle Problem Language Result Execution time Memory
836490 2023-08-24T11:48:20 Z unnick City Mapping (NOI18_citymapping) C++14
57 / 100
60 ms 18080 KB
#include "citymapping.h"

//#include <iostream>
#include <functional>
#include <algorithm>
#include <numeric>
#include <vector>

using namespace std;

#define ll long long

typedef struct {
	int to;
	ll d;
} edge_t;

void find_roads(int N, int Q, int A[], int B[], int W[]) {
	if (Q <= 12000) {
		int start;
		{
			ll maxd = 0;
			for (int i = 2; i <= N; i++) {
				ll d = get_distance(1, i);
				if (d > maxd) {
					maxd = d;
					start = i;
				}
			}
		}
		vector<ll> dists(N);
		dists[start-1] = 0;
		vector<int> ids(N);
		iota(ids.begin(), ids.end(), 0);
		for (int i = 1; i <= N; i++) {
			if (i == start) continue;
			dists[i-1] = get_distance(start,i);
		}
		sort(ids.begin(), ids.end(), [&](int a, int b) {
			return dists[a] < dists[b];
		});
		for (int i = 0; i < N-1; i++) {
			A[i] = ids[i]+1;
			B[i] = ids[i+1]+1;
			W[i] = dists[ids[i+1]] - dists[ids[i]];
		}
	} else {
		vector<ll> dists(N*N, -1);
		auto ds = [&](int i_, int j_){
			i_--;
			j_--;
			int i = min(i_, j_);
			int j = max(i_, j_);
			if (dists[i+j*N] == -1) {
				dists[i+j*N] = get_distance(i+1, j+1);
			}
			return dists[i+j*N];
		};
		int out_idx = 0;
		auto output = [&](int x, int y, int dist) {
			A[out_idx] = x;
			B[out_idx] = y;
			W[out_idx++] = dist;
		};
		// for (int i = 1; i <= N; i++) {
		// 	for (int j = i+1; j <= N; j++) {
		// 		// if (get_distance(i,j) == 1) {
		// 		// 	A[k] = i;
		// 		// 	B[k] = j;
		// 		// 	W[k++] = 1;
		// 		// }
		// 		dists[i+j*N] = dists[j+i*N] = get_distance(i,j);
		// 	}
		// }

		std::function<void(vector<edge_t>&, int)> rec = [&](vector<edge_t> &edges, int x) -> void {
			if (edges.size() == 0) return;
			vector<edge_t> sub;
			vector<edge_t> rest;
			int y = edges[0].to;
			output(x, y, edges[0].d);
			for (edge_t edge : edges) {
				if (edge.to == x || edge.to == y) continue;
				ll dist = ds(y, edge.to);
				if (dist < edge.d) {
					sub.push_back({.to = edge.to, .d = ds(y, edge.to)});
				} else {
					rest.push_back(edge);
				}
			}
			sort(sub.begin(), sub.end(), [](edge_t a, edge_t b) {
				return a.d < b.d;
			});
			rec(sub, y);
			rec(rest, x);
		};
		vector<edge_t> edges;
		for (int i = 2; i <= N; i++) {
			ll dist = ds(1, i);
			edges.push_back({.to = i, .d = dist});
		}
		sort(edges.begin(), edges.end(), [](edge_t a, edge_t b) {
			return a.d < b.d;
		});
		rec(edges, 1);
	}
}

Compilation message

citymapping.cpp: In function 'void find_roads(int, int, int*, int*, int*)':
citymapping.cpp:37:29: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
   37 |    dists[i-1] = get_distance(start,i);
      |                 ~~~~~~~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 11476 KB Correct: 251481 out of 500000 queries used.
2 Correct 26 ms 14372 KB Correct: 296644 out of 500000 queries used.
3 Correct 4 ms 8404 KB Correct: 25535 out of 500000 queries used.
4 Correct 4 ms 8404 KB Correct: 21641 out of 500000 queries used.
5 Correct 8 ms 8660 KB Correct: 43331 out of 500000 queries used.
# Verdict Execution time Memory Grader output
1 Correct 21 ms 11476 KB Correct: 251481 out of 500000 queries used.
2 Correct 26 ms 14372 KB Correct: 296644 out of 500000 queries used.
3 Correct 4 ms 8404 KB Correct: 25535 out of 500000 queries used.
4 Correct 4 ms 8404 KB Correct: 21641 out of 500000 queries used.
5 Correct 8 ms 8660 KB Correct: 43331 out of 500000 queries used.
6 Correct 60 ms 18080 KB Correct: 479862 out of 500000 queries used.
7 Correct 23 ms 12804 KB Correct: 219439 out of 500000 queries used.
8 Correct 5 ms 8532 KB Correct: 26548 out of 500000 queries used.
9 Correct 5 ms 8408 KB Correct: 19290 out of 500000 queries used.
10 Correct 6 ms 8584 KB Correct: 38697 out of 500000 queries used.
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
2 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
3 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
4 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
5 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
2 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
3 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
4 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
5 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
6 Correct 1 ms 468 KB Correct: 1994 out of 12000 queries used.
7 Correct 1 ms 468 KB Correct: 1990 out of 12000 queries used.
8 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
9 Correct 1 ms 500 KB Correct: 1992 out of 12000 queries used.
10 Correct 1 ms 468 KB Correct: 1986 out of 12000 queries used.
# Verdict Execution time Memory Grader output
1 Correct 21 ms 11476 KB Correct: 251481 out of 500000 queries used.
2 Correct 26 ms 14372 KB Correct: 296644 out of 500000 queries used.
3 Correct 4 ms 8404 KB Correct: 25535 out of 500000 queries used.
4 Correct 4 ms 8404 KB Correct: 21641 out of 500000 queries used.
5 Correct 8 ms 8660 KB Correct: 43331 out of 500000 queries used.
6 Correct 60 ms 18080 KB Correct: 479862 out of 500000 queries used.
7 Correct 23 ms 12804 KB Correct: 219439 out of 500000 queries used.
8 Correct 5 ms 8532 KB Correct: 26548 out of 500000 queries used.
9 Correct 5 ms 8408 KB Correct: 19290 out of 500000 queries used.
10 Correct 6 ms 8584 KB Correct: 38697 out of 500000 queries used.
11 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
12 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
13 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
14 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
15 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
16 Correct 1 ms 468 KB Correct: 1994 out of 12000 queries used.
17 Correct 1 ms 468 KB Correct: 1990 out of 12000 queries used.
18 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
19 Correct 1 ms 500 KB Correct: 1992 out of 12000 queries used.
20 Correct 1 ms 468 KB Correct: 1986 out of 12000 queries used.
21 Incorrect 7 ms 8660 KB Too many calls to get_distance().
22 Halted 0 ms 0 KB -