1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <bits/stdc++.h> using namespace std; const int N = 1e6, M = 3e5; struct Edge { int e, ne; Edge() { e = ne = 0; } } edge[N]; int head[M], idx = 0; void add(int a, int b) { idx++, edge[idx].e = b, edge[idx].ne = head[a], head[a] = idx; } long long w[M]; int main() { int n; scanf("%d", &n); int a, b; for (int i = 0; i < n - 1; i++) { scanf("%d%d", &a, &b); add(a, b), add(b, a); } for (int i = 1; i <= n; i++) scanf("%lld", &w[i]); long long sum = 0, maxn = 0; long long he, rmax; for (int i = 1; i <= n; i++) { int u = head[i]; he = (rmax = w[edge[u].e]) % 10007; u = edge[u].ne; for (; u != 0; u = edge[u].ne) { sum = (sum + he * w[edge[u].e]) % 10007; maxn = max(maxn, rmax * w[edge[u].e]); he = (he + w[edge[u].e]) % 10007; rmax = max(rmax, w[edge[u].e]); } } printf("%lld %lld", maxn, (sum * 2) % 10007); return 0; }
|