Submission #506029

# Submission time Handle Problem Language Result Execution time Memory
506029 2022-01-11T12:08:05 Z pragmatist Hard route (IZhO17_road) C++17
19 / 100
2000 ms 3148 KB
#include <bits/stdc++.h>                            
 
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define int long long
#define nl "\n"
 
using namespace std;

typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;

const int N = (int)1e5 + 7;
const int M = (int)7e6 + 7;
const ll MOD = (ll)1e9;                    
const int inf = (int)1e9 + 7;
const ll INF = (ll)3e18 + 7;

pii dir[] = {{-1, -1}, {1, 1}, {-1, 1}, {1, -1}};

int n, p[N], d[N], D[N];
vector<int> g[N];

void dfs(int v, int pr) {
	p[v] = pr;
	for(auto to : g[v]) {
		if(to != pr) {
			d[to] = d[v] + 1;
			dfs(to, v);
		}	
	}
}  

void DFS(int v, int pr) {
	for(auto to : g[v]) {
		if(to != pr) {
		    D[to] = D[v] + 1;
			DFS(to, v);
		}
	}	
}

void solve() { 
	cin >> n;
	for(int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	}
	int mx = 0, cnt = 0;
	vector<int> v;
	for(int i = 1; i <= n; ++i) {
		if(sz(g[i]) == 1) v.pb(i);
	}
	for(int i = 0; i < sz(v); ++i) {
		for(int j = i + 1; j < sz(v); ++j) {
			vector<int> pth;
			int a = v[i], b = v[j];
			d[a] = 0;
			dfs(a, a);
			int h = 0;
			int x = b;
			while(x != a) {
				pth.pb(x);
				x = p[x];	
			}
			pth.pb(a);
			fill(D + 1, D + 1 + n, 0);
			for(auto x : v) {
				if(x != a && x != b) {
					D[x] = 0;
					DFS(x, x);
					int cur = inf;   
					for(auto e : pth) cur = min(cur, D[e]); 
					h = max(h, cur);
				}
			}
			mx = max(mx, d[b] * h);
		}
	}
	for(int i = 0; i < sz(v); ++i) {
		for(int j = i + 1; j < sz(v); ++j) {
			vector<int> pth;
			int a = v[i], b = v[j];
			d[a] = 0;
			dfs(a, a);
			int h = 0;
			int x = b;
			while(x != a) {
				pth.pb(x);
				x = p[x];	
			}
			pth.pb(a);
			fill(D + 1, D + 1 + n, 0);
			for(auto x : v) {
				if(x != a && x != b) {
					D[x] = 0;
					DFS(x, x);
					int cur = inf;   
					for(auto e : pth) cur = min(cur, D[e]); 
					h = max(h, cur);
				}
			}
			if(d[b] * h == mx) cnt++;
		}
	}

	cout << mx << ' ' << cnt << nl;
}

signed main() {                   
	ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
   	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; ++i) {
        //cout << "Case " << i << ": ";
        solve();
    }
    return 0;
}
/*
3
3 1
2 3
1 1
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2668 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2672 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 17 ms 2684 KB Output is correct
8 Correct 10 ms 2680 KB Output is correct
9 Correct 18 ms 2636 KB Output is correct
10 Correct 15 ms 2684 KB Output is correct
11 Correct 89 ms 2672 KB Output is correct
12 Correct 91 ms 2660 KB Output is correct
13 Correct 90 ms 2664 KB Output is correct
14 Correct 88 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 1 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 1 ms 2660 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 9 ms 2636 KB Output is correct
24 Correct 709 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2668 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2672 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 17 ms 2684 KB Output is correct
8 Correct 10 ms 2680 KB Output is correct
9 Correct 18 ms 2636 KB Output is correct
10 Correct 15 ms 2684 KB Output is correct
11 Correct 89 ms 2672 KB Output is correct
12 Correct 91 ms 2660 KB Output is correct
13 Correct 90 ms 2664 KB Output is correct
14 Correct 88 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 1 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 1 ms 2660 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 9 ms 2636 KB Output is correct
24 Correct 709 ms 2668 KB Output is correct
25 Execution timed out 2073 ms 3148 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2668 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2672 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 17 ms 2684 KB Output is correct
8 Correct 10 ms 2680 KB Output is correct
9 Correct 18 ms 2636 KB Output is correct
10 Correct 15 ms 2684 KB Output is correct
11 Correct 89 ms 2672 KB Output is correct
12 Correct 91 ms 2660 KB Output is correct
13 Correct 90 ms 2664 KB Output is correct
14 Correct 88 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 1 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 1 ms 2660 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 9 ms 2636 KB Output is correct
24 Correct 709 ms 2668 KB Output is correct
25 Execution timed out 2073 ms 3148 KB Time limit exceeded
26 Halted 0 ms 0 KB -