이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |