-
Notifications
You must be signed in to change notification settings - Fork 103
/
blinnet.cpp
111 lines (111 loc) · 1.48 KB
/
blinnet.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 2008-08-28
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define gc getchar_unlocked
#else
#define gc getchar
#endif
using namespace std;
struct Edge
{
int v1;
int v2;
int wt;
Edge(){}
Edge(int v,int V,int W):v1(v),v2(V),wt(W){}
};
bool operator<(Edge e1,Edge e2)
{
return e1.wt<e2.wt;
}
#define MAXEDGES 500000
int in()
{
char c;
int res=0;
for(;;)
{
c=gc();
if (c<=32)
return res;
res=(res<<1)+(res<<3)+c-'0';
}
}
int main()
{
int t,i,j,V,E,n;
t=in();
static Edge edges[MAXEDGES];
int id[10000];
int sz[10000];
while (t--)
{
gc();
V=in();
E=0;
char s[20];
int x,y;
for (i=0; i<V; i++)
{
id[i]=i;
sz[i]=1;
do{}while (gc()-'\n');
n=in();
for (j=0; j<n; j++)
{
x=in();
y=in();
if (x-1>i)
continue;
edges[E].v1=i;
edges[E].v2=x-1;
edges[E].wt=y;
E++;
if (E==MAXEDGES)
throw;
}
}
sort(edges,edges+E);
int done=0;
unsigned int weight=0;
for (i=0; done<V-1; i++)
{
int a=edges[i].v1;
int b=edges[i].v2;
int w=edges[i].wt;
while (a!=id[a])
{
if (id[a]!=id[id[a]])
sz[id[a]]-=sz[a];
a=id[id[a]];
}
while (b!=id[b])
{
if (id[b]!=id[id[b]])
sz[id[b]]-=sz[b];
b=id[id[b]];
}
if (a==b)
continue;
else
{
weight+=w;
done++;
if (sz[a]<sz[b])
{
id[a]=b;
sz[b]+=sz[a];
}
else
{
id[b]=a;
sz[a]+=sz[b];
}
}
}
printf("%d\n",weight);
}
return 0;
}