#include<bits/stdc++.h>
#include "citymapping.h"
using namespace std;
const int N = 1e3 + 1;
long long dist[N][N];
int n;
int par[N];
long long gd(int x,int y){
if(dist[x][y] != -1) return dist[x][y];
return dist[x][y] = dist[y][x] = get_distance(x + 1,y + 1);
}
bool inpath(int a,int b, int c){// check if c lies on a - b
return gd(a, c) + gd(c, b) == gd(a, b);
}
void dfs(int r,int p, set<int> temp){
par[r] = p;
if(temp.size() == 0)
return;
vector<pair<int,int>> tt;
for(int j : temp){
tt.push_back({gd(r, j), j});
}
sort(tt.begin(),tt.end());
while(tt.size() > 3)
tt.pop_back();
while(tt.size() > 1 && inpath(r, tt[1].second, tt[0].second)){
tt.erase(tt.begin() + 1);
}
for(int al = 0;al<2;al++){
if(tt.size() > 2 && inpath(r, tt[2].second, tt[al].second)){
tt.erase(tt.begin() + 2);
}
}
for(int al = 0;al<tt.size();al++){
temp.erase(temp.find(tt[al].second));
}
// Only children are in tt
vector<set<int>> hold(tt.size());
for(auto e : temp){
for(int i = 0;i<tt.size();i++){
if(inpath(r, e , tt[i].second)){
hold[i].insert(e);
break;
}
}
}
for(int i = 0;i<tt.size();i++){
dfs(tt[i].second, r , hold[i]);
}
}
void find_roads(int N, int Q, int A[], int B[], int W[]) {
n = N;
for(int i = 0;i<N;i++){
for(int j = 0;j<N;j++){
dist[i][j] = -1;
}
}
int r = 0;
set<int> temp;
for(int i = 0;i<n;i++){
if(r == i)continue;
temp.insert(i);
}
// vector<array<int,3>> edges;
dfs(r, -1, temp);
vector<array<int,3>> edges;
int p1 = 0;
for(int i = 0;i<n;i++){
if(i != r){
edges.push_back({i + 1, par[i] + 1, (int)gd(i,par[i])});
}
}
for(int i = 0;i<n-1;i++){
A[i] = edges[i][0], B[i] = edges[i][1], W[i] = edges[i][2];
}
return;
}
Compilation message
citymapping.cpp: In function 'void dfs(int, int, std::set<int>)':
citymapping.cpp:34:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int al = 0;al<tt.size();al++){
| ~~^~~~~~~~~~
citymapping.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0;i<tt.size();i++){
| ~^~~~~~~~~~
citymapping.cpp:47:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i = 0;i<tt.size();i++){
| ~^~~~~~~~~~
citymapping.cpp: In function 'void find_roads(int, int, int*, int*, int*)':
citymapping.cpp:67:6: warning: unused variable 'p1' [-Wunused-variable]
67 | int p1 = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
23500 KB |
Correct: 251481 out of 500000 queries used. |
2 |
Correct |
92 ms |
29072 KB |
Correct: 308832 out of 500000 queries used. |
3 |
Correct |
11 ms |
9440 KB |
Correct: 27160 out of 500000 queries used. |
4 |
Correct |
10 ms |
9172 KB |
Correct: 21473 out of 500000 queries used. |
5 |
Correct |
19 ms |
10332 KB |
Correct: 43091 out of 500000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
23500 KB |
Correct: 251481 out of 500000 queries used. |
2 |
Correct |
92 ms |
29072 KB |
Correct: 308832 out of 500000 queries used. |
3 |
Correct |
11 ms |
9440 KB |
Correct: 27160 out of 500000 queries used. |
4 |
Correct |
10 ms |
9172 KB |
Correct: 21473 out of 500000 queries used. |
5 |
Correct |
19 ms |
10332 KB |
Correct: 43091 out of 500000 queries used. |
6 |
Incorrect |
7 ms |
8588 KB |
Reported list of edges differ from actual. |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
9044 KB |
Too many calls to get_distance(). |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
9044 KB |
Too many calls to get_distance(). |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
23500 KB |
Correct: 251481 out of 500000 queries used. |
2 |
Correct |
92 ms |
29072 KB |
Correct: 308832 out of 500000 queries used. |
3 |
Correct |
11 ms |
9440 KB |
Correct: 27160 out of 500000 queries used. |
4 |
Correct |
10 ms |
9172 KB |
Correct: 21473 out of 500000 queries used. |
5 |
Correct |
19 ms |
10332 KB |
Correct: 43091 out of 500000 queries used. |
6 |
Incorrect |
7 ms |
8588 KB |
Reported list of edges differ from actual. |
7 |
Halted |
0 ms |
0 KB |
- |