Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define sz(x) ((int)(x).size())
- #define endl '\n'
- #define debug(X) cout << #X << " = " << X << endl
- #define rep(i,a,b) for(int(i)=(a);(i)<(b);i++)
- #define SZ(v) (int)v.size()
- using namespace std;
- typedef long long ll;
- const int mod = (int) 1e9 + 7;
- const int oo = (int) 1e9;
- const int N = 1e5 + 2, E = 2e5 + 3;
- int n, m, e;
- int src, dst;
- int head[N], nxt[E], to[E];
- int longest_path[E][2], res[E];
- /* fast input */
- char buf[1<<24],*w=buf;
- int getint(){
- while(!isdigit(*w))w++;
- int r = 0;
- while(isdigit(*w)){
- r = r * 10 + *w - '0';
- w++;
- }
- return r;
- }
- void init()
- {
- e = 0;
- memset(head,-1,sizeof(head));
- memset(res,-1,sizeof(res));
- }
- void addEdge(int f, int t)
- {
- nxt[e] = head[f];
- head[f] = e;
- to[e++] = t;
- }
- void addBiEdge(int a, int b)
- {
- addEdge(a, b);
- addEdge(b, a);
- }
- int solve(int u, int p, int idx)
- {
- if(~res[idx])return res[idx];
- int ans = 0;
- for(int k=head[u]; ~k; k=nxt[k]){
- int v = to[k];
- if(v==p) continue;
- int tmp = solve(v,u,k);
- longest_path[idx][0] = max(longest_path[idx][0], longest_path[k][1]+1);
- if(longest_path[idx][0] > longest_path[idx][1])
- swap(longest_path[idx][0], longest_path[idx][1]);
- ans = max(tmp, ans);
- }
- return res[idx] = max(ans, longest_path[idx][0] + longest_path[idx][1]);
- }
- int main()
- {
- fread(buf,sizeof(char),1<<24,stdin);
- int n,a,b;
- int ans= 0;
- init();
- n = getint();
- pair<int,int> adj[N+1];
- int edge[E];
- rep(i,0,n-1)
- {
- a = getint();b = getint();
- edge[i]=e;
- addBiEdge(--a, --b);
- adj[i]= make_pair(a,b);
- }
- rep(i,0,n-1)
- {
- a = adj[i].first; b = adj[i].second;
- solve(a, b, edge[i]+1);
- solve(b, a, edge[i]);
- }
- rep(i,0,n-1)
- ans=max(ans, res[edge[i]]*res[edge[i]+1]);
- printf("%d", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement