답안 #710693

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710693 2023-03-15T15:33:40 Z aVe Papričice (COCI20_papricice) C++14
50 / 110
1000 ms 25616 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
#define se second
#define fi first
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
ll set_on(ll n, ll k){
	return (n |= 1 << k);
}
ll set_off(ll n, ll k){
	return (n &= ~(1UL << k));
}
bool check_bit(ll n, ll k){
	int bit = (n >> k) & 1U;
	if(bit == 1)
		return true;
	return false;
}
int n;
vi G[200100];
int ans = 1e9, sum[200100], depth[200100];
vi segtree, euler, first;
vector<bool> vis;
void dfs2(int node) {
        vis[node] = true;
        first[node] = euler.size();
        euler.push_back(node);
        for (auto to : G[node]) {
            if (!vis[to]) {
                dfs2(to);
                euler.push_back(node);
            }
        }
    }
void build(int node, int b, int e) {
        if (b == e) {
            segtree[node] = euler[b];
        } else {
            int mid = (b + e) / 2;
            build(node << 1, b, mid);
            build(node << 1 | 1, mid + 1, e);
            int l = segtree[node << 1], r = segtree[node << 1 | 1];
            segtree[node] = (depth[l] <depth[r]) ? l : r;
        }
}
void LCA(int root = 0) {
        first.resize(n);
        euler.reserve(n * 2);
        vis.assign(n, false);
        dfs2(root);
        int m = euler.size();
        segtree.resize(m * 4);
        build(1, 0, m - 1);
    }
int query(int node, int b, int e, int L, int R) {
        if (b > R || e < L)
            return -1;
        if (b >= L && e <= R)
            return segtree[node];
        int mid = (b + e) >> 1;

        int left = query(node << 1, b, mid, L, R);
        int right = query(node << 1 | 1, mid + 1, e, L, R);
        if (left == -1) return right;
        if (right == -1) return left;
        return depth[left] < depth[right] ? left : right;
    }
int lca(int u, int v) {
        int left = first[u], right = first[v];
        if (left > right)
            swap(left, right);
        return query(1, 0, euler.size() - 1, left, right);
}
void get_depth(int at, int prev, int dep){
	depth[at] = dep;
	for(auto next : G[at]){
		if(next == prev)	continue;
		get_depth(next, at, dep + 1);
	}
}
void dfs(int at, int prev){
	sum[at] = 1;
	for(auto next : G[at]){
		if(next == prev)	continue;
		dfs(next, at);
		sum[at] += sum[next];
	}
}
bool comp(int& A, int& B){
	if(depth[A] >= depth[B])	return false;
	return true;
}
void solve(){
	cin>>n;
	vector<pii> edge;
	for(int i = 1; i < n; i++){
		int a, b;
		cin>>a>>b;
		a--; b--;
		G[a].pb(b);
		G[b].pb(a);
		edge.pb(mp(a,b));
	}
	get_depth(0, -1, 0);
	LCA();
	for(int i = 0; i < edge.size(); i++){
		if(depth[edge[i].se] < depth[edge[i].fi]){
			swap(edge[i].fi, edge[i].se);
		}
	}
	dfs(0, -1);
	for(int i = 0; i < edge.size(); i++){
		for(int j = i + 1; j < edge.size(); j++){
			int a = sum[0];
			int b= sum[edge[i].se];
			int c = sum[edge[j].se];
			int father = lca(edge[i].se, edge[j].se);
			if(father == edge[i].se){
				a -= b;
				b -= c;
			}
			else if(father == edge[j].se){
				a -= c;
				c -= b;
			}
			else{
				a-=b;
				a-=c;
			}
			int maks = max(a, max(b,c));
			int mins = min(a, min(b,c));
			ans = min(ans, maks - mins);
		}
	}
	cout<<ans<<endl;
	
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.precision(10);
	cout<<fixed;
	startTime = clock();
	int t=1;
//	cin>>t;
	while(t--)
		solve();

	return 0;
}




Compilation message

papricice.cpp: In function 'void solve()':
papricice.cpp:141:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |  for(int i = 0; i < edge.size(); i++){
      |                 ~~^~~~~~~~~~~~~
papricice.cpp:147:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |  for(int i = 0; i < edge.size(); i++){
      |                 ~~^~~~~~~~~~~~~
papricice.cpp:148:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |   for(int j = i + 1; j < edge.size(); j++){
      |                      ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4948 KB Output is correct
2 Correct 7 ms 4948 KB Output is correct
3 Correct 7 ms 4948 KB Output is correct
4 Correct 6 ms 4948 KB Output is correct
5 Correct 6 ms 5052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4948 KB Output is correct
2 Correct 7 ms 4948 KB Output is correct
3 Correct 7 ms 4948 KB Output is correct
4 Correct 6 ms 4948 KB Output is correct
5 Correct 6 ms 5052 KB Output is correct
6 Correct 440 ms 5204 KB Output is correct
7 Correct 464 ms 5324 KB Output is correct
8 Correct 423 ms 5328 KB Output is correct
9 Correct 418 ms 5228 KB Output is correct
10 Correct 459 ms 5220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4948 KB Output is correct
2 Correct 7 ms 4948 KB Output is correct
3 Correct 7 ms 4948 KB Output is correct
4 Correct 6 ms 4948 KB Output is correct
5 Correct 6 ms 5052 KB Output is correct
6 Correct 440 ms 5204 KB Output is correct
7 Correct 464 ms 5324 KB Output is correct
8 Correct 423 ms 5328 KB Output is correct
9 Correct 418 ms 5228 KB Output is correct
10 Correct 459 ms 5220 KB Output is correct
11 Execution timed out 1069 ms 25616 KB Time limit exceeded
12 Halted 0 ms 0 KB -