#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];
long long temp = get_distance(x + 1, y + 1);
dist[x][y] = temp;
dist[y][x] = temp;
return temp;
}
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<long long,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:37:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int al = 0;al<tt.size();al++){
| ~~^~~~~~~~~~
citymapping.cpp:43:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 0;i<tt.size();i++){
| ~^~~~~~~~~~
citymapping.cpp:50:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i = 0;i<tt.size();i++){
| ~^~~~~~~~~~
citymapping.cpp: In function 'void find_roads(int, int, int*, int*, int*)':
citymapping.cpp:70:6: warning: unused variable 'p1' [-Wunused-variable]
70 | int p1 = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
25140 KB |
Correct: 251481 out of 500000 queries used. |
2 |
Correct |
97 ms |
31076 KB |
Correct: 308832 out of 500000 queries used. |
3 |
Correct |
12 ms |
9556 KB |
Correct: 27160 out of 500000 queries used. |
4 |
Correct |
13 ms |
9172 KB |
Correct: 21473 out of 500000 queries used. |
5 |
Correct |
19 ms |
10580 KB |
Correct: 43091 out of 500000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
25140 KB |
Correct: 251481 out of 500000 queries used. |
2 |
Correct |
97 ms |
31076 KB |
Correct: 308832 out of 500000 queries used. |
3 |
Correct |
12 ms |
9556 KB |
Correct: 27160 out of 500000 queries used. |
4 |
Correct |
13 ms |
9172 KB |
Correct: 21473 out of 500000 queries used. |
5 |
Correct |
19 ms |
10580 KB |
Correct: 43091 out of 500000 queries used. |
6 |
Correct |
187 ms |
63100 KB |
Correct: 479862 out of 500000 queries used. |
7 |
Incorrect |
98 ms |
33200 KB |
Reported list of edges differ from actual. |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
9172 KB |
Too many calls to get_distance(). |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
9172 KB |
Too many calls to get_distance(). |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
25140 KB |
Correct: 251481 out of 500000 queries used. |
2 |
Correct |
97 ms |
31076 KB |
Correct: 308832 out of 500000 queries used. |
3 |
Correct |
12 ms |
9556 KB |
Correct: 27160 out of 500000 queries used. |
4 |
Correct |
13 ms |
9172 KB |
Correct: 21473 out of 500000 queries used. |
5 |
Correct |
19 ms |
10580 KB |
Correct: 43091 out of 500000 queries used. |
6 |
Correct |
187 ms |
63100 KB |
Correct: 479862 out of 500000 queries used. |
7 |
Incorrect |
98 ms |
33200 KB |
Reported list of edges differ from actual. |
8 |
Halted |
0 ms |
0 KB |
- |