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 <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
set<int> adj[100005];
vector<pii> edges;
int a, b;
bool found = false;
int dist[100005];
int ans = 100000000;
int temp = 0;
void dfs1(int v, int p){
if(v == b){
found = true;
return;
}
for(int u : adj[v]){
if(u != p){
edges.pb(mp(v, u));
dfs1(u, v);
if(found)return;
edges.pop_back();
}
}
}
void dfs2(int v, int p){
//cuut << v << " " << dist[v] << "\n";
temp = max(temp, dist[v]);
int add = 1;
for(int u : adj[v]){
if(u == p)continue;
dist[u] = dist[v] + add;
dfs2(u,v);
add++;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n >> a >> b;
a--; b--;
for(int i = 0; i < n-1; i++){
int x, y;
cin >> x >> y;
x--; y--;
adj[x].insert(y);
adj[y].insert(x);
}
dfs1(a, -1);
for(pii u : edges){
//cout << u.f << " " << u.s << "\n";
//erase this edge
adj[u.f].erase(u.s);
adj[u.s].erase(u.f);
//do stuff
dfs2(a, -1);
dfs2(b, -1);
ans = min(ans, temp);
//add it back
temp = 0;
memset(dist, 0,sizeof(dist));
adj[u.f].insert(u.s);
adj[u.s].insert(u.f);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |