题目描述 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. 重写java.lang.String IndexOf()方法,实现对字符串以ASCII规则截取

    /** * 根据元数据和目标ascii位数截取字符串,失败返回-1 * @param sourceStr 元数据字符串 * @param endIndex 截取到第几位 * @return 结果字符串 ...

  2. SELECT TOP 100 PERCENT * 的含义

    --返回符合条件的100%的记录,即所有符合条件的记录SELECT TOP 100 PERCENT * --返回符合条件的100条记录,即只返回符合条件的100条记录SELECT TOP 100 * ...

  3. raw cannot be resolved or is not a field解决办法

    解决raw文件夹问题 查看左侧项目/res文件夹下是否有raw文件夹,(一定是放到res文件夹下,raw在项目开始创建时候不会自动创建,所以要自己创建)

  4. ActiveSync日志分析

    Here are the steps to exporting the ActiveSync log data: Create a folder on the root      of C:\     ...

  5. jQuery 开始动画,停止动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. samba的安装及其使用

    参考pdf文档:https://pan.baidu.com/s/1iPJ1iPtNx7ZXNHRurrjfqw centos7 samba配置#centos7安装Sambayum -y install ...

  7. Getting start with dbus in systemd (02) - How to create a private dbus-daemon

    Getting start with dbus in systemd (02) 创建一个私有的dbus-daemon (session) 环境 这里我们会有两个app: app1(client),ap ...

  8. 如何手写一款KOA的中间件来实现断点续传

    本文实现的断点续传只是我对断点续传的一个理解.其中有很多不完善的地方,仅仅是记录了一个我对断点续传一个实现过程.大家应该也会发现我用的都是一些H5的api,老得浏览器不会支持,以及我并未将跨域考虑入内 ...

  9. Unexpected token d in JSON at position 669 while parsing near '...ct-mode":"^6.0.2"}

    问题 在安装 babel 的时候,遇到问题 Unexpected token d in JSON at position 669 while parsing near '...ct-mode" ...

  10. 远程连接mysql数据库

    远程连接mysql数据库需要以下步骤(改表法): 1.数据源(数据库服务器)对要访问数据库进行授权: 2.本地库(本地)连接: 具体方法: 1. 进入数据服务器,输入cmd进入控制台:输入 mysql ...