• 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 n1n−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 n2n−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=1u<vnmex(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 (2n30002≤n≤3000), the number of gangs in the network.

     

    Each of the next n1n−1 lines contains integers uiui and vivi (1ui,vin1≤ui,vi≤n; uiviui≠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 }
    View Code

     

     

  • 相关阅读:
    c#除掉字符串最后一个字符几种方法
    DateTime.Compare(t1,t2)比较两个日期大小
    图片如何存入数据库
    C#文本文件(.txt)读写
    【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
    【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)
    【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)
    【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)
    【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)
    总结用CoreText绘制文本时遇到的问题以及解决办法
  • 原文地址:https://www.cnblogs.com/ghosh/p/12681053.html
Copyright © 2020-2023  润新知