题目

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:

\[s=\sum_{1\le u \le v \le n} mex(u,v)
\]

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?

输入格式

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

Each of the next n−1 lines contains integers \(u_i\) and \(v_i (1 \le u_i,v_i \le n; u_i \ne 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.

输出格式

print the maximum possible value of \(S\) — the number of password layers in the gangs' network.

样例输入1

  1. 3
  2. 1 2
  3. 2 3

样例输出1

  1. 3

样例输入1

  1. 5
  2. 1 2
  3. 1 3
  4. 1 4
  5. 3 5

样例输出1

  1. 10

注意

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

With this assignment, \(mex(1,2)=0\), \(mex(1,3)=24\) 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\).

题解

看一下样例2

首先考虑边权为\(0\)的这条边,只要通过这条边的,最小的整数就是\(1\)了,那么经过这条边路径的个数就是\(0\)贡献的代价,即这条边右边的点数量乘左边点数量:\(2 \times 3 = 6\)

再考虑\(1\),如果单独考虑它,经过它的最小整数是\(0\),对答案没有一点贡献了,所以必须和1组合起来,那么把\(0-1\)看做一个整体,右边一个点,左边3个点,所以贡献就是\(1 \times 3 = 3\)

注意这里的贡献是1的原因是之前已经有一层1的贡献,这里是2的贡献,所以每条链只多了1的贡献

对于\(2\),必须和\(0,1\)组合起来,而且只能考虑\(2-0-1\)这一条链,所以左边1个点,右边1个点,贡献就是\(1 \times 1 = 1\)

对于\(3\),无法构成一条链,贡献就是\(0\)

所以加起来就是\(10\),和样例输出一样

注意从小到大所有权值必须在一条链上,如果不够成一条链,比如\(3\),最小整数就是\(0\),相当于没有贡献了

简化模型,只考虑一条链

设\(dp_{i,j}\)为从\(i\)到\(j\)的\(S\)最大值

然后把左边的点数后右边点数的积加上中间的链的dp值,中间的dp值就可以用递归实现.

注意这里的递归可以使用记忆化搜索.

  1. #include <cstdio>
  2. #include <cstring>
  3. #define max(a, b) ((a) > (b) ? (a) : (b))
  4. const int maxn = 3005;
  5. long long dp[maxn][maxn], cnt[maxn][maxn], ans;
  6. int fa[maxn][maxn], head[maxn << 1], next[maxn << 1], to[maxn << 1], n, x, y, ct;
  7. void dfs(int x, int f, int root) {
  8. cnt[root][x] = 1;
  9. fa[root][x] = f;
  10. for (int i = head[x]; i; i = next[i]) {
  11. if (to[i] == f) continue;
  12. dfs(to[i], x, root);
  13. cnt[root][x] += cnt[root][to[i]];
  14. }
  15. }
  16. long long dpf(int x, int y) {
  17. if (x == y) return 0;
  18. if (dp[x][y] != -1) return dp[x][y];
  19. return dp[x][y] = cnt[y][x] * cnt[x][y] + max(dpf(fa[y][x], y), dpf(x, fa[x][y]));
  20. }
  21. int main() {
  22. scanf("%d", &n);
  23. for (int i = 1; i < n; i++) {
  24. scanf("%d%d", &x, &y);
  25. to[++ct] = --y, next[ct] = head[--x], head[x] = ct;
  26. to[++ct] = x, next[ct] = head[y], head[y] = ct;
  27. }
  28. for (int i = 0; i < n; i++) dfs(i, -1, i);
  29. memset(dp, -1, sizeof(dp));
  30. for (int i = 0; i < n; i++)
  31. for (int j = 0; j < n; j++) ans = max(ans, dpf(i, j));
  32. printf("%lld", ans);
  33. }

Codeforces 1292C Xenon's Attack on the Gangs 题解的更多相关文章

  1. CF1292C Xenon's Attack on the Gangs 题解

    传送门 题目描述 输入格式 输出格式 题意翻译 给n个结点,n-1条无向边.即一棵树.我们需要给这n-1条边赋上0~ n-2不重复的值.mex(u,v)表示从结点u到结点v经过的边权值中没有出现的最小 ...

  2. CF1292C Xenon's Attack on the Gangs

    题目链接:https://codeforces.com/problemset/problem/1292/C 题意 在一颗有n个节点的树上,给每个边赋值,所有的值都在\([0,n-2]\)内并且不重复, ...

  3. Xenon's Attack on the Gangs(树规)

    题干 Input Output Example Test 1: Test 2: 3 5 1 2 1 2 2 3 1 3 1 4 3 5 3 10 Tips 译成人话 给n个结点,n-1条无向边.即一棵 ...

  4. Xenon's Attack on the Gangs,题解

    题目: 题意: 有一个n个节点的树,边权为0-n-2,定义mex(a,b)表示除了ab路径上的自然数以外的最小的自然数,求如何分配边权使得所有的mex(a,b)之和最大. 分析: 看似有点乱,我们先不 ...

  5. 【树形DP】CF 1293E Xenon's Attack on the Gangs

    题目大意 vjudge链接 给n个结点,n-1条无向边.即一棵树. 我们需要给这n-1条边赋上0~ n-2不重复的值. mex(u,v)表示从结点u到结点v经过的边权值中没有出现的最小非负整数. 计算 ...

  6. Codeforces Round #609 (Div. 2)前五题题解

    Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...

  7. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  8. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解

    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...

  9. Educational Codeforces Round 59 (Rated for Div. 2) DE题解

    Educational Codeforces Round 59 (Rated for Div. 2) D. Compression 题目链接:https://codeforces.com/contes ...

随机推荐

  1. CentOS7——搭建LNMP环境(WordPress案例)

    CentOS7--搭建LNMP环境(WordPress案例) LNMP组成介绍 LNMP(Linux-Nginx-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统 ...

  2. nacos基础--客户端下载

    对于nacos的作用,我在这里不在过多介绍,不知道的同学可以自行先了解,对于nacos,有官网进行介绍,对于一个初学者来说是一件非常方便的事情. 官网地址:https://nacos.io 但是在下载 ...

  3. Redis的持久化设计

    Redis 持久化设计 持久化的功能:Redis是内存数据库,数据都是存储在内存中的,为了避免进程退出导致数据的永久丢失,要定期将Redis中的数据以某种形式从内存保存到硬盘,当下次Reids重启时, ...

  4. String 类中常用方法

    序号 方法定义 类型 描述 1 public String(char[] value) 构造 直接将一个字符数组变为一个字符串 2 public String(char[] value,int off ...

  5. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(八)

    系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...

  6. #Linux 下 Xampp的安装与Hello World

    一.下载安装 去官网下载 移动下载完毕的xampp-linux-x64-7.4.6-0-installer.run 到/usr/local/jayce-softwares/xampp目录下(jayce ...

  7. Netty中的这些知识点,你需要知道!

    一.Channel Channel是一个接口,而且是一个很大的接口,我们称之为“大而全”,囊括了server端及client端接口所需要的接口. Channel是一个门面,封装了包括网络I/O及相关的 ...

  8. async/await到底该怎么用?如何理解多线程与异步之间的关系?

    前言 如标题所诉,本文主要是解决是什么,怎么用的问题,然后会说明为什么这么用.因为我发现很多萌新都会对之类的问题产生疑惑,包括我最初的我,网络上的博客大多知识零散,刚开始看相关博文的时候,就这样.然后 ...

  9. keycode 大全:

  10. AndroidStudio 编译失败;Caused by: org.gradle.api.resources.ResourceException: Could not get resource..

    错误信息: Caused by: org.gradle.api.resources.ResourceException: Could not get resource 'https://jcenter ...