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;
}
上一篇:C++多继承的好处是增加了弹性和灵活性,Delphi类强迫单继承TObject是为了提供许多强大功能


下一篇:python Flask框架CBV视图