This repository was archived by the owner on Sep 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1013.cpp
More file actions
182 lines (169 loc) · 3.57 KB
/
1013.cpp
File metadata and controls
182 lines (169 loc) · 3.57 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
auto nextInt () -> int;
auto readN (int *array, int n) -> void;
template <typename T>
auto print (const T &) -> void;
template <typename T>
auto println (const T &) -> void;
template <typename T>
auto printsp (const T &) -> void;
template <typename T>
struct Greater;
template <typename T>
struct Less;
constexpr int kMaxn = 5e5 + 10;
struct Edge {
union {
struct {
int from;
int to;
int cost, costb;
};
int v[4];
};
bool visited;
auto operator< (const Edge &rhs) const -> bool {
return cost < rhs.cost;
}
};
Edge edges[kMaxn];
std::basic_string<int> eid[kMaxn];
int dfs[kMaxn], low[kMaxn];
int curr;
auto tarjan (int ix, int parent) -> void {
low[ix] = dfs[ix] = ++curr;
for (auto &id : eid[ix]) {
auto &edge = edges[id];
if (edge.visited) continue;
edge.visited = true;
auto to = edge.from == ix ? edge.to : edge.from;
if (dfs[to] == 0) {
tarjan(to, ix);
if (low[to] > dfs[ix]) {
edge.cost = edge.costb;
}
low[ix] = std::min(low[ix], low[to]);
} else {
low[ix] = std::min(low[ix], dfs[to]);
}
}
}
int parent[kMaxn];
auto init (int n) {
if (n < 0) return;
parent[n] = n;
init(n - 1);
}
auto get (int n) {
if (parent[n] == n) return n;
return parent[n] = get(parent[n]);
}
auto join (int x, int y) {
int a = get(x), b = get(y);
if (a == b) return false;
parent[a] = b;
return true;
}
template <typename T>
auto sort (T *array, int l, int r) -> void {
if (r - l <= 1) return;
int mid = (l + r) / 2;
sort(array, l, mid);
sort(array, mid, r);
T tmp[r - l + 1];
int s = 0;
int p = l;
int q = mid;
while (s < r - l) {
if (p < mid && (q >= r || array[p] < array[q])) {
tmp[s] = array[p];
++s; ++p;
} else {
tmp[s] = array[q];
++s; ++q;
}
}
for (int i = 0; i < r - l; ++i) array[i + l] = tmp[i];
}
auto main () -> int {
int n = nextInt(), m = nextInt();
for (int i = 0; i < m; ++i) {
auto &e = edges[i];
readN(e.v, 4);
eid[e.from] += i;
eid[e.to] += i;
}
tarjan(1, 1);
sort(edges, 0, m);
init(n);
unsigned long long ans = 0;
for (int i = 0; i < m; ++i) {
auto &e = edges[i];
if (join(e.from, e.to)) {
ans += e.cost;
}
}
println(ans);
return 0;
}
auto nextInt () -> int {
int i = 0, sign = 1;
char c;
while (!isdigit(c = getchar())) if (c == '-') sign = -1;
do {
i = i * 10 + c - '0';
} while (isdigit(c = getchar()));
return i * sign;
}
auto readN (int *array, int n) -> void {
for (int i = 0; i < n; ++i) array[i] = nextInt();
}
template <>
auto print<int> (const int &val) -> void {
printf("%d", val);
}
template <>
auto print<char> (const char &val) -> void {
putchar(val);
}
template <>
auto print<char *> (char * const &val) -> void {
printf("%s", val);
}
template <>
auto print<const char *> (const char * const &val) -> void {
printf("%s", val);
}
template <>
auto print<long long> (const long long &val) -> void {
printf("%lld", val);
}
template <>
auto print<unsigned long long> (const unsigned long long &val) -> void {
printf("%llu", val);
}
template <typename T>
auto println (const T &val) -> void {
print(val);
putchar('\n');
}
template <typename T>
auto printsp (const T &val) -> void {
print(val);
putchar(' ');
}
template <typename T>
struct Greater {
auto operator() (const T &lhs, const T &rhs) const -> bool {
return lhs > rhs;
}
};
template <typename T>
struct Less {
auto operator() (const T &lhs, const T &rhs) const -> bool {
return lhs < rhs;
}
};