Submission #7468798


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef pair<ll, l_l> lll;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};

priority_queue<lll, vector<lll>, greater<lll>> que;

int main() {
    ll N, Q;
    cin >> N >> Q;
    UnionFind uni(N);
    while(Q--) {
        ll A, B, C;
        cin >> A >> B >> C;
        que.push({C, {A, B}});
        que.push({C+1, {(A+1)%N, B}});
    }
    ll ans = 0;
    while(uni.size(1) != N) {
        lll now = que.top();
        que.pop();
        ll a = now.second.first;
        ll b = now.second.second;
        ll c = now.first;
        if(uni.merge(a, b)) {
            ans += c;
            que.push({c+2, {(a+1)%N, (b+1)%N}});
        }
    }
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task G - Zigzag MST
User kort0n
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 2077 Byte
Status AC
Exec Time 319 ms
Memory 17644 KB

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 158 ms 14572 KB
01-03.txt AC 230 ms 15980 KB
01-04.txt AC 11 ms 3328 KB
01-05.txt AC 11 ms 3328 KB
01-06.txt AC 14 ms 3328 KB
01-07.txt AC 15 ms 3328 KB
01-08.txt AC 23 ms 3456 KB
01-09.txt AC 42 ms 4220 KB
01-10.txt AC 146 ms 11120 KB
01-11.txt AC 224 ms 14572 KB
01-12.txt AC 259 ms 17388 KB
01-13.txt AC 254 ms 16236 KB
01-14.txt AC 258 ms 17388 KB
01-15.txt AC 255 ms 15980 KB
01-16.txt AC 257 ms 16748 KB
01-17.txt AC 260 ms 17644 KB
01-18.txt AC 256 ms 16492 KB
01-19.txt AC 12 ms 3328 KB
01-20.txt AC 35 ms 3584 KB
01-21.txt AC 114 ms 7412 KB
01-22.txt AC 291 ms 15980 KB
01-23.txt AC 297 ms 17516 KB
01-24.txt AC 25 ms 4220 KB
01-25.txt AC 274 ms 17260 KB
01-26.txt AC 12 ms 3328 KB
01-27.txt AC 38 ms 4220 KB
01-28.txt AC 207 ms 10864 KB
01-29.txt AC 305 ms 15468 KB
01-30.txt AC 319 ms 17260 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