#include <bits/stdc++.h>
#include "citymapping.h"
using namespace std;
const int nax = 1002;
struct Dsu {
int par[nax];
int sz[nax];
Dsu() {
for(int i = 0; i < nax; i++) {
par[i] = i;
sz[i] = 1;
}
}
int find(int v) {
if (v == par[v]) {
return v;
}
return par[v] = find(par[v]);
}
bool unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (sz[b] > sz[a]) swap(a, b);
par[b] = a;
sz[a] += sz[b];
return true;
}
};
int dist[nax][nax];
int get_dist(int a, int b) {
if (dist[a][b] != 0) {
return dist[a][b];
}
return dist[a][b] = get_distance(a, b);
}
void brute(int N, int* A, int* B, int* W) {
vector<array<int, 3>> d;
for(int i = 1; i <= N; i++) {
for(int j = i+1; j <= N; j++) {
get_dist(i, j);
d.push_back({dist[i][j], i, j});
}
}
sort(d.begin(), d.end());
Dsu dsu;
int idx = 0;
for(auto& i : d) {
if (dsu.unite(i[1], i[2])) {
A[idx] = i[1];
B[idx] = i[2];
W[idx] = i[0];
idx++;
}
}
}
void find_roads(int N, int Q, int A[], int B[], int W[]) {
if (Q == 500000) {
brute(N, A, B, W);
return;
}
vector<array<int, 2>> d;
for(int i = 2; i <= N; i++) {
d.push_back({get_dist(1, i), i});
}
sort(d.begin(), d.end());
A[0] = 1, B[0] = d[0][1], W[0] = d[0][0];
if (N == 2) return;
if (get_dist(1, d[1][1]) == W[0] + get_dist(B[0], d[1][1])) {
// line
for(int i = 1; i < N-1; i++) {
A[i] = B[i-1];
B[i] = d[i][1];
W[i] = get_dist(A[i], B[i]);
//cout << A[i] << " " << B[i] << " " << W[i] << "\n";
}
return;
}
int pirmas = B[0], antras = d[1][1];
A[1] = 1;
B[1] = antras;
W[1] = get_dist(A[1], B[1]);
for(int i = 2; i < N; i++) {
if (d[i][0] == get_dist(1, pirmas) + get_dist(pirmas, d[i][1])) {
A[i] = pirmas;
B[i] = d[i][1];
pirmas = B[i];
}
else {
A[i] = antras;
B[i] = d[i][1];
antras = B[i];
}
W[i] = get_dist(A[i], B[i]);
}
//for(auto& i : d) {
//cout << i[0] << " " << i[1] << "\n";
//}
return;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
10336 KB |
Correct: 498501 out of 500000 queries used. |
2 |
Correct |
102 ms |
10300 KB |
Correct: 499500 out of 500000 queries used. |
3 |
Correct |
100 ms |
10176 KB |
Correct: 492528 out of 500000 queries used. |
4 |
Correct |
102 ms |
10248 KB |
Correct: 494515 out of 500000 queries used. |
5 |
Correct |
108 ms |
10296 KB |
Correct: 498501 out of 500000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
10336 KB |
Correct: 498501 out of 500000 queries used. |
2 |
Correct |
102 ms |
10300 KB |
Correct: 499500 out of 500000 queries used. |
3 |
Correct |
100 ms |
10176 KB |
Correct: 492528 out of 500000 queries used. |
4 |
Correct |
102 ms |
10248 KB |
Correct: 494515 out of 500000 queries used. |
5 |
Correct |
108 ms |
10296 KB |
Correct: 498501 out of 500000 queries used. |
6 |
Incorrect |
82 ms |
10304 KB |
Reported list of edges differ from actual. |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
3796 KB |
get_distance() arguments out of range. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
3796 KB |
get_distance() arguments out of range. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
10336 KB |
Correct: 498501 out of 500000 queries used. |
2 |
Correct |
102 ms |
10300 KB |
Correct: 499500 out of 500000 queries used. |
3 |
Correct |
100 ms |
10176 KB |
Correct: 492528 out of 500000 queries used. |
4 |
Correct |
102 ms |
10248 KB |
Correct: 494515 out of 500000 queries used. |
5 |
Correct |
108 ms |
10296 KB |
Correct: 498501 out of 500000 queries used. |
6 |
Incorrect |
82 ms |
10304 KB |
Reported list of edges differ from actual. |
7 |
Halted |
0 ms |
0 KB |
- |