Submission #1084667

# Submission time Handle Problem Language Result Execution time Memory
1084667 2024-09-06T16:18:45 Z hungeazy Torrent (COI16_torrent) C++17
100 / 100
274 ms 51416 KB
/*
* @Author: hungeazy
* @Date:   2024-09-06 23:05:29
* @Last Modified by:   hungeazy
* @Last Modified time: 2024-09-06 23:18:23
*/
#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 >> a >> b;
    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);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23896 KB Output is correct
2 Correct 14 ms 23900 KB Output is correct
3 Correct 15 ms 23900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 244 ms 42168 KB Output is correct
2 Correct 226 ms 47820 KB Output is correct
3 Correct 248 ms 51144 KB Output is correct
4 Correct 263 ms 48340 KB Output is correct
5 Correct 274 ms 45904 KB Output is correct
6 Correct 250 ms 46380 KB Output is correct
7 Correct 232 ms 51416 KB Output is correct