//Never stop trying
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
const int MOD = 1e9 + 7; //998244353
const ll INF = 2e9;
const int MX = 200 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
ll random(ll a, ll b){
return a + rng() % (b - a + 1);
}
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl;
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
#ifndef LOCAL
#include "towns.h"
#endif
int NN,N;
#ifdef LOCAL
vpi adj[MX];
int dis[MX];
void dfs2(int u){
for(auto v: adj[u]){
if(dis[v.fi]==-1){
dis[v.fi]=dis[u]+v.se;
dfs2(v.fi);
}
}
}
int getDistance(int u, int v){
FOR(i,0,NN) dis[i]=-1;
dis[u]=0;
dfs2(u);
return dis[v];
}
#endif
int U,V,dist[MX][MX],alpha[MX],depth[MX];
bool comp(int u, int v){ //1: same
if(u==U || v==U) return 0;
return getDistance(u,v) != depth[u] + depth[v];
}
//Fisher and Salzberg's algo
bool check(vi a){
vi list, bucket;
FOR(i,0,sz(a)){
if(list.empty() || !comp(list.back(),a[i])){
list.pb(a[i]);
if(!bucket.empty()){
list.pb(bucket.back());
bucket.pop_back();
}
}
else bucket.pb(a[i]);
}
int T=list.back();
while(!list.empty()){
if(comp(T,list.back())){
if(sz(list)>=2) list.pop_back(),list.pop_back();
else bucket.pb(list.back()),list.pop_back();
}
else{
list.pop_back();
if(!sz(bucket)) break;
bucket.pop_back();
}
}
return (!bucket.empty());
}
int hubDistance(int N, int sub){
::N=N;
memset(dist,-1,sizeof(dist));
FOR(i,0,N) dist[i][i]=0;
int diam=0;
FOR(i,1,N){
dist[0][i]=dist[i][0]=getDistance(0,i);
if(ckmax(diam,dist[0][i])) U=i;
}
diam=0;
FOR(i,0,N){
if(i!=0 && i!=U) dist[U][i]=dist[i][U]=getDistance(U,i);
if(ckmax(diam,dist[U][i])) V=i;
}
vi vec;
map<int,vi>mp;
int R=INF;
FOR(i,0,N){
alpha[i]=(dist[0][U]+dist[i][U]-dist[0][i])/2;
depth[i]=(-dist[0][U]+dist[i][U]+dist[0][i])/2;
vec.pb(alpha[i]);
mp[alpha[i]].pb(i);
ckmin(R,max(alpha[i],diam-alpha[i]));
}
sort(all(vec));
int hub=-1,pref=0;
for(auto x: vec){
if(max(x,diam-x)==R && pref<=N/2 && N-pref-sz(mp[x])<=N/2)
hub=x;
pref+=sz(mp[x]);
}
if(hub==-1) return -R;
vi subt; subt.assign(all(mp[hub]));
while(sz(subt)<N) subt.pb(U);
if(check(subt)) return -R;
return R;
}
#ifdef LOCAL
int main() {
boost; IO();
cin>>NN>>N;
FOR(i,0,NN-1){
int u,v,w; cin>>u>>v>>w;
adj[u].eb(v,w);
adj[v].eb(u,w);
}
//cout << getDistance(3,7) << endl;
cout << hubDistance(N,0) << endl;
return 0;
}
#endif
/* Careful!!!
.Array bounds
.Infinite loops
.Uninitialized variables / empty containers
.Multisets are shit
Some insights:
.Binary search
.Graph representation
.Write brute force code
.Change your approach
*/
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:127:31: warning: declaration of 'N' shadows a global declaration [-Wshadow]
127 | int hubDistance(int N, int sub){
| ^
towns.cpp:69:8: note: shadowed declaration is here
69 | int NN,N;
| ^
towns.cpp:127:28: warning: unused parameter 'sub' [-Wunused-parameter]
127 | int hubDistance(int N, int sub){
| ~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
492 KB |
Output is correct |
2 |
Correct |
15 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
20 ms |
492 KB |
Output is correct |
5 |
Correct |
20 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
492 KB |
Output is correct |
2 |
Correct |
15 ms |
492 KB |
Output is correct |
3 |
Correct |
20 ms |
492 KB |
Output is correct |
4 |
Correct |
22 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
19 ms |
492 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |