-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathACMContestAndBlackout_UVA10600_2OJ.cpp
140 lines (116 loc) · 2.82 KB
/
ACMContestAndBlackout_UVA10600_2OJ.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
struct edge
{
int u;
int v;
int d;
bool operator < (const edge & temp) const
{
return d < temp.d;
}
bool operator == (const edge & temp) const
{
return u == temp.u && v == temp.v && d == temp.d;
}
};
int _find(int u, vector<int> & p)
{
if (p[u] == u)
{
return u;
}
else
{
int ans = _find(p[u], p);
p[u] = ans;
return ans;
}
}
int solve(int schT, edge & del, vector<edge> & ways)
{
vector<int> p(schT + 1);
for (int i = 1; i <= schT; ++i)
{
p[i] = i;
}
int ans = 0, conT = ways.size();
for (int i = 1, j = 0; i <= schT - 1 && j <= conT - 1; ++j)
{
if (del == ways[j])
{
continue;
}
int res0 = _find(ways[j].u, p), res1 = _find(ways[j].v, p);
if (res0 == res1)
{
continue;
}
p[res0] = res1;
ans += ways[j].d;
++i;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int caseT; cin >> caseT;
for (int caseC = 1; caseC <= caseT; ++caseC)
{
int schT, conT; cin >> schT >> conT;
vector<int> p(schT + 1);
vector<bool> mark(conT, false);
vector<edge> ways, pickeds;
for (int conC = 1; conC <= conT; ++conC)
{
int u, v, d; cin >> u >> v >> d;
edge temp{u, v, d};
ways.push_back(temp);
}
sort(ways.begin(), ways.end());
for (int i = 1; i <= schT; ++i)
{
p[i] = i;
}
int total0 = 0, total1 = 2147483647;
vector<vector<int>> g(schT + 1);
for (int i = 1, j = 0; i <= schT - 1 && j <= conT - 1; ++j)
{
int res0 = _find(ways[j].u, p), res1 = _find(ways[j].v, p);
if (res0 == res1)
{
continue;
}
p[res0] = res1;
g[ways[j].u].push_back(ways[j].v);
g[ways[j].v].push_back(ways[j].u);
mark[j] = true;
total0 += ways[j].d;
pickeds.push_back(ways[j]);
++i;
}
int size0 = pickeds.size();
for (int i = 0; i <= size0 - 1; ++i)
{
int res = solve(schT, pickeds[i], ways);
if (res <= total1 && res >= total0)
{
total1 = res;
}
}
cout << total0 << ' ' << total1 << '\n';
}
cout.flush();
return 0;
}