Xenon's Attack on the Gangs Codeforces Round #614 (Div. 2)
Xenon’s Attack on the Gangs
题意: 给你一棵树,将0~n-2一一赋值给n-1条边,则S最大可能取值
S = ∑ 1 ≤ u < v ≤ n m e x ( u , v ) S=\sum_{1\le u< v\le n}mex(u,v) S=∑1≤u<v≤nmex(u,v)
mex(u,v)表示u到v的路径中未出现的最小非负数
思路: 这里推荐一个视频题解,个人觉得讲得非常好 qscqesze
S = ∑ 1 ≤ u < v ≤ n m e x ( u , v ) = ∑ 1 ≤ x ≤ n ( ∑ m e x ( u , v ) = x x ) = ∑ 1 ≤ x ≤ n ( ∑ m e x ( u , v ) ≥ x 1 ) S=\sum_{1\le u< v\le n}mex(u,v)=\sum_{1\le x\le n}(\sum_{mex(u,v)=x}x)=\sum_{1\le x \le n}(\sum_{mex(u,v)\ge x}1) S=∑1≤u<v≤nmex(u,v)=∑1≤x≤n(∑mex(u,v)=xx)=∑1≤x≤n(∑mex(u,v)≥x1)
所以我们只需要考虑每条边的贡献即可,每条边要有贡献。
首先我们可以看0所在的边的两个端点u,v,则0的贡献为size[v][u]*size[u][v],接着看1,我们发现只有将0,1连接在一起贡献才会尽可能的大,所以我们要使得[0,m]分布在一条链上。所以我们可以对这棵树进行dp,dp[u][v]表示以u,v为端点的链
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=3e3+5;vector<int>G[N];
int fa[N][N],sz[N][N];
ll dp[N][N],ans;void dfs(int u,int f,int root){sz[root][u]=1;fa[root][u]=f;for(auto v: G[u]){if(v==f)continue;dfs(v,u,root);sz[root][u]+=sz[root][v];}
}ll sl(int x,int y){if(x==y)return 0;if(dp[x][y]!=-1)return dp[x][y];dp[x][y]=1ll*sz[y][x]*sz[x][y]+max(sl(fa[y][x],y),sl(x,fa[x][y]));return dp[x][y];
}int main(){#ifdef Mizpfreopen("in.txt","r",stdin);
#endifstd::ios::sync_with_stdio(0);int n;cin>>n;for(int i=1;i<n;i++){int u,v;cin>>u>>v;G[u].push_back(v);G[v].push_back(u);}for(int i=1;i<=n;i++){dfs(i,0,i);}memset(dp,-1,sizeof dp);for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){ans=max(ans,sl(i,j));}}cout<<ans<<endl;return 0;
}
Xenon's Attack on the Gangs Codeforces Round #614 (Div. 2)相关推荐
- E. Xenon's Attack on the Gangs,Codeforces Round #614 (Div. 2),树形dp
E. Xenon's Attack on the Gangs http://codeforces.com/contest/1293/problem/E On another floor of the ...
- Codeforces Round #614 (Div. 2) E. Xenon's Attack on the Gangs(DP记忆化搜索+思维)
题目链接:https://codeforces.com/contest/1293/problem/E 题目大意: 给出一棵树,要求给树的每条边赋权值,使得树中所有点的 m e x ( x , y ...
- Codeforces Round #614 (Div. 1) C.Xenon's Attack on the Gangs(树形dp)
题目 给你一棵n(n<=3000)个点的树,树上每个点对(u,v)对答案的贡献是mex(u,v), mex就是sg函数里的那个mex 每个点对只被统计一次,令所有点对的贡献和最大,输出贡献和 思 ...
- Codeforces Round #614 (Div. 2) D. Aroma‘s Search 暴力 + 思维
传送门 文章目录 题意: 思路: 题意: 给你x0,y0,ax,ay,bx,byx_0,y_0,a_x,a_y,b_x,b_yx0,y0,ax,ay,bx,by,让后根据[ax∗xi−1+ ...
- Codeforces 1292C Xenon's Attack on the Gangs
题目链接 Codeforces 1292C Xenon's Attack on the Gangs 题目大意 给定一棵树,将 [ 0 , n − 2 ] [0,n-2] [0,n−2]内的每个整数都仅 ...
- Codeforces Round #506 (Div. 3)
Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...
- Codeforces Round #563 (Div. 2)/CF1174
Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...
- 构造 Codeforces Round #302 (Div. 2) B Sea and Islands
题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...
- Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...
最新文章
- POJ - 3160 Father Christmas flymouse tanjar缩点构图+dfs
- spring3.x企业应用开发实战 pdf_吃透Spring全家桶:Spring源码+SpringBoot+SpringCloud实战...
- 2.CCGridAction(3D效果),3D反转特效,凸透镜特效,液体特效,3D翻页特效,水波纹特效,3D晃动的特效,扭曲旋转特效,波动特效,3D波动特效
- TypeScript 编译生成的 JavaScript 源代码里的 ɵcmp 属性
- 20165306 第八周学习任务
- 使用FFMPEG SDK解码流数据获得YUV数据及其大小
- springboot 集成redis_一文详解Spring Boot 集成 Redis
- Python介绍、发展史、安装、变量、注释、输入
- HDU 3662 3D Convex Hull(三维凸包面的个数)
- 数据挖掘(data mining),机器学习(machine learning),和人工智能(AI)的区别是什么
- qt 限制一段时间内对button只能点按一次_299元入手拓牛智能垃圾桶,用第一次想退货,第三天我上瘾了...
- 延时队列的几种实现方式(只有原理,并没有源码)
- 电力系统微型计算机继电保护试题及答案,全国2010年月高等教育自学考试电力系统微型计算机继电保护试题及答案.doc...
- 面试官看到一定会打我---软件测试工程师面试套路和暗语灵魂解密
- 我想成为一个记计算机程序员英语作文,我的梦想高中英语作文带翻译
- maven依赖查询网站
- 服务器维护封号,LOL客服的关于他们自己服务器问题导致账号被封号的问题
- GIS空间分析之公路选线
- 访问图片出现403的解决办法
- 房多多2020年财报:再度录得亏损,第四季度毛利率骤降至8%