#include "citymapping.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e3+5;
ll dist[MAXN][MAXN];
vector<tuple<int, int, int> > ans;
vector<int> subtree[MAXN];
ll further[MAXN][2];
mt19937 rng(time(0));
int n;
ll query(int a, int b) {
if(a == b) return 0;
if(a > b) swap(a, b);
if(dist[a][b]) return dist[a][b];
// printf("query %d %d\n", a, b);
return dist[a][b] = get_distance(a, b);
}
void solve(int cur) {
if(subtree[cur].size() == 1) return;
int x = further[cur][0];
int y = rng()%subtree[cur].size();
while(subtree[cur][y] == x) y = rng()%subtree[cur].size();
// printf("solve %d >> %d %d\n", cur, x, y); for(auto vertex : subtree[cur]) printf("%d ", vertex); cout << endl;
vector<pair<ll, int> > line;
vector<pair<ll, int> > aux;
for(int vertex : subtree[cur]) {
ll val = query(x, vertex) + query(y, vertex) - query(x, y); val /= 2;
if(val == 0) line.push_back({query(x, vertex), vertex});
val = query(x, y) - query(vertex, y) + val;
aux.push_back({val, vertex});
}
sort(line.begin(), line.end());
sort(aux.begin(), aux.end());
subtree[cur].clear();
further[cur][0] = 0;
further[cur][1] = -1;
int p = 0;
for(int i = 0; i < line.size(); i++) {
int vertex = line[i].second;
ll dx = line[i].first;
while(p < aux.size() && aux[p].first == dx) {
ll val = query(x, aux[p].second) - dx;
if(val > further[vertex][1]) further[vertex][0] = aux[p].second, further[vertex][1] = val;
subtree[vertex].push_back(aux[p++].second);
}
if(i != 0) ans.push_back({vertex, line[i-1].second, line[i].first-line[i-1].first});
}
for(auto item : line) solve(item.second);
}
void find_roads(int N, int Q, int A[], int B[], int W[]) {
n = N;
for(int i = 1; i <= n; i++) subtree[n+1].push_back(i);
further[n+1][0] = rng()%(n+1)+1;
solve(n+1);
for(int i = 0; i < ans.size(); i++) tie(A[i], B[i], W[i]) = ans[i];
}
Compilation message
citymapping.cpp: In function 'void solve(int)':
citymapping.cpp:44:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < line.size(); i++) {
~~^~~~~~~~~~~~~
citymapping.cpp:47:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(p < aux.size() && aux[p].first == dx) {
~~^~~~~~~~~~~~
citymapping.cpp: In function 'void find_roads(int, int, int*, int*, int*)':
citymapping.cpp:65:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ans.size(); i++) tie(A[i], B[i], W[i]) = ans[i];
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2424 KB |
Correct: 3631 out of 500000 queries used. |
2 |
Incorrect |
9 ms |
4344 KB |
get_distance() arguments out of range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2424 KB |
Correct: 3631 out of 500000 queries used. |
2 |
Incorrect |
9 ms |
4344 KB |
get_distance() arguments out of range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
6264 KB |
Correct: 5009 out of 12000 queries used. |
2 |
Correct |
9 ms |
5112 KB |
Correct: 4326 out of 12000 queries used. |
3 |
Correct |
8 ms |
3832 KB |
Correct: 2783 out of 12000 queries used. |
4 |
Correct |
9 ms |
4728 KB |
Correct: 2211 out of 12000 queries used. |
5 |
Correct |
9 ms |
4472 KB |
Correct: 3717 out of 12000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
6264 KB |
Correct: 5009 out of 12000 queries used. |
2 |
Correct |
9 ms |
5112 KB |
Correct: 4326 out of 12000 queries used. |
3 |
Correct |
8 ms |
3832 KB |
Correct: 2783 out of 12000 queries used. |
4 |
Correct |
9 ms |
4728 KB |
Correct: 2211 out of 12000 queries used. |
5 |
Correct |
9 ms |
4472 KB |
Correct: 3717 out of 12000 queries used. |
6 |
Correct |
9 ms |
4600 KB |
Correct: 3661 out of 12000 queries used. |
7 |
Correct |
9 ms |
3960 KB |
Correct: 4105 out of 12000 queries used. |
8 |
Correct |
10 ms |
4472 KB |
Correct: 3073 out of 12000 queries used. |
9 |
Correct |
9 ms |
3576 KB |
Correct: 2225 out of 12000 queries used. |
10 |
Correct |
10 ms |
6008 KB |
Correct: 4351 out of 12000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2424 KB |
Correct: 3631 out of 500000 queries used. |
2 |
Incorrect |
9 ms |
4344 KB |
get_distance() arguments out of range. |
3 |
Halted |
0 ms |
0 KB |
- |