Submission #1004143
Source Code Expand
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>
typedef long long signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for(int i = (l) ; i < (r); i++)
#define incII(i, l, r) for(int i = (l) ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r) ; i >= (l); i--)
#define inc( i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec( i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
template<typename T> void swap(T & x, T & y) { T t = x; x = y; y = t; return; }
template<typename T> T abs(T x) { return (0 <= x ? x : -x); }
template<typename T> T max(T a, T b) { return (b <= a ? a : b); }
template<typename T> T min(T a, T b) { return (a <= b ? a : b); }
template<typename T> bool setmin(T & a, T b) { if(a <= b) { return false; } else { a = b; return true; } }
template<typename T> bool setmax(T & a, T b) { if(b <= a) { return false; } else { a = b; return true; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
// ---- ----
const int N = 200000;
int n, q;
LL a[N], b[N], c[N];
LL ec[N];
const LL INF = 2000000000LL;
std::pair< LL, std::pair<int, int> > p[N * 2];
int id[N];
std::vector<int> vec[N];
void uf_init() {
inc(i, n) {
id[i] = i;
vec[i].push_back(i);
}
}
bool unite(int x, int y) {
int vx = id[x], vy = id[y];
if(vx == vy) { return false; }
if(vec[vx].size() < vec[vy].size()) { swap(vx, vy); }
for(auto && e : vec[vy]) {
id[e] = vx;
vec[vx].push_back(e);
}
vec[vy].clear();
return true;
}
int main() {
scanf("%d%d", &n, &q);
inc(i, q) { scanf("%lld%lld%lld", &a[i], &b[i], &c[i]); }
inc(i, n) { ec[i] = INF; }
inc(i, q) {
setmin(ec[ a[i] ], c[i] + 1);
setmin(ec[ b[i] ], c[i] + 2);
}
LL pre = INF;
inc(i, n * 2) {
setmin(ec[i % n], pre + 2);
pre = ec[i % n];
}
inc(i, n) {
p[i] = std::make_pair(ec[i], std::make_pair(i, (i + 1) % n));
}
inc(i, q) {
p[n + i] = std::make_pair(c[i], std::make_pair(a[i], b[i]));
}
std::sort(p, p + n + q);
uf_init();
LL ans = 0;
inc(i, n + q) {
if(unite(p[i].second.first, p[i].second.second)) { ans += p[i].first; }
}
printf("%lld\n", ans);
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
FF256grhy |
Language |
C++14 (GCC 5.4.1) |
Score |
1300 |
Code Size |
2531 Byte |
Status |
AC |
Exec Time |
153 ms |
Memory |
26360 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:66:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &q);
^
./Main.cpp:67:56: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
inc(i, q) { scanf("%lld%lld%lld", &a[i], &b[i], &c[i]); }
^
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
1300 / 1300 |
Status |
|
|
Set Name |
Test Cases |
sample |
sample-01.txt, sample-02.txt, sample-03.txt |
all |
sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt |
Case Name |
Status |
Exec Time |
Memory |
01-01.txt |
AC |
8 ms |
4992 KB |
01-02.txt |
AC |
76 ms |
12800 KB |
01-03.txt |
AC |
127 ms |
25980 KB |
01-04.txt |
AC |
49 ms |
17788 KB |
01-05.txt |
AC |
41 ms |
17788 KB |
01-06.txt |
AC |
46 ms |
17788 KB |
01-07.txt |
AC |
49 ms |
18148 KB |
01-08.txt |
AC |
52 ms |
18428 KB |
01-09.txt |
AC |
59 ms |
18680 KB |
01-10.txt |
AC |
105 ms |
22264 KB |
01-11.txt |
AC |
112 ms |
19548 KB |
01-12.txt |
AC |
147 ms |
26108 KB |
01-13.txt |
AC |
148 ms |
26104 KB |
01-14.txt |
AC |
150 ms |
26232 KB |
01-15.txt |
AC |
145 ms |
26104 KB |
01-16.txt |
AC |
152 ms |
26232 KB |
01-17.txt |
AC |
145 ms |
26104 KB |
01-18.txt |
AC |
120 ms |
25596 KB |
01-19.txt |
AC |
47 ms |
18232 KB |
01-20.txt |
AC |
70 ms |
18940 KB |
01-21.txt |
AC |
90 ms |
20856 KB |
01-22.txt |
AC |
153 ms |
26360 KB |
01-23.txt |
AC |
144 ms |
26360 KB |
01-24.txt |
AC |
45 ms |
18300 KB |
01-25.txt |
AC |
133 ms |
25596 KB |
01-26.txt |
AC |
50 ms |
17788 KB |
01-27.txt |
AC |
59 ms |
18552 KB |
01-28.txt |
AC |
109 ms |
22904 KB |
01-29.txt |
AC |
117 ms |
20092 KB |
01-30.txt |
AC |
142 ms |
25848 KB |
sample-01.txt |
AC |
8 ms |
4992 KB |
sample-02.txt |
AC |
8 ms |
4992 KB |
sample-03.txt |
AC |
8 ms |
4992 KB |