Submission #7475603


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

const int N = 200005, INF = 2000000000;
int n, Q, pre_tag[N], suf_tag[N], fa[N];
struct edge {
	int u, v, w;
};
bool operator<(const edge &a, const edge &b) {
	return a.w < b.w;
}

int find(int x) {
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

int main() {
	scanf("%d%d", &n, &Q);
	for (int i = 0; i < n; ++i)
		pre_tag[i] = suf_tag[i] = INF;
	vector<edge> e;
	for (int i = 1; i <= Q; ++i) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		suf_tag[a] = min(suf_tag[a], c - 2 * a + 1);
		pre_tag[a] = min(pre_tag[a], c + 2 * (n - a) + 1);
		suf_tag[b] = min(suf_tag[b], c - 2 * b + 2);
		pre_tag[b] = min(pre_tag[b], c + 2 * (n - b) + 2);
		e.push_back((edge){a, b, c});
	}
	for (int i = 1; i < n; ++i)
		suf_tag[i] = min(suf_tag[i], suf_tag[i - 1]);
	for (int i = n - 2; i >= 0; --i)
		pre_tag[i] = min(pre_tag[i], pre_tag[i + 1]);
	for (int i = 0; i < n; ++i)
		e.push_back({i, (i + 1) % n, min(suf_tag[i], pre_tag[i]) + 2 * i});
	sort(e.begin(), e.end());
	for (int i = 0; i < n; ++i)
		fa[i] = i;
	long long ans = 0;
	for (const auto &p : e) {
		int s = find(p.u), t = find(p.v);
		if (s != t)
			fa[s] = t, ans += p.w;
	}
	printf("%lld\n", ans);
}

Submission Info

Submission Time
Task G - Zigzag MST
User rushcheyo
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 1250 Byte
Status AC
Exec Time 101 ms
Memory 11628 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:18: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:24:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a, &b, &c);
                              ^

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 3
AC × 36
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, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt AC 1 ms 256 KB
01-02.txt AC 65 ms 4212 KB
01-03.txt AC 95 ms 10096 KB
01-04.txt AC 22 ms 7792 KB
01-05.txt AC 15 ms 6512 KB
01-06.txt AC 19 ms 7152 KB
01-07.txt AC 17 ms 5748 KB
01-08.txt AC 18 ms 5748 KB
01-09.txt AC 24 ms 6132 KB
01-10.txt AC 60 ms 9328 KB
01-11.txt AC 84 ms 8560 KB
01-12.txt AC 96 ms 8432 KB
01-13.txt AC 101 ms 9456 KB
01-14.txt AC 96 ms 8816 KB
01-15.txt AC 98 ms 9328 KB
01-16.txt AC 95 ms 9584 KB
01-17.txt AC 97 ms 9200 KB
01-18.txt AC 73 ms 11244 KB
01-19.txt AC 17 ms 6388 KB
01-20.txt AC 21 ms 6260 KB
01-21.txt AC 42 ms 6516 KB
01-22.txt AC 83 ms 9200 KB
01-23.txt AC 88 ms 9712 KB
01-24.txt AC 17 ms 8560 KB
01-25.txt AC 91 ms 11628 KB
01-26.txt AC 22 ms 7664 KB
01-27.txt AC 26 ms 7024 KB
01-28.txt AC 63 ms 9072 KB
01-29.txt AC 79 ms 8816 KB
01-30.txt AC 91 ms 9840 KB
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB
sample-03.txt AC 1 ms 256 KB