Codeforces Round #614 (Div. 2) E. Xenon's Attack on the Gangs

On another floor of the A.R.C. Markland-N, the young man Simon "Xenon" Jackson, takes a break after finishing his project early (as always). Having a lot of free time, he decides to put on his legendary hacker "X" instinct and fight against the gangs of the cyber world.

His target is a network of nn small gangs. This network contains exactly n−1n−1 direct links, each of them connecting two gangs together. The links are placed in such a way that every pair of gangs is connected through a sequence of direct links.

By mining data, Xenon figured out that the gangs used a form of cross-encryption to avoid being busted: every link was assigned an integer from 00 to n−2n−2 such that all assigned integers are distinct and every integer was assigned to some link. If an intruder tries to access the encrypted data, they will have to surpass SS password layers, with SS being defined by the following formula:

S=∑1≤u<v≤nmex(u,v)S=∑1≤u<v≤nmex(u,v)

Here, mex(u,v)mex(u,v) denotes the smallest non-negative integer that does not appear on any link on the unique simple path from gang uu to gang vv.

Xenon doesn‘t know the way the integers are assigned, but it‘s not a problem. He decides to let his AI‘s instances try all the passwords on his behalf, but before that, he needs to know the maximum possible value of SS, so that the AIs can be deployed efficiently.

Now, Xenon is out to write the AI scripts, and he is expected to finish them in two hours. Can you find the maximum possible SS before he returns?

Input

The first line contains an integer nn (2≤n≤30002≤n≤3000), the number of gangs in the network.

Each of the next n−1n−1 lines contains integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n; ui≠viui≠vi), indicating there‘s a direct link between gangs uiui and vivi.

It‘s guaranteed that links are placed in such a way that each pair of gangs will be connected by exactly one simple path.

Output

Print the maximum possible value of SS — the number of password layers in the gangs‘ network.

Examples

Input

3
1 2
2 3

Output

3

Input

5
1 2
1 3
1 4
3 5

Output

10

Note

In the first example, one can achieve the maximum SS with the following assignment:

With this assignment, mex(1,2)=0mex(1,2)=0, mex(1,3)=2mex(1,3)=2 and mex(2,3)=1mex(2,3)=1. Therefore, S=0+2+1=3S=0+2+1=3.

In the second example, one can achieve the maximum SS with the following assignment:

With this assignment, all non-zero mex value are listed below:

  • mex(1,3)=1mex(1,3)=1
  • mex(1,5)=2mex(1,5)=2
  • mex(2,3)=1mex(2,3)=1
  • mex(2,5)=2mex(2,5)=2
  • mex(3,4)=1mex(3,4)=1
  • mex(4,5)=3mex(4,5)=3

Therefore, S=1+2+1+2+1+3=10S=1+2+1+2+1+3=10.

题解就直接看这个视频吧,感觉讲的不错 题解

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 3005;
 4 struct Node{
 5     int to, next, dis;
 6 }edge[maxn << 1];
 7 int cnt0, cnt[maxn][maxn], head[maxn], fa[maxn][maxn];
 8 int n, s, a[maxn], f[maxn][maxn];
 9 void Add(int u, int v){
10     edge[++cnt0].to = v;
11     edge[cnt0].next = head[u];
12     head[u] = cnt0;
13 }
14 void Dfs(int rt, int father, int root){
15     cnt[root][rt] = 1;
16     fa[root][rt] = father;
17     for (int i = head[rt]; i; i = edge[i].next){
18         int v = edge[i].to;
19         if (v != father) Dfs(v, rt, root), cnt[root][rt] += cnt[root][v];
20     }
21 }
22 int Solve(int x, int y){
23     if (x == y) return 0;
24     if (f[x][y] != -1) return f[x][y];
25     f[x][y] = cnt[y][x] * cnt[x][y] + max(Solve(fa[y][x], y), Solve(x, fa[x][y]));
26     return f[x][y];
27 }
28 int main(){
29     memset(f, -1, sizeof(f));
30     scanf("%d", &n);
31     for (int i = 1; i < n; i++){
32         int u, v;
33         scanf("%d%d", &u, &v);
34         u--, v--;
35         Add(u, v);
36         Add(v, u);
37     }
38     for (int i = 0; i < n; i++){
39         Dfs(i, -1, i);
40     }
41     int ans = 0;
42     for (int i = 0; i < n; i++){
43         for (int j = 0; j < n; j++){
44             ans = max(ans , Solve(i, j));
45         }
46     }
47     cout << ans;
48     return 0;
49 }

Codeforces Round #614 (Div. 2) E. Xenon's Attack on the Gangs

原文地址:https://www.cnblogs.com/ghosh/p/12681053.html

时间: 2024-08-01 05:51:55

Codeforces Round #614 (Div. 2) E. Xenon's Attack on the Gangs的相关文章

Codeforces Round #614 (Div. 2) D. Aroma&#39;s Search

题目链接:http://codeforces.com/contest/1293/problem/D 题意: 给定x0,y0,ax,ay,bx,by 即一堆经验点:(x0,y0),(x1,y1)等价于(ax*x0+bx,ay*y0+by),(x2,y2)等价于(ax*x1+bx,ay*y1+by),(x3,y3)等价于(ax*x2+bx,ay*y2+by)...... 再给定xs,ys,t 即起点(xs,ys),时间t 上下左右每走一步都需要1单位时间,问在t时间内,从起点出发最多可以吃到多少个经

Codeforces Round #614 (Div. 2) C. NEKO&#39;s Maze Game

题目链接:http://codeforces.com/contest/1293/problem/C 题意:给定n,q,即给定一个2*n的格子,有q个查询. 每个查询给定一个ri和ci,ri为1或2,ci在1到n之间,即给定一个(ri,ci),该点自该查询起状态进行转变(可经过/不可经过). 如某个查询给定1,2,即点(1,2)无法经过,若之后查询再次给定1,2,则该点(1,2)可以经过. 问能否从(1,1)走到(2,n),保证给定的查询不会经过起点和终点. 思路: 由于n和q最大都是1e5,所以

Codeforces Round #614 (Div. 2)

A. ConneR and the A.R.C. Markland-N 题目链接:https://codeforces.com/contest/1293/problem/A 题意: 有一个长为 n 的楼层,其中有 k 个楼层没有餐厅 ,你现在在 s 层,问你最少走多少个楼层可以到达餐厅吃饭 分析: 因为 k 只有 1000,所以直接往 s 层上下方找(当找到 0 或者 n + 1 时说明这个方向没有答案) #include<bits/stdc++.h> using namespace std;

Codeforces Round #614 (Div. 2) B - JOE is on TV!

原题题面:https://codeforces.com/contest/1293/problem/B 解题思路: Σi=1~n 1/i ??? 1 /* 2 Written By. StelaYuri 3 On 2020/01/19 4 */ 5 #include<bits/stdc++.h> 6 using namespace std; 7 typedef long long ll; 8 int a[1005],b[1005]; 9 void solve(){ 10 int n,i; 11

Codeforces Round #614 (Div. 2) A - ConneR and the A.R.C. Markland-N

原题题面:https://codeforces.com/contest/1293/problem/A 题目大意: ConneR老师想吃东西,他现在在大楼的第s层,大楼总共有n层,但是其中有k层的餐厅关门了. 然后给了这k层关门的餐厅分别所在的楼层. 所以问ConneR老师最少得往上(或者往下)走几层楼,才能到最近的还开门的餐厅就餐? 解题思路1: 对于关闭的k层,存在数组a里排序.(放在1~k的位置) 先循环一遍数组a,看看s层是否存在于a数组里,如果不存在,直接输出0作为答案. 如果存在,开始

Codeforces Round #614 (Div. 2) A( A - ConneR and the A.R.C. Markland-N)

A - ConneR and the A.R.C. Markland-N 题目链接:http://codeforces.com/contest/1293/problem/A 题意:一栋楼房有n(1~n)层,有个人身处s楼,现在想要到餐厅吃饭,可是现在有k个餐厅关闭的,问你该人至少爬几层楼梯才能到开放的餐厅吃饭 思路:...这题暴力没戏..又是超时又是超内存...分两种,一个是往上找出最小的i-s即可,一个是往下找,找出最小的s-i即可,,用了数组还是超时..用了map就过了 // // Crea

Codeforces Round #614 (Div. 2) 比赛总结

比赛情况 怒切 \(A,B,C,D\),后面 \(E,F\) 两题技术太菜不会做,不知道什么时候可以补起来. 比赛总结 事实证明: 比赛前喝一瓶抗疲劳饮料对比赛状态的进入有显著效果. 比赛有人陪着打对AC题目有显著效果. 说正经的: 不要紧张,也不要太过放松.这样才有利于发挥出真实水平. 下面就开始 喜闻乐见 的题解吧. A 入门题 枚举找到最近的可用楼层即可.用 \(STL\) 里面的 map 判断一个楼层是否可用使用. Code #include<bits/stdc++.h> #defin

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿