Submission #992896
Source Code Expand
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = (ll)2e9;
const int N = 500500;
int n, q;
int m;
struct Edge
{
int v, u;
ll w;
Edge() : v(), u(), w() {}
Edge (int _v, int _u, ll _w) : v(_v), u(_u), w(_w) {}
bool operator < (const Edge &e) const
{
return w < e.w;
}
};
Edge ed[N];
ll a[N];
int par[N];
int sz[N];
int getPar(int v)
{
return (par[v] == -1 ? v : par[v] = getPar(par[v]));
}
bool unite(int v, int u)
{
v = getPar(v);
u = getPar(u);
if (v == u) return false;
if (sz[v] < sz[u]) swap(v, u);
sz[v] += sz[u];
par[u] = v;
return true;
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
a[i] = INF;
while(q--)
{
int v, u;
ll w;
scanf("%d%d%lld", &v, &u, &w);
ed[m++] = Edge(v, u, w);
a[v] = min(a[v], w + 1);
a[u] = min(a[u], w + 2);
}
for (int k = 0; k < 2; k++)
{
a[0] = min(a[0], a[n - 1] + 2);
for (int i = 1; i < n; i++)
a[i] = min(a[i], a[i - 1] + 2);
}
for (int i = 0; i < n; i++)
ed[m++] = Edge(i, (i + 1) % n, a[i]);
sort(ed, ed + m);
for (int i = 0; i < n; i++)
{
par[i] = -1;
sz[i] = 1;
}
ll ans = 0;
for (int i = 0; i < m; i++)
if (unite(ed[i].v, ed[i].u))
ans += ed[i].w;
printf("%lld\n", ans);
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
Um_nik |
Language |
C++14 (GCC 5.4.1) |
Score |
1300 |
Code Size |
1464 Byte |
Status |
AC |
Exec Time |
107 ms |
Memory |
11264 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:54: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:61:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%lld", &v, &u, &w);
^
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 |
10 ms |
8064 KB |
01-02.txt |
AC |
75 ms |
8064 KB |
01-03.txt |
AC |
103 ms |
11264 KB |
01-04.txt |
AC |
29 ms |
11264 KB |
01-05.txt |
AC |
22 ms |
11136 KB |
01-06.txt |
AC |
26 ms |
11136 KB |
01-07.txt |
AC |
24 ms |
11264 KB |
01-08.txt |
AC |
24 ms |
11264 KB |
01-09.txt |
AC |
27 ms |
11264 KB |
01-10.txt |
AC |
64 ms |
11264 KB |
01-11.txt |
AC |
92 ms |
9600 KB |
01-12.txt |
AC |
103 ms |
11136 KB |
01-13.txt |
AC |
101 ms |
11136 KB |
01-14.txt |
AC |
105 ms |
11136 KB |
01-15.txt |
AC |
107 ms |
11136 KB |
01-16.txt |
AC |
104 ms |
11264 KB |
01-17.txt |
AC |
101 ms |
11264 KB |
01-18.txt |
AC |
83 ms |
11136 KB |
01-19.txt |
AC |
24 ms |
11264 KB |
01-20.txt |
AC |
24 ms |
11136 KB |
01-21.txt |
AC |
42 ms |
11136 KB |
01-22.txt |
AC |
83 ms |
11136 KB |
01-23.txt |
AC |
85 ms |
11136 KB |
01-24.txt |
AC |
25 ms |
11136 KB |
01-25.txt |
AC |
103 ms |
11264 KB |
01-26.txt |
AC |
28 ms |
11264 KB |
01-27.txt |
AC |
29 ms |
11136 KB |
01-28.txt |
AC |
65 ms |
11136 KB |
01-29.txt |
AC |
88 ms |
9856 KB |
01-30.txt |
AC |
94 ms |
11264 KB |
sample-01.txt |
AC |
10 ms |
8064 KB |
sample-02.txt |
AC |
10 ms |
8064 KB |
sample-03.txt |
AC |
10 ms |
8064 KB |