无向连通图 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]联合权值的更多相关文章

  1. Noip2014 提高组 T2 联合权值 连通图+技巧

    联合权值 描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的 ...

  2. NOIP 2014 T2 联合权值 DFS

    背景 NOIP2014提高组第二题 描述 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每条边的长度均为1.图上两点(u, v)的距离定义为u点到v点的最短距离.对 ...

  3. 【前缀和】【前缀MAX】洛谷 P1351 NOIP2014提高组 day1 T2 联合权值

    不难发现,树中与某个点距离为2的点只可能是它的父亲的父亲.儿子的儿子 或者 兄弟,分类讨论一下即可. 只有对于兄弟我们不能暴力搞,维护一下每个节点的所有儿子的前缀和.前缀MAX就行了. #includ ...

  4. NOIP 2004 联合权值

    洛谷 P1351 联合权值 洛谷传送门 JDOJ 2886: [NOIP2014]联合权值 D1 T2 JDOJ传送门 Description 无向连通图 G有 n个点,n-1条边.点从 1到 n依次 ...

  5. Codevs 3728 联合权值

    问题描述 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每 条边的长度均为1.图上两点(u,v)的距离定义为u点到v点的最短距离.对于图G上的点 对(u,v),若它 ...

  6. P1906联合权值

    描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的最短距离. ...

  7. [NOIP2014] 提高组 洛谷P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  8. NOIp 2014 #2 联合权值 Label:图论 !!!未AC

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  9. 【洛谷P1351】联合权值

    我们枚举中间点,当连的点数不小于2时进行处理 最大值好搞 求和:设中间点 i 所连所有点权之和为sum 则对于每个中间点i的联合权值之和为: w[j]*(sum-w[j])之和 #include< ...

  10. NOIP2014 联合权值

    2.联合权值 (link.cpp/c/pas) [问题描述] 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi  ,每条边的长度均为1.图上两点(u, v)的距离定义为u ...

随机推荐

  1. Hibernate游记——盘缠篇(jar包)

    需要的jar包我都放我百度网盘了 jar(包含注解需要的jar)包传送门:http://pan.baidu.com/s/1o60OYvg

  2. [Machine Learning with Python] Data Visualization by Matplotlib Library

    Before you can plot anything, you need to specify which backend Matplotlib should use. The simplest ...

  3. CodeChef - RIN Course Selection

    Read problems statements in Mandarin Chineseand Russian. Rin is attending a university. She has M se ...

  4. Android中使用SDcard进行文件的读取

    来自:http://www.cnblogs.com/greatverve/archive/2012/01/13/android-SDcard.html 平时我们需要在手机上面存储想音频,视频等等的大文 ...

  5. 主流浏览器js 引擎内核市场份额attialx总结vOa9

    原文: http://blog.csdn.net/attilax/article/details/40016... 时间: 2014-10-12 atitit.. 主流浏览器 js 引擎 内核 市场份 ...

  6. Windows10系统修复

    sfc /scannow 命令将扫描所有受保护的系统文件,并用位于 %WinDir%\System32\dllcache 的压缩文件夹中的缓存副本替换损坏的文件. %WinDir% 占位符代表Wind ...

  7. sklearn特征选择和分类模型

    sklearn特征选择和分类模型 数据格式: 这里.原始特征的输入文件的格式使用libsvm的格式,即每行是label index1:value1 index2:value2这样的稀疏矩阵的格式. s ...

  8. CentOS 6.4安装Puppet

    CentOS安装Puppet   环境介绍:centos6.4x64 採用CentOS-6.4-x86_64-minimal.iso最小化安装   puppet版本号3.6.2.ruby1.8.7,f ...

  9. 使用".."指定git提交范围与"..."指定git提交范围的区别

    http://blog.csdn.net/hansel/article/details/8952967 使用".."(两个点)和"..."(三个点)都可以指定一 ...

  10. kubernetes管理之使用yq工具截取属性

    系列目录 前面我们讲解过使用go-template或者jsonpath格式(kubectl get 资源 --output go-tempalte(或jsonpath))来截取属性的值,并且我们比较了 ...