zoukankan      html  css  js  c++  java
  • O 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 (n) small gangs. This network contains exactly (n−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 (0) to (n−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 (S) password layers, with (S) being defined by the following formula:

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

    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 (S), 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 (S) before he returns?

    Input

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

    Each of the next (n−1) lines contains integers (u_i) and (v_i) ((1≤u_i,v_i≤n; u_i≠v_i)), indicating there's a direct link between gangs (u_i) and (v_i).

    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 (S) — the number of password layers in the gangs' network.

    Examples

    Input 1

    3
    1 2
    2 3
    

    Output 1

    3
    

    Input 2

    5
    1 2
    1 3
    1 4
    3 5
    

    Output 2

    10
    

    Note

    In the first example, one can achieve the maximum (S) with the following assignment:

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

    In the second example, one can achieve the maximum (S) with the following assignment:

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

    (mex(1,3)=1)
    (mex(1,5)=2)
    (mex(2,3)=1)
    (mex(2,5)=2)
    (mex(3,4)=1)
    (mex(4,5)=3)
    Therefore, (S=1+2+1+2+1+3=10).

    题解

    可以看看这个视频
    反正我是看这个看懂了

    解题思路

    代码

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 3e3+5;
    struct side {
    	int t, next;
    }a[N*2];
    int head[N], tot;
    void add(int x, int y) {
    	a[++tot].t = y;
    	a[tot].next = head[x];
    	head[x] = tot;
    }
    int n, fa[N][N];
    long long f[N][N], s[N][N], ans;
    void dfs(int x, int fx, int rt) {
    	fa[rt][x] = fx;
    	s[rt][x] = 1;
    	for(int i = head[x]; i; i = a[i].next) {
    		int y = a[i].t;
    		if (y == fx) continue;
    		dfs(y, x, rt);
    		s[rt][x] += s[rt][y];
    	}
    }
    long long dp(int x, int y) {
    	if (x == y) return 0;
    	if (f[x][y] != -1) return f[x][y];
    	return f[x][y] = s[x][y] * s[y][x] + max(dp(fa[y][x], y), dp(x, fa[x][y]));
    }
    int main() {
    	memset(f, -1, sizeof(f));
    	scanf("%d", &n);
    	for(int i = 1, x, y; i < n; i++)
    		scanf("%d%d", &x, &y),
    		add(x, y), add(y, x);
    	for(int i = 1; i <= n; i++)
    		dfs(i, -1, i);
    	for(int i = 1; i <= n; i++)
    		for(int j = 1; j <= n; j++)
    			ans = max(ans, dp(i, j));
    	printf("%lld", ans);
    	return 0;
    }
    
  • 相关阅读:
    2012 Multi-University Training Contest 8
    uva 11354最小生成树瓶颈路(lca算法实现)(rmq在多校二中有一道题)
    POJ 3164最小树形图
    uva11865 二分+最小树形图(朱刘算法)
    LA 5717枚举+最小生成树回路性质
    2014/3/9 长沙多校(第二次)
    zoj3759(待解决+算法木有问题+but需要java大数)
    ztr loves lucky numbers--hdu5676(DFS)
    C. Nearest vectors--cf598C(极角排序)
    D. Spongebob and Squares--cf599D(数学)
  • 原文地址:https://www.cnblogs.com/Z8875/p/12681378.html
Copyright © 2011-2022 走看看