Submission #993681


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using D = double;
using uint = unsigned int;

#ifdef WIN32
    #define LLD "%I64d"
#else
    #define LLD "%lld"
#endif

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

const int maxn = 200005;

int p[maxn], s[maxn];
set<pair<int, pair<int, int>>> edges;
int n, m;

inline int find(int a)
{
    return p[a] == a ? a : p[a] = find(p[a]);
}

inline bool unite(int a, int b)
{
    a = find(a);
    b = find(b);
    if (a == b) return false;
    if (s[a] < s[b]) swap(a, b);
    p[b] = a;
    if (s[a] == s[b]) s[a]++;
    return true;
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        edges.insert({c, {a, b}});
        edges.insert({c + 1, {a + 1, b}});
    }
    for (int i = 0; i < n; i++) p[i] = i, s[i] = 0;
    ll answer = 0;
    while (!edges.empty())
    {
        int cura = edges.begin()->se.fi % n;
        int curb = edges.begin()->se.se % n;
        int curc = edges.begin()->fi;
        edges.erase(edges.begin());
        if (unite(cura, curb))
        {
            edges.insert({curc + 2, {cura + 1, curb + 1}});
            answer += curc;
        }
    }
    cout << answer << endl;
    return 0;
}

Submission Info

Submission Time
Task G - Zigzag MST
User KAN
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 1440 Byte
Status AC
Exec Time 309 ms
Memory 26752 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:46:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
                          ^
./Main.cpp:50:36: 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 3 ms 256 KB
01-02.txt AC 211 ms 25216 KB
01-03.txt AC 242 ms 26752 KB
01-04.txt AC 15 ms 1792 KB
01-05.txt AC 15 ms 1792 KB
01-06.txt AC 18 ms 1792 KB
01-07.txt AC 21 ms 1792 KB
01-08.txt AC 25 ms 1920 KB
01-09.txt AC 37 ms 3072 KB
01-10.txt AC 148 ms 14336 KB
01-11.txt AC 263 ms 25984 KB
01-12.txt AC 264 ms 26752 KB
01-13.txt AC 262 ms 26752 KB
01-14.txt AC 265 ms 26752 KB
01-15.txt AC 264 ms 26752 KB
01-16.txt AC 271 ms 26752 KB
01-17.txt AC 282 ms 26752 KB
01-18.txt AC 72 ms 1792 KB
01-19.txt AC 17 ms 1792 KB
01-20.txt AC 33 ms 2048 KB
01-21.txt AC 100 ms 8960 KB
01-22.txt AC 304 ms 26752 KB
01-23.txt AC 299 ms 26752 KB
01-24.txt AC 24 ms 3328 KB
01-25.txt AC 262 ms 26752 KB
01-26.txt AC 16 ms 1792 KB
01-27.txt AC 38 ms 2944 KB
01-28.txt AC 177 ms 17280 KB
01-29.txt AC 279 ms 26112 KB
01-30.txt AC 309 ms 26752 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