Submission #1286207

#TimeUsernameProblemLanguageResultExecution timeMemory
1286207samarthkulkarniHard route (IZhO17_road)C++20
0 / 100
1 ms576 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// #pragma GCC target ("avx2")
// #pragma GCC optimize ("O3")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC optimize("fast-math")

#define ll long long
#define ld long double
#define vi vector<ll>
#define endl "\n"
#define pr pair<ll, ll>
#define ff first 
#define ss second
#define all(x) x.begin(), x.end()
const int mod = 1e9+7;
void _p(ll a){cout<<a<<endl;}
void _p(string a){cout<<a<<endl;}
void _p(ld a) {cout<<a<<endl;}
template <class T>
void _p(vector<T> a){for(T val:a)cout<<val<<" ";cout<<endl;}
#define debug(x) cout<<#x<<" -> ";_p(x)

typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > ordered_set;

vector<pr> move8 = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
vector<pr> move4 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<pr> move2 = {{0, 1}, {1, 0}};



void solution();
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solution();
    return 0;
}


const int MAXN = 110;
const int MAXK = log2(MAXN)+1;
vi adj[MAXN];
ll binaryLifting[MAXN][MAXK];
ll parent[MAXN];
vi leaf;
ll depth[MAXN];
ll root;


void dfs(ll a, ll p = -1, ll d = 0) {
	depth[a] = d;
	for (auto b : adj[a]) {
		if (b == p) continue;
		parent[b] = a;
		dfs(b, a, d+1);
	}

	if (adj[a].size() == 1) leaf.push_back(a);
}


ll lca(ll a, ll b) {
	if (depth[a] < depth[b]) swap(a, b);
	ll k = depth[a] - depth[b];
	for (int i = MAXK-1; i >= 0; i--) {
		if (k & (1 << i)) a = binaryLifting[a][i];
	}
	if (a == b) return a;

	for (int i = MAXK-1; i >= 0; i--) {
		if (binaryLifting[a][i] != binaryLifting[b][i]) {
			a = binaryLifting[a][i];
			b = binaryLifting[b][i];
		}
	}
	return parent[a];
}
ll dist(ll a, ll b) {
	ll k = lca(a, b);
	return depth[a] + depth[b] - 2*depth[k];
}

void solution() {
    ll n; cin >> n;
    for (int i = 1; i < n ;i++) {
    	ll a, b; cin >> a >> b;
    	adj[a].push_back(b);
    	adj[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
    	if (adj[i].size() >= 2) {root = i; break;}
    }

    dfs(root);
    parent[root] = root;
    ll N = leaf.size();

    if (N == 2) {
    	cout << 0 << " " << 1 << endl;
    	return;
    }	

    for (int j = 0; j < MAXK; j++) {
    	for (int i = 1; i <= n; i++) {
    		if (j == 0) {binaryLifting[i][j] = parent[i]; continue;}
    		binaryLifting[i][j] = binaryLifting[binaryLifting[i][j-1]][j-1];
    	}
    }


    map<ll, ll> ans;

    for (int i = 0; i < N; i++) {
    	for (int j = i+1; j < N; j++) {
    		ll x = leaf[i], z = leaf[j];
    		ll a = dist(x, z);

    		ll D = 0;
    		for (int k = 0; k < N; k++) {
    			if (k == j || k == i) continue;
    			ll y = leaf[k];

    			ll b;
    			
    			if (lca(x, y) == root && lca(y, z) == root) {
    				b = dist(lca(x, z), y);
    			} else {
    				b = min( dist(lca(x, y), y), dist(lca(y, z), y) );
    			}

    			D = max(D, a*b);
    		}

    		ans[D]++;
    	}
    }



    cout << ans.rbegin()->ff << " " << ans.rbegin()->ss << endl;
    
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...