P1351 联合权值

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> son[maxn];
int v[maxn], s[maxn], w[maxn];
int main() {
	freopen("input.in", "r", stdin);
	int n, m;
	cin >> n;
	memset(s, 0,sizeof(s));
	for(int i = 1; i <= n-1; i++) {
		int x, y;
		cin >> x >> y;
		son[x].push_back(y);
		son[y].push_back(x);
	}
	for(int i = 1; i <= n; i++) {
		cin >> v[i];
	}	
	int first[maxn], second[maxn];
	for(int i = 1; i <= n; i++) {
		vector<int>::iterator it;
		for(it = son[i].begin(); it != son[i].end(); it++) {
			s[i] += v[*it];
			if(v[*it] > first[i]) { second[i] = first[i];first[i] = v[*it];} 
			else if(v[*it] > second[i]) second[i] = v[*it];
		}
	}
	int maxx = 0, sum = 0;
	for(int i = 1; i <= n; i++) maxx = max(maxx,first[i] * second[i]);
	for(int i = 1; i <= n; i++) {
		vector<int>::iterator it;
		for(it = son[i].begin(); it != son[i].end(); it++) {
			w[i] += ((s[i] - v[*it]) * v[*it])%10007;
		}
		sum += w[i] % 10007;
	}
	cout << maxx << ' ' << sum % 10007 << endl;
}

posted on 2016-11-07 21:38  蒟蒻konjac  阅读(132)  评论(0编辑  收藏  举报