Submission #995321


Source Code Expand

#include <cstdio>
#include <cassert>
#include <algorithm>
#include <set>
using namespace std;

typedef long long llong;

const int N = 1000500;

int n;

struct chain {
    int a, b, c;
    void promote() {
        if ((++a) >= n)
            a = 0;
        if ((++b) >= n)
            b = 0;
        c += 2;
    }
};

int par[N], rnk[N];

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

bool merge(int a, int b) {
    a = get(a);
    b = get(b);
    if (a == b)
        return false;
    if (rnk[a] > rnk[b]) {
        swap(a, b);
    }
    if (rnk[a] == rnk[b])
        rnk[b]++;
    par[a] = b;
    return true;
}

int cpt = 0;
chain C[N];

struct evt {
    int t;
    int i;
};

int ept = 0;
evt E[N];

void add_chain(int a, int b, int c) {
    E[ept++] = {c, cpt};
    C[cpt++] = {a, b, c};
}

set<int> ids[2];

int main() {
    int q;
    int mn = 1e9 + 42;
    scanf("%d %d", &n, &q);
    for (int i = 0; i < q; i++) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        mn = min(mn, c);
        add_chain(a, b, c);
        add_chain((a + 1) % n, b, c + 1);
    }
    sort(E, E + ept, [] (evt a, evt b) { return a.t < b.t; } );
    int pt = 0;
    llong ans = 0;

    for (int i = 0; i < n; i++)
        par[i] = i;

    int cnt = 0;
    for (int t = mn; cnt < n - 1; t++) {
        while (pt != ept && E[pt].t <= t) {
            ids[t % 2].insert(E[pt].i);
            pt++;
        }
        auto& cur = ids[t % 2];
        vector<int> todel;
        for (int i : cur) {
            if (!merge(C[i].a, C[i].b)) {
                todel.push_back(i);
            } else {
                ans += C[i].c;
                assert(C[i].c == t);
                C[i].promote();
                cnt++;
            }
        }
        for (int i : todel)
            cur.erase(i);
    }
    printf("%lld\n", ans);
}

Submission Info

Submission Time
Task G - Zigzag MST
User Zlobober
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 1960 Byte
Status AC
Exec Time 208 ms
Memory 28476 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:65:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &q);
                           ^
./Main.cpp:68:38: 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 × 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 2 ms 256 KB
01-02.txt AC 80 ms 8064 KB
01-03.txt AC 93 ms 8832 KB
01-04.txt AC 7 ms 1024 KB
01-05.txt AC 7 ms 1024 KB
01-06.txt AC 7 ms 1024 KB
01-07.txt AC 6 ms 1024 KB
01-08.txt AC 7 ms 1152 KB
01-09.txt AC 11 ms 1664 KB
01-10.txt AC 52 ms 5504 KB
01-11.txt AC 90 ms 8832 KB
01-12.txt AC 96 ms 9472 KB
01-13.txt AC 96 ms 9472 KB
01-14.txt AC 97 ms 9472 KB
01-15.txt AC 97 ms 9472 KB
01-16.txt AC 97 ms 9472 KB
01-17.txt AC 111 ms 9472 KB
01-18.txt AC 201 ms 20072 KB
01-19.txt AC 7 ms 1024 KB
01-20.txt AC 13 ms 2048 KB
01-21.txt AC 72 ms 9740 KB
01-22.txt AC 208 ms 28472 KB
01-23.txt AC 207 ms 28476 KB
01-24.txt AC 14 ms 1536 KB
01-25.txt AC 111 ms 8832 KB
01-26.txt AC 7 ms 1024 KB
01-27.txt AC 22 ms 2432 KB
01-28.txt AC 132 ms 10244 KB
01-29.txt AC 188 ms 13236 KB
01-30.txt AC 204 ms 14840 KB
sample-01.txt AC 2 ms 256 KB
sample-02.txt AC 2 ms 384 KB
sample-03.txt AC 2 ms 256 KB