Tree and Permutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1421 Accepted Submission(s): 533
Problem Description
There are N vertices connected by N−1 edges, each edge has its own length.
The set { 1,2,3,…,N } contains a total of N! unique permutations, let’s say the i-th permutation is Pi and Pi,j is its j-th number.
For the i-th permutation, it can be a traverse sequence of the tree with N vertices, which means we can go from the Pi,1-th vertex to the Pi,2-th vertex by the shortest path, then go to the Pi,3-th vertex ( also by the shortest path ) , and so on. Finally we’ll reach the Pi,N-th vertex, let’s define the total distance of this route as D(Pi) , so please calculate the sum of D(Pi) for all N! permutations.
Input
There are 10 test cases at most.
The first line of each test case contains one integer N ( 1≤N≤105 ) .
For the next N−1 lines, each line contains three integer X, Y and L, which means there is an edge between X-th vertex and Y-th of length L ( 1≤X,Y≤N,1≤L≤109 ) .
Output
For each test case, print the answer module 109+7 in one line.
Sample Input
3 1 2 1 2 3 1 3 1 2 1 1 3 2
Sample Output
16 24
Source
看起来很难……事实上就是个纸老虎,(x, y)在遍历序列中相领的情况有(n - 1)!种,对于在(x, y)之间的边都有(n - 1)!的贡献,反过来考虑每条边,对于左侧的点集S和右侧的点集T而言,每条从S到T或从T到S的路径都会造成一次访问,所以每条边的贡献就是
len * Size(S) * Size(T) * 2 * (n - 1)!
一遍dfs就完了
比赛的时候sz数组忘了开long long, wa了两发才过……惨惨
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 100100;
const int MAXE = 200200;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int lim = 100100;
template <typename T> inline void read(T &x) {
int ch = getchar();
bool fg = false;
for (x = 0; !isdigit(ch); ch = getchar()) {
if (ch == '-') {
fg = true;
}
}
for (; isdigit(ch); ch = getchar()) {
x = x * 10 + ch - '0';
}
if (fg) {
x = -x;
}
}
struct Edge {
int to, nxt;
LL len;
Edge() {}
Edge(int _to, int _nxt, LL _len) : to(_to), nxt(_nxt), len(_len){}
}E[MAXE];
int h[MAXN], cnt, n;
int fa[MAXN];
LL sz[MAXN];
LL dep[MAXN];
LL fac[MAXN + 10];
LL inv[MAXN + 10];
void init() {
fac[0] = 1;
for(int i = 1; i < lim; i++) fac[i] = (long long)fac[i - 1] * i % mod;
inv[0] = inv[1] = 1;
for(int i = 2; i < lim; i++) inv[i] = (long long)(mod - mod / i) * inv[mod % i] % mod;
for(int i = 1; i < lim; i++) inv[i] = (long long)inv[i] * inv[i - 1] % mod;
}
typedef pair<int, int> pii;
inline void add_edge(int u, int v, int w) {
E[++cnt] = Edge(v, h[u], w), h[u] = cnt;
E[++cnt] = Edge(u, h[v], w), h[v] = cnt;
}
void dfs(int x) {
sz[x] = 1;
for(int i = h[x]; i; i = E[i].nxt) {
int to = E[i].to;
if(to == fa[x]) continue;
fa[to] = x;
dfs(to);
sz[x] += sz[to];
}
}
signed main() {
init();
while(scanf("%d", &n) != EOF) {
memset(h, 0, sizeof(h));
cnt = 0;
memset(fa, 0, sizeof(fa));
dep[1] = 0;
for(int i = 1; i < n; i++) {
int a, b, c;
read(a), read(b), read(c);
add_edge(a, b, c);
}
dfs(1);
LL ans = 0;
for(int x = 1; x <= n; x++) {
for(int i = h[x]; i; i = E[i].nxt) {
int to = E[i].to;
if(fa[x] == to) continue;
LL tmp = (n - sz[to]) * sz[to] % mod;
ans = (ans + ((tmp * 2 % mod) * fac[n - 1] % mod) * (E[i].len % mod)) % mod;
}
}
printf("%lld\n", (ans + mod) % mod);
}
return 0;
}