/*
* @Author: hungeazy
* @Date: 2024-09-06 23:05:29
* @Last Modified by: hungeazy
* @Last Modified time: 2024-09-06 23:17:33
*/
#include <bits/stdc++.h>
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define int long long
#define ull unsigned long long
#define sz(x) x.size()
#define sqr(x) (1LL * (x) * (x))
#define all(x) x.begin(), x.end()
#define fill(f,x) memset(f,x,sizeof(f))
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define FOD(i,r,l) for(int i=r;i>=l;i--)
#define ii pair<int,int>
#define iii pair<int,ii>
#define di pair<ii,ii>
#define vi vector<int>
#define vii vector<ii>
#define mii map<int,int>
#define fi first
#define se second
#define pb push_back
#define MOD 1000000007
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define MASK(i) (1LL << (i))
#define c_bit(i) __builtin_popcountll(i)
#define BIT(x,i) ((x) & MASK(i))
#define SET_ON(x,i) ((x) | MASK(i))
#define SET_OFF(x,i) ((x) & ~MASK(i))
#define oo 1e18
#define name ""
#define endl '\n'
#define time() cerr << endl << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms.";
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
const int N = (int)1e6+10;
int n,m,a,b;
vi g[N];
namespace hungeazy {
int par[N],dp[N],ans=oo,del;
void build(int u, int p)
{
for (int v : g[u])
if (v != p)
{
par[v] = u;
build(v,u);
}
}
void DFS(int u, int p)
{
dp[u] = 0;
priority_queue<int> q;
for (int v : g[u])
if (v != p and v != del)
{
DFS(v,u);
q.push(dp[v]);
}
int cnt = 1;
while (!q.empty())
{
int x = q.top();
maximize(dp[u],cnt+x);
q.pop();
++cnt;
}
}
bool check(int u, int v)
{
del = v;
DFS(a,-1);
del = u;
DFS(b,-1);
minimize(ans,max(dp[a],dp[b]));
return dp[u] > dp[v];
}
void solve(void)
{
build(a,-1);
if (a == b)
{
DFS(a,-1);
cout << dp[a];
return;
}
vi vec;
int tmp = b;
while (tmp != a)
{
vec.pb(tmp);
tmp = par[tmp];
}
if (n <= 1e3)
{
for (int x : vec) check(par[x],x);
cout << ans;
return;
}
int l = 0, r = vec.size()-1;
while (l <= r)
{
int mid = (l+r)>>1;
if (check(par[vec[mid]],vec[mid])) l = mid+1;
else r = mid-1;
}
cout << ans;
}
}
signed main()
{
fast;
if (fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
cin >> n;
FOR(i,1,n-1)
{
int u,v;
cin >> u >> v;
g[u].pb(v); g[v].pb(u);
}
cin >> m;
if (m == 1) cin >> a, b = a;
else cin >> a >> b;
hungeazy::solve();
time();
return 0;
}
// ██░ ██ █ ██ ███▄ █ ▄████
//▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
//▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
//░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
//░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
// ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
// ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
// ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
// ░ ░ ░ ░ ░ ░
Compilation message
torrent.cpp: In function 'int main()':
torrent.cpp:132:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:133:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
369 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2056 ms |
429712 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |