Submission #993689


Source Code Expand

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

struct union_find {
	vector<int> v;
	union_find(int n) : v(n, -1) {}
	int find(int x) { return v[x] < 0 ? x : v[x] = find(v[x]); }
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return;
		if (-v[x] < -v[y]) swap(x, y);
		v[x] += v[y]; v[y] = x;
	}
	bool root(int x) { return v[x] < 0; }
	bool same(int x, int y) { return find(x) == find(y); }
	int size(int x) { return -v[find(x)]; }
};

int main() {
	int N, Q; cin >> N >> Q;
	vector<pair<int, i_i> > p;
	while (Q--) {
		int a, b, c; scanf("%d%d%d", &a, &b, &c);
		p.pb(make_pair(c, i_i(a, b)));
		p.pb(make_pair(c + 1, i_i(a + 1, b)));
	}
	sort(p.begin(), p.end());
	Q = p.size();
	int k = 0;
	union_find uf(N);
	ll ans = 0;
	vector<i_i> E1, E2;
	for (int c = p[0].first; uf.size(0) < N; c++) {
		for (; k < Q && p[k].first == c; k++)
			E1.pb(i_i(p[k].second));
		vector<i_i> _E;
		for (i_i e: E1) {
			if (uf.same(e.first % N, e.second % N)) continue;
			uf.unite(e.first % N, e.second % N);
			ans += c;
			e.first++; e.second++;
			_E.pb(e);
		}
		E1 = _E;
		swap(E1, E2);
	}
	cout << ans << endl;
}

Submission Info

Submission Time
Task G - Zigzag MST
User sugim48
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 1860 Byte
Status AC
Exec Time 127 ms
Memory 10860 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:55:43: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   int a, b, c; scanf("%d%d%d", &a, &b, &c);
                                           ^

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 3
AC × 33
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 3 ms 256 KB
01-02.txt AC 89 ms 6512 KB
01-03.txt AC 110 ms 6640 KB
01-04.txt AC 16 ms 1024 KB
01-05.txt AC 17 ms 1024 KB
01-06.txt AC 16 ms 1024 KB
01-07.txt AC 14 ms 1024 KB
01-08.txt AC 12 ms 1024 KB
01-09.txt AC 15 ms 1404 KB
01-10.txt AC 57 ms 3444 KB
01-11.txt AC 102 ms 6512 KB
01-12.txt AC 107 ms 6512 KB
01-13.txt AC 106 ms 6512 KB
01-14.txt AC 107 ms 6512 KB
01-15.txt AC 107 ms 6512 KB
01-16.txt AC 107 ms 6512 KB
01-17.txt AC 107 ms 6512 KB
01-18.txt AC 105 ms 10860 KB
01-19.txt AC 15 ms 1024 KB
01-20.txt AC 11 ms 1152 KB
01-21.txt AC 43 ms 4212 KB
01-22.txt AC 122 ms 10860 KB
01-23.txt AC 121 ms 10860 KB
01-24.txt AC 22 ms 1404 KB
01-25.txt AC 95 ms 6512 KB
01-26.txt AC 16 ms 1024 KB
01-27.txt AC 21 ms 1532 KB
01-28.txt AC 84 ms 5616 KB
01-29.txt AC 121 ms 7660 KB
01-30.txt AC 127 ms 8172 KB
sample-01.txt AC 3 ms 256 KB
sample-02.txt AC 3 ms 256 KB
sample-03.txt AC 3 ms 256 KB