struct TreeDiameter{
vector<vector<PII>> G;
ll N;
ll start,end;
VI path;
ll d;
VI TO;
TreeDiameter(ll n){
N=n;
G.resize(N);
TO.resize(N, -1);
}
void add_edge(ll a, ll b, ll cost){
G[a].push_back({b,cost});
G[b].push_back({a,cost});
}
void calc(){
auto pa = dfs(0, -1);
ll farthest_index = pa.second;
pa = dfs(farthest_index, -1);
d = pa.first;
start=farthest_index;
end=pa.second;
}
PII dfs(ll i, ll prev){
PII ret = {0,i};
for(auto pa : G[i]){
ll to = pa.first;
ll cost = pa.second;
if(to==prev)continue;
auto result = dfs(to, i);
if(result.first + cost > ret.first){
ret.first = result.first + cost;
ret.second = result.second;
TO[i]=to;
}
}
return ret;
}
VI get_path(){
VI V;
V.push_back(start);
ll now=start;
while(now!=end){
ll to = TO[now];
V.push_back(to);
now = to;
}
return V;
}
};
他の問題もACしておく
- C - Removing Coins
- 003-Longest Circular Road(★4)