描述

无向连通图 G 有 n 个点,n-1 条边。点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1。图上两点(u, v)的距离定义为 u 点到 v 点的最短距离。对于图 G 上的点对(u, v),若它们的距离为 2,则它们之间会产生WuWu×WvWv的联合权值。

请问图 G 上所有可产生联合权值的有序点对中,联合权值最大的是多少?所有联合权值之和是多少?

格式

输入格式

第一行包含 1 个整数 n。

接下来 n-1 行,每行包含 2 个用空格隔开的正整数 u、v,表示编号为 u 和编号为 v 的点 之间有边相连。

最后 1 行,包含 n 个正整数,每两个正整数之间用一个空格隔开,其中第 i 个整数表示 图 G 上编号为 i 的点的权值为WiWi。

输出格式

输出共 1 行,包含 2 个整数,之间用一个空格隔开,依次为图 G 上联合权值的最大值 和所有联合权值之和。由于所有联合权值之和可能很大,输出它时要对10007取余。

样例1

样例输入1[复制]

5

1 2

2 3

3 4

4 5

1 5 2 3 10

样例输出1[复制]

20 74

限制

对于 30%的数据,1 < n ≤ 100;

对于 60%的数据,1 < n ≤ 2000;

对于 100%的数据,1 < n ≤ 200,000,0 < WiWi ≤ 10,000。

【题解】





设w[a]+w[b]+w[c]+w[d]=sum[e]

则这个图的答案就是w[a](sum[e]-w[a])+w[b](sum[e]-w[b])+….

这样只要枚举n-1条边就能算出总的权值了;

最大权值只要求出和上图中c相邻的点中w的值最大和次大的就好;

要求出每个点的次大和最大;

然后取它们乘积的最大值;

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long using namespace std; const int MAXN = 2e5+100;
const int MOD = 10007;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0); struct bian
{
int x,y;
}; struct abc
{
int max1,max2;
}; int n;
bian b[MAXN];
int w[MAXN],sum[MAXN];
abc c[MAXN]; void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_int(n);
for (int i = 1;i <= n-1;i++)
input_int(b[i].x),input_int(b[i].y);
for (int i = 1;i <= n;i++)
input_int(w[i]);
for (int i = 1;i <= n-1;i++)
{
int x = b[i].x,y = b[i].y;
sum[x]=(sum[x]+w[y])%MOD;
sum[y]=(sum[y]+w[x])%MOD;
if (w[y]>c[x].max1)
{
swap(c[x].max1,c[x].max2);
c[x].max1 = w[y];
}
else
if (w[y]>c[x].max2)
c[x].max2 = w[y];
if (w[x]>c[y].max1)
{
swap(c[y].max1,c[y].max2);
c[y].max1 = w[x];
}
else
if (w[x]>c[y].max2)
c[y].max2 = w[x];
}
int ans1 = c[1].max1*c[1].max2;
for (int i = 2;i <= n;i++)
if (c[i].max1*c[i].max2>ans1)
ans1 = c[i].max1*c[i].max2;
int ans2 = 0;
for (int i = 1;i <= n-1;i++)
{
int x = b[i].x,y = b[i].y;
ans2 = (ans2+w[y]*(sum[x]-w[y]+MOD) + MOD)%MOD;
ans2 = (ans2+w[x]*(sum[y]-w[x]+MOD) + MOD)%MOD;
}
printf("%d %d\n",ans1,ans2);
return 0;
}

【19.00%】【vijos p1906】联合权值的更多相关文章

  1. P1906联合权值

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

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

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

  3. Codevs 3728 联合权值

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

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

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

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

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

  6. 【洛谷P1351】联合权值

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

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

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

  8. NOIP2014 联合权值

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

  9. NOIP2014提高组第二题联合权值

    还是先看题吧: 试题描述  无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi ,每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 ...

随机推荐

  1. PatentTips - Method for guest operating system integrity validation

    BACKGROUND The embodiments relate to guest operating system integrity validation, and more particula ...

  2. iOS8: 企业开发的终结?

    iOS 8 的公布(如今是 iOS8.1),并非对全部人来说都是值得高兴的事情. 对那些使用企业部署(不经过商店公布)的 app 开发人员来说,又被苹果坑到了. 由于 iOS 8 的一个Bug.导致企 ...

  3. struts2笔记---struts2的执行过程

    1.服务器启动: 加载项目web.xml 创建struts核心过滤器对象,执行filter-->init() struts-default.xml     核心功能的初始化 struts-plu ...

  4. (转)ipv4的网段表示方法

    简单一点举例说明:ip段:10.0.0.1-10.0.0.255            的表示方法:10.0.0.0/24ip段:10.0.0.1-10.0.255.255        的表示方法: ...

  5. IOS RGB颜色转换

    - (UIColor *)getColor:(NSString *)hexColor { unsigned int red,green,blue; NSRange range; range.lengt ...

  6. POJ 3641 Oulipo KMP 水题

    http://poj.org/problem?id=3461 直接KMP就好.水题 #include<cstdio> #include<cstring> const int M ...

  7. HDU 1248 寒冰王座 完全背包

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1248 中文题,大意就不说了. 第一道完全背包题,跟着背包九讲做的. 和0-1背包的区别在于所不同的是每种 ...

  8. 4、linux开发中常用指令

    1.cat /proc/device 可以查看各个全部字符设备和块设备,在register_chrdev中设置的名字在打印出来的信息中可以看到:2.top 可以看各个应用程序占用CPU量及PID等信息 ...

  9. [RxJS] Replace zip with combineLatest when combining sources of data

    This lesson will highlight the true purpose of the zip operator, and how uncommon its use cases are. ...

  10. [Ionic2] Device Interaction in an Ionic App with Cordova Plugins

    In this lesson, we are going to learn how to interact with native components through Cordova plugins ...