CODE FESTIVAL 2016 Final

Submission #1418535

Source codeソースコード

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

using Int = long long;
#define rep(i, n) for (Int i = 0; i < Int(n); ++i)
#define rep1(i, n) for (Int i = 1; i <= Int(n); ++i)
void prtln() {} template<class T, class... Ts> void prtln(T&& t, Ts&&... ts) { cout << t << "¥n "[!!sizeof...(Ts)]; prtln(ts...); }

class UnionFind {
private:
	vector<int> par, rank;

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

public:
	UnionFind(int size) : par(size), rank(size, 0) {
		for (int i = 0; i < size; ++i) par[i] = i;
	}

	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return;
		if (rank[x] < rank[y]) {
			par[x] = y;
		} else {
			par[y] = x;
			if (rank[x] == rank[y]) ++rank[x];
		}
	}

	bool isSame(int x, int y) { return find(x) == find(y); }
};

struct Edge {
    Int a, b, c;
    Edge(Int a, Int b, Int c) : a(a), b(b), c(c) {}
    bool operator<(const Edge& o) const { return c > o.c; }
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);
    Int n, q; cin >> n >> q;
    priority_queue<Edge> es;
    rep(i, q) {
        Int a, b, c; cin >> a >> b >> c;
        es.emplace(a, b, c);
    }

    UnionFind uf(n);

    Int cnt = 0, cost = 0;

    while (cnt < n-1) {
        Edge e = es.top(); es.pop();
        if (!uf.isSame(e.a, e.b)) {
            cost += e.c;
            uf.unite(e.a, e.b);
            ++cnt;
        }
        es.emplace(e.b, (e.a+1)%n, e.c+1);
    }
    cout << cost << endl;
}

Submission

Task問題 G - Zigzag MST
User nameユーザ名 tomcatowl
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 TLE
Score得点 0
Source lengthソースコード長 1530 Byte
File nameファイル名
Exec time実行時間 ms
Memory usageメモリ使用量 -

Test case

Set

Set name Score得点 / Max score Cases
sample - sample-01.txt,sample-02.txt,sample-03.txt
all 0 / 1300 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

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01-01.txt AC 1 ms 256 KB
01-02.txt AC 50 ms 8048 KB
01-03.txt AC 172 ms 7276 KB
01-04.txt AC 9 ms 1792 KB
01-05.txt AC 16 ms 1792 KB
01-06.txt AC 22 ms 1792 KB
01-07.txt AC 39 ms 1792 KB
01-08.txt AC 109 ms 1920 KB
01-09.txt AC 160 ms 2172 KB
01-10.txt AC 333 ms 4596 KB
01-11.txt AC 173 ms 7792 KB
01-12.txt AC 329 ms 7660 KB
01-13.txt AC 468 ms 7536 KB
01-14.txt AC 419 ms 8048 KB
01-15.txt AC 350 ms 7792 KB
01-16.txt AC 329 ms 7148 KB
01-17.txt AC 355 ms 7660 KB
01-18.txt TLE
01-19.txt AC 22 ms 1792 KB
01-20.txt AC 128 ms 1920 KB
01-21.txt AC 262 ms 3316 KB
01-22.txt AC 334 ms 7532 KB
01-23.txt AC 368 ms 8304 KB
01-24.txt TLE
01-25.txt TLE
01-26.txt AC 15 ms 1792 KB
01-27.txt TLE
01-28.txt TLE
01-29.txt TLE
01-30.txt TLE
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB
sample-03.txt AC 1 ms 256 KB