【19.00%】【vijos p1906】联合权值
描述
无向连通图 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】联合权值的更多相关文章
- P1906联合权值
描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的最短距离. ...
- [NOIP2014提高组]联合权值
题目:洛谷P1351.Vijos P1906.codevs3728.UOJ#16. 题目大意:有一个无向连通图,有n个点n-1条边,每个点有一个权值$W_i$,每条边长度为1.规定两个距离为2的点i和 ...
- Codevs 3728 联合权值
问题描述 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每 条边的长度均为1.图上两点(u,v)的距离定义为u点到v点的最短距离.对于图G上的点 对(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 提高组 T2 联合权值 连通图+技巧
联合权值 描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的 ...
- NOIP2014 联合权值
2.联合权值 (link.cpp/c/pas) [问题描述] 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每条边的长度均为1.图上两点(u, v)的距离定义为u ...
- NOIP2014提高组第二题联合权值
还是先看题吧: 试题描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi ,每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 ...
随机推荐
- crm2013 查看下拉框的选项
在CRM2011中,我们非常easy查看下拉框的选择.打开页面,按F12.把光标对准目标,就会显示出详细的选项,如图:' watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...
- python获取序列中最大值
test =[ [1, 2, 3], [4, 5, 6], [7, 8, 9]] #这个就可以看做是二维数组了,直接创建print(test)print(test[:][1]) ...
- Ubuntu VMware Tools安装详细过程(非常靠谱)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.前言 VMware Ubuntu安装以及详细过程:https://blog.csdn.net/qq_41782425/arti ...
- StackExchange.Redis 官方文档(五) Keys, Values and Channels
原文:StackExchange.Redis 官方文档(五) Keys, Values and Channels Keys, Values and Channels 在使用redis的过程中,要注意到 ...
- 扩展的方法:es6 安装模块builder
https://github.com/es-shims/es5-shim/ Image.png 检测浏览器可支持es5,不支持就扩展,做兼容: 扩展的方法: Image.png 取所有对象的键值: o ...
- php课程 10-35 php实现文件上传的注意事项是什么
php课程 10-35 php实现文件上传的注意事项是什么 一.总结 一句话总结:记得限制大小和类型,还有就是用move_uploaded_file($sfile,$dfile);函数把上传到php临 ...
- C#中lock
http://www.cnblogs.com/apsnet/archive/2012/07/08/2581475.html
- 解题报告 之 HDU5305 Friends
解题报告 之 HDU5305 Friends Description There are people and pairs of friends. For every pair of friend ...
- POJ 3086 Triangular Sums (ZOJ 2773)
http://poj.org/problem?id=3086 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1773 让你计算两 ...
- 24、vb2_buffer和videobuf_buffer比较分析
看韦东山视频第三期摄像头驱动中构造了自己的vivi驱动,但是使用的videoBuf结构体,新的版本用的是vb2_buffer结构,我机器上(ubuntu12.04)使用的内核是linux3.2,看了看 ...