#include "citymapping.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define endl "\n"
const double PI=3.141592653589;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
vector<int>sub,in,out;
vector<vector<int>>adj;
int t = 1;
void comp(int u, int p,set<int>&active){
sub[u] = 1;
in[u] = t++;
for(int v : adj[u]){
if(v==p || !active.count(v))continue;
comp(v,u,active);
sub[u]+=sub[v];
}
out[u] = t++;
}
bool above(int u, int v){
return in[u] <= in[v] && out[u] >= out[v];
}
void find_roads(int n, int Q, int a[], int b[], int w[]) {
vector<pair<ll,int>>k;
vector<ll>dist1(n+1);
for(int i = 2;i<=n;++i)dist1[i] = get_distance(1,i);
for(int i = 2;i<=n;++i)k.pb({dist1[i],i});
sort(all(k));
adj.resize(n+1);
sub.resize(n+1);
in.resize(n+1);
out.resize(n+1);
set<int>active = {1};
int p = 0;
for(int i = 0;i<k.size();++i){
int prev = 1,last = 0;
while(active.size()>1){
sub = vector<int>(n+1);
in = vector<int>(n+1);
out = vector<int>(n+1);
t = 1;
comp(prev,-1,active);
int sz = active.size();
int mn = 1e9, x = 1;
for(auto w : active){
if(abs((sz/2)-sub[w]) < mn){
mn = abs((sz/2)-sub[w]);
x = w;
}
}
int u = k[i].second;
set<int>ne;
int kkk = get_distance(x,u);
last = kkk;
if(dist1[x]+kkk == dist1[u]){
// all nodes in subtree of x are valid
prev = x;
for(auto y : active){
if(above(x,y))ne.insert(y);
}
}else{
// all nodes not in subtree of x are valid
prev = 1;
for(auto y : active){
if(!above(x,y))ne.insert(y);
}
}
active = ne;
}
int v = *active.begin();
adj[k[i].second].pb(v);
adj[v].pb(k[i].second);
a[p] = k[i].second;
b[p] = v;
w[p++] = get_distance(k[i].second,v);
for(int j = 0;j<=i;++j)active.insert(k[j].second);
active.insert(1);
}
return;
}
Compilation message
citymapping.cpp: In function 'void find_roads(int, int, int*, int*, int*)':
citymapping.cpp:68: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]
68 | for(int i = 0;i<k.size();++i){
| ~^~~~~~~~~
citymapping.cpp:69:16: warning: variable 'last' set but not used [-Wunused-but-set-variable]
69 | int prev = 1,last = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
226 ms |
568 KB |
Correct: 9971 out of 500000 queries used. |
2 |
Incorrect |
2 ms |
468 KB |
get_distance() arguments out of range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
226 ms |
568 KB |
Correct: 9971 out of 500000 queries used. |
2 |
Incorrect |
2 ms |
468 KB |
get_distance() arguments out of range. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
233 ms |
748 KB |
Correct: 9882 out of 12000 queries used. |
2 |
Correct |
237 ms |
576 KB |
Correct: 9903 out of 12000 queries used. |
3 |
Correct |
226 ms |
744 KB |
Correct: 9981 out of 12000 queries used. |
4 |
Correct |
215 ms |
588 KB |
Correct: 9905 out of 12000 queries used. |
5 |
Correct |
212 ms |
584 KB |
Correct: 9879 out of 12000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
233 ms |
748 KB |
Correct: 9882 out of 12000 queries used. |
2 |
Correct |
237 ms |
576 KB |
Correct: 9903 out of 12000 queries used. |
3 |
Correct |
226 ms |
744 KB |
Correct: 9981 out of 12000 queries used. |
4 |
Correct |
215 ms |
588 KB |
Correct: 9905 out of 12000 queries used. |
5 |
Correct |
212 ms |
584 KB |
Correct: 9879 out of 12000 queries used. |
6 |
Incorrect |
227 ms |
560 KB |
Too many calls to get_distance(). |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
226 ms |
568 KB |
Correct: 9971 out of 500000 queries used. |
2 |
Incorrect |
2 ms |
468 KB |
get_distance() arguments out of range. |
3 |
Halted |
0 ms |
0 KB |
- |