This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "citymapping.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
using namespace std;
using lint = long long;
using pi = pair<lint, lint>;
const int MAXN = 1005;
 
lint mem[MAXN][MAXN];
 
lint dist(int x, int y){
	if(x == y) return 0;
	if(mem[x][y]) return mem[x][y];
	return mem[x][y] = mem[y][x] = get_distance(x, y);
}
 
vector<pi> gph[MAXN];
lint dep[MAXN];
bool vis[MAXN];
int sz[MAXN], msz[MAXN], par[MAXN];
 
void dfs(int x, int p){
	sz[x] = 1;
	msz[x] = 0;
	for(auto &i : gph[x]){
		if(!vis[i.second] && i.second != p){
			dfs(i.second, x);
			sz[x] += sz[i.second];
			msz[x] = max(msz[x], sz[i.second]);
		}
	}
}
 
void find_roads(int N, int Q, int A[], int B[], int W[]) {
	auto make_edge = [&](int s, int e, int x){
//		printf("make_edge(%d, %d, %d)\n", s, e, x);
		gph[s].emplace_back(x, e);
		gph[e].emplace_back(x, s);
		for(int i=0; i<N-1; i++){
			if(!W[i]){
				tie(A[i], B[i], W[i]) = make_tuple(s, e, x);
				break;
			}
		}
	};
	vector<pi> v;
	for(int i=2; i<=N; i++){
		dep[i] = dist(1, i);
		v.emplace_back(dep[i], i);
	}
	sort(v.begin(), v.end());
	vector<int> prv = {1};
	for(auto &i : v){
		if(sz(prv) == 1){
			par[i.second] = 1;
			make_edge(1, i.second, i.first);
			prv.push_back(i.second);
			continue;
		}
		memset(vis, 0, sizeof(vis));
		int curpos = 1;
		pi lowest(0, 1);
		dfs(1, -1);
		while(true){
			int L = curpos;
			while(true){
				pi ans(0, 0);
				for(auto &j : gph[L]){
					if(j.second != par[L]) ans = max(ans, pi(sz[j.second], j.second));
				}
				if(ans.second == 0) break;
				L = ans.second;
			}
			lint d1 = dep[L];
			lint d2 = dist(L, i.second);
			lint d3 = dep[i.second];
			lint branch = (d1 + d2 - d3) / 2;
			int pL = -1;
			bool found_vis = vis[L];
			while(d1 - branch < dep[L]){
				pL = L;
				L = par[L];
				if(vis[L]) found_vis = 1;
			}
			if(found_vis) break;
			vis[L] = 1;
			vector<int> sons;
			for(auto &j : gph[L]){
				if(j.second != par[L] && j.second != pL){
					sons.push_back(j.second);
				}
			}
			lowest = max(lowest, pi(dep[L], L));
			assert(sz(sons) <= 2);
			if(sz(sons) == 0) break;
			if(sz(sons) == 1){
				curpos = sons[0];
			}
			else{
				if(dep[sons[0]] + dist(sons[0], i.second) == dep[i.second]){
					curpos = sons[0];
				}
				else curpos = sons[1];
			}
		}
		make_edge(lowest.second, i.second, dist(lowest.second, i.second));
		par[i.second] = lowest.second;
		prv.push_back(i.second);
	}
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |