日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Two Paths CodeForces - 14D(暴力+树的直径)

發(fā)布時間:2023/12/15 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Two Paths CodeForces - 14D(暴力+树的直径) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

As you know, Bob’s brother lives in Flatland. In Flatland there are n cities, connected by n?-?1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.

The ?Two Paths? company, where Bob’s brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn’t cross (i.e. shouldn’t have common cities).

It is known that the profit, the ?Two Paths? company will get, equals the product of the lengths of the two paths. Let’s consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.

Input
The first line contains an integer n (2?≤?n?≤?200), where n is the amount of cities in the country. The following n?-?1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai,?bi (1?≤?ai,?bi?≤?n).

Output
Output the maximum possible profit.

Examples
Input
4
1 2
2 3
3 4
Output
1
Input
7
1 2
1 3
1 4
1 5
1 6
1 7
Output
0
Input
6
1 2
2 3
2 4
5 4
6 4
Output
4
我承認一開始被這個分數(shù)和她的標簽嚇著了。。
后來發(fā)現(xiàn)n最大才二百的時候,直接枚舉每條邊,刪掉這條邊之后形成兩棵樹。求這兩棵樹的直徑,更新答案。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=100+100; vector<int> p[maxx]; int dp[maxx][2]; int ans; int n;inline int dfs(int u,int f) {int sum=0;int max1=0,max2=0;for(int i=0;i<p[u].size();i++){if(p[u][i]!=f){sum=max(sum,dfs(p[u][i],u));if(ans>max1){max2=max1;max1=ans;}else max2=max(max2,ans);}}sum=max(sum,max1+max2);ans=max1+1;return sum; } int main() {int x,y;scanf("%d",&n);for(int i=1;i<n;i++){scanf("%d%d",&x,&y);p[x].push_back(y);p[y].push_back(x);}int ans=0;int _max=0;for(int i=1;i<=n;i++){for(int j=0;j<p[i].size();j++){ans=0;int _max1=dfs(i,p[i][j]);int _max2=dfs(p[i][j],i);_max=max(_max,_max1*_max2);}}cout<<_max<<endl; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Two Paths CodeForces - 14D(暴力+树的直径)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。