forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvent.cpp
76 lines (76 loc) · 1.2 KB
/
invent.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 2008-07-20
// Note: I'm pretty sure I cheated on this problem by looking at the solution
// on the IPSC website...
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
using namespace std;
struct Edge
{
int s;
int t;
int w;
Edge(){}
Edge(int S,int T,int W):s(S),t(T),w(W){}
};
bool operator<(Edge e1,Edge e2)
{
return e1.w<e2.w;
}
int main()
{
int T,i,N,j;
int id[15000];
int size[15000];
Edge E[15000];
long long sum=0;
scanf("%d",&T);
for (i=0; i<T; i++)
{
scanf("%d",&N);
for (j=0; j<N; j++)
{
id[j]=j;
size[j]=1;
}
for (j=0; j<N-1; j++)
{
scanf("%d %d %d",&E[j].s,&E[j].t,&E[j].w);
E[j].s--;
E[j].t--;
}
sort(E,E+(N-1));
sum=0;
for (j=0; j<N-1; j++)
{
int s=E[j].s;
int t=E[j].t;
while (s!=id[s])
{
if (id[s]!=id[id[s]])
size[id[s]]-=size[s];
//size[id[id[s]]]+=size[s];
s=id[s]=id[id[s]];
}
while (t!=id[t])
{
if (id[t]!=id[id[t]])
size[id[t]]-=size[t];
t=id[t]=id[id[t]];
}
sum+=((long long)size[s]*size[t]-1)*(E[j].w+1)+E[j].w;
if (size[s]>size[t])
{
size[s]+=size[t];
id[t]=s;
}
else
{
size[t]+=size[s];
id[s]=t;
}
}
printf("%lld\n",sum);
}
return 0;
}