题目描述 Description

输入描述 Input Description

输出描述 Output Description

样例输入 Sample Input

样例输出 Sample Output

数据范围及提示 Data Size & Hint

思路:
1、以每一个点为轴,左右两个点算权值
2、注意加法结合律(a+b+c)2 = a2 + b2 + c2 + 2ab + 2ac + 2bc
3、取模问题,(a+b)%c ≠ (a%c + b%c)%c,(a + b) % c = (a + b%c)%c
代码:
(注意链式前向星的使用)
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn =;
struct edge{
int next;
int to;
int power;
};
edge test[maxn];
int head[maxn],cur = ,n,j[maxn],p[maxn],max1[maxn],max2[maxn],nmax1[maxn],coun[maxn];
long long int vall = ,vmax = ,sub =; int use(int i)
{
int t=head[i];
int ti,tmp,sum=,cha2=; while (t!=-)
{ ti=test[t].to;
sum=(sum+p[ti]%)%;
cha2+=p[ti]*p[ti]%;
if (p[ti]>max1[i] )
{ max1[i]=p[ti];
nmax1[i]=ti;
}
t=test[t].next;
coun[i]++; }
sum=(sum*sum% +-cha2%)%;
vall+=sum%;
return sum;
} /*int dfs(int deep,int last,int now){
if(deep == 2){
if(last == now) return 0;
int temp;
temp = p[now]* p[last];
if(temp > vmax) vmax = temp;
return 0;
} for(int k=head[now];k>-1;k=test[k].next){
dfs(deep + 1,last,test[k].to);
}
} */
void dfs(int i)
{ int ansi=;
if (coun[i]>)
{
int t=head[i];
int ti;
while (t!=-)
{
ti=test[t].to;
if (ti!=nmax1[i] )
{
if (p[ti]>max2[i] )
max2[i]=p[ti];}
t=test[t].next;
}
}
if (vmax<max1[i]*max2[i] ) vmax=max1[i]*max2[i]; } void add(int u,int v,int w){
test[cur].power = w;
test[cur].to = v;
test[cur].next = head[u];
head[u] = cur++;
}
int main(){
cin>>n;
int u,v,w;
for(int i = ;i < n;i++){
test[i].power = ;
test[i].next = -;
head[i] = -;
j[i] = ;
}
int tu,tv;
for(int i = ;i < n-;i++){
cin>>tu>>tv;
tu--;
tv--;
add(tu,tv,);
add(tv,tu,); }
for(int i = ;i < n;i++) cin>>p[i]; for(int i = ;i < n;i++)use(i);
for(int i = ;i < n;i++)dfs(i);
cout<<vmax<<" "<<vall % <<endl;
return ;
}

codevs3728 联合权值的更多相关文章

  1. codevs3728联合权值(LCA)

    3728 联合权值  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 输入描述 Input Des ...

  2. 「NOIP2014」「Codevs3728」 联合权值(乱搞

    3728 联合权值 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 输入描述 Input Description 输出描述 Ou ...

  3. [NOIP2014提高组]联合权值

    题目:洛谷P1351.Vijos P1906.codevs3728.UOJ#16. 题目大意:有一个无向连通图,有n个点n-1条边,每个点有一个权值$W_i$,每条边长度为1.规定两个距离为2的点i和 ...

  4. Codevs 3728 联合权值

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

  5. P1906联合权值

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

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

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

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

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

  8. 【洛谷P1351】联合权值

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

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

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

随机推荐

  1. MVC C# 直接导出txt文件

    用asp.net根据数据内容自动生成一个txt文本文件并提供用户下载,此方法文件不保存在服务器上,直接提供给用户下载,到网上搜了一下,都是用的Response.BinaryWrite(),用了几下,发 ...

  2. vue学习之遇见的问题

    1.本地图片加载不出来 错误原因:图片放置位置不对: 解决方法:需要将图片放在static文件夹里

  3. CCF|最小差值|Java

    import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = ...

  4. Mysql函数、语句

    一:日期函数: 日期函数: SELECT CURDATE(); # 2018-07-07 SELECT CURTIME(); # 11:28:24 SELECT NOW(); # 2018-07-07 ...

  5. jquery 序列化form表单

    1.为什么要将form表单序列化? ajax上传form表单的原始方式,是将form表单中所需要的键值对先获取,然后再组装成数据(两种方式:http:localhost:8080/test.do?pe ...

  6. shell脚本的练习

    创建一个以.sh结束的文件. 规则: 文件的头部使用#!/bin/sh 开头   这个是一个标识的作用,告诉使用哪种脚本来执行 echo 用来向命令行来输出的东西

  7. 6-Java-C(打印大X)

    题目描述: 小明希望用星号拼凑,打印出一个大X,他要求能够控制笔画的宽度和整个字的高度. 为了便于比对空格,所有的空白位置都以句点符来代替. 要求输入两个整数m n,表示笔的宽度,X的高度.用空格分开 ...

  8. C++中何时使用引用

    使用引用参数的原因: 程序员能够修改调用函数中的数据对象 通过传递引用而不是整个数据对象,可以提高程序的运行速度. 当数据对象较大时(如结构和类对象),第二个原因最重要,这些也是使用指针参数的原因.这 ...

  9. 表单文件上传编码方式(enctype 属性)

    enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 如下: <form action="upload.php" method="post&quo ...

  10. 使用JQuery MiniUI,json数据构建TreeGrid(树图)

    index.html直接上代码. 需要引用MiniUI的boot.js <!DOCTYPE html> <html> <head> <meta charset ...