[noip2014day1-T2]联合权值
无向连通图 G 有 n 个点,n-1 条边。点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi,每条边的长度均为 1。图上两点(u, v)的距离定义为 u 点到 v 点的最短距离。对于图 G 上的点对(u, v),若它们的距离为 2,则它们之间会产生Wu*Wv的联合权值。
请问图 G 上所有可产生联合权值的有序点对中,联合权值最大的是多少?所有联合权值之和是多少?
题解:树形dp;
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iomanip>
#include<map>
#include<set>
#include<vector>
#include<ctime>
#include<cmath>
#define LL long long
using namespace std;
#define LL long long
#define up(i,j,n) for(int i=(j);(i)<=(n);(i)++)
#define max(x,y) ((x)<(y)?(y):(x))
#define min(x,y) ((x)<(y)?(x):(y))
#define FILE "1"
const int maxn=,mod=;
int read(){
int x=;bool flag=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')flag=;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return flag?-x:x;
}
int n; struct node{
int y,next;
}e[maxn<<];
int linkk[maxn],len=,w[maxn],maxx[maxn];
int Max=,Sum=;
void insert(int x,int y){
e[++len].y=y;
e[len].next=linkk[x];
linkk[x]=len;
}
void init(){
n=read();
int x,y;
up(i,,n){
x=read(),y=read();
insert(x,y);insert(y,x);
}
up(i,,n)w[i]=read();
}
int q[maxn],tail,head=,fa[maxn],siz[maxn],ans[maxn],ru[maxn];
void bfs(){
head=tail=;
q[++tail]=;int x;
while(++head<=tail){
x=q[head];
for(int i=linkk[x];i;i=e[i].next){
if(e[i].y==fa[x])continue;
ru[x]++;
fa[e[i].y]=x;
q[++tail]=e[i].y;
}
}
tail=,head=;
up(i,,n)if(!ru[i])q[++tail]=i;
while(++head<=tail){
x=q[head];
ans[x]=(ans[x]+w[fa[x]]*siz[x])%mod;
Sum+=ans[x];
if(--ru[fa[x]]==)q[++tail]=fa[x];
ans[fa[x]]=(ans[fa[x]]+siz[fa[x]]*w[x])%mod;
siz[fa[x]]=(siz[fa[x]]+w[x])%mod;
Sum=(Sum+ans[x])%mod;
if(w[x]*maxx[fa[x]]>Max)Max=w[x]*maxx[fa[x]];
if(w[fa[x]]*maxx[x]>Max)Max=w[fa[x]]*maxx[x];
if(w[x]>maxx[fa[x]])maxx[fa[x]]=w[x];
}
cout<<Max<<" "<<Sum<<endl;
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
init();
bfs();
}
[noip2014day1-T2]联合权值的更多相关文章
- Noip2014 提高组 T2 联合权值 连通图+技巧
联合权值 描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的 ...
- NOIP 2014 T2 联合权值 DFS
背景 NOIP2014提高组第二题 描述 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每条边的长度均为1.图上两点(u, v)的距离定义为u点到v点的最短距离.对 ...
- 【前缀和】【前缀MAX】洛谷 P1351 NOIP2014提高组 day1 T2 联合权值
不难发现,树中与某个点距离为2的点只可能是它的父亲的父亲.儿子的儿子 或者 兄弟,分类讨论一下即可. 只有对于兄弟我们不能暴力搞,维护一下每个节点的所有儿子的前缀和.前缀MAX就行了. #includ ...
- NOIP 2004 联合权值
洛谷 P1351 联合权值 洛谷传送门 JDOJ 2886: [NOIP2014]联合权值 D1 T2 JDOJ传送门 Description 无向连通图 G有 n个点,n-1条边.点从 1到 n依次 ...
- Codevs 3728 联合权值
问题描述 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每 条边的长度均为1.图上两点(u,v)的距离定义为u点到v点的最短距离.对于图G上的点 对(u,v),若它 ...
- P1906联合权值
描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的最短距离. ...
- [NOIP2014] 提高组 洛谷P1351 联合权值
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- NOIp 2014 #2 联合权值 Label:图论 !!!未AC
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- 【洛谷P1351】联合权值
我们枚举中间点,当连的点数不小于2时进行处理 最大值好搞 求和:设中间点 i 所连所有点权之和为sum 则对于每个中间点i的联合权值之和为: w[j]*(sum-w[j])之和 #include< ...
- NOIP2014 联合权值
2.联合权值 (link.cpp/c/pas) [问题描述] 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每条边的长度均为1.图上两点(u, v)的距离定义为u ...
随机推荐
- 如何快速定位TempDB产生问题
步骤1.TempDB压力诊断 等待类型诊断 TempDB的争用压力在等待篇中已经简单介绍,等待的表现为 pagelatch_类等待,等待的资源是 “2: X :X ” tempDB所在磁盘的响应时间 ...
- Network | DHCP
动态主机设置协议(Dynamic Host Configuration Protocol, DHCP)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途: 给内部网络或网络服务供应商自动分配I ...
- SpringUtils写法
@Componentpublic class SpringUtils implements ApplicationContextAware { @Override public void setApp ...
- OpenGL - Tessellation Shader 【转】
http://blog.sina.com.cn/s/blog_8c7d49f20102v4qm.html Patch is just an ordered list of vertices (在tes ...
- 编译php ./configure命令enable和with有什么区别
原文: https://segmentfault.com/q/1010000009174725 ---------------------------------------------------- ...
- css 中的浮动
css中 浮动的作用: 使元素脱离正常的文档流并使其移动到其父元素的“最左边”或“最右边”. css中 浮动的特点: 1)改变元素类型,使元素支持宽高: 2)半脱离文档流: 3)文本环绕: 4)顶对齐 ...
- PS 如何制作眼泪效果
1.用钢笔工具勾出眼泪的路径然后按Ctrl + Enter转为选区 2.按Ctrl + J 把选区复制出来,执行滤镜 > 扭曲 > 球面化 同样的方法制作流出的眼泪,然后添加图层样式选择投 ...
- [ssh新闻公布系统三]存储新闻
一.存储新闻dao方法 在NewsDao.java中新增存储新闻的saveOrupdate方法 public void saveOrupdate(News news){ getSession().sa ...
- Foundation框架 - NSException类
NSException类 WBStudentManager.h #import <Foundation/Foundation.h> NSString* const NameInvalidE ...
- mysql手动停止无响应查询方法
http://www.chenweionline.cn/archives/61.htm