zoukankan      html  css  js  c++  java
  • 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

     

     

  • 相关阅读:
    【Thinkphp教程】URL路由功能解析
    MYSQL 错误#145解决方法
    【Thinkphp教程】空模块
    【Thinkphp教程】 如何进行模块分组
    mySQL中删除unique key的语法
    使用php让浏览器刷新
    Spring+Jpa整合的过程中遇到的一个问题。。。纠结了我半天。。。
    关于mule studio的应用
    解决eclipse和myeclipse不能编译项目的问题
    ajax fileupload上传组件的使用感悟
  • 原文地址:https://www.cnblogs.com/ghosh/p/12681053.html
Copyright © 2011-2022 走看看