Submission #1818584


Source Code Expand

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct TEdge
{
    int u, v, w;
} a[100005];
int n, m, k, c[100005], dsu[200005];
long long ans = 0;

inline bool operator<(const TEdge &a, const TEdge &b)
{
    return a.w < b.w;
}

int trace(int u)
{
    return (dsu[u] < 0 ? u : dsu[u] = trace(dsu[u]));
}

bool unity(int u, int v)
{
    if ((u = trace(u)) == (v = trace(v)))
        return false;
    if (dsu[u] > dsu[v])
        swap(u, v);
    dsu[u] += dsu[v];
    dsu[v] = u;
    return true;
}

int main()
{
    memset(dsu, -1, sizeof(dsu));
    scanf("%d%d%d", &n, &m, &k);
    --k;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", c + i);
        if (c[i] == 0)
            c[i] = n + i;
    }
    for (int i = 1; i <= m; i++)
        scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
    sort(a + 1, a + m + 1);
    for (int i = 1; k && i <= m; i++)
        if (unity(c[a[i].u], c[a[i].v]))
        {
            ans += a[i].w;
            --k;
        }
    printf("%lld", k ? -1 : ans);
}

Submission Info

Submission Time
Task A - Colorful MST
User kuroni
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1110 Byte
Status AC
Exec Time 45 ms
Memory 2560 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:38:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &n, &m, &k);
                                ^
./Main.cpp:42:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", c + i);
                           ^
./Main.cpp:47:51: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
                                                   ^

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 0 / 0 100 / 100 100 / 100 200 / 200 300 / 300
Status
AC × 4
AC × 7
AC × 12
AC × 7
AC × 28
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt
Subtask1 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt
Subtask2 s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt
Subtask3 00_example_02.txt, s3_13.txt, s3_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt
Subtask4 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s3_13.txt, s3_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s4_19.txt, s4_20.txt, s4_21.txt, s4_22.txt, s4_23.txt, s4_24.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 1 ms 1024 KB
00_example_02.txt AC 1 ms 1024 KB
00_example_03.txt AC 1 ms 1024 KB
00_example_04.txt AC 2 ms 1024 KB
s1_01.txt AC 1 ms 1024 KB
s1_02.txt AC 44 ms 2560 KB
s1_03.txt AC 1 ms 1024 KB
s1_04.txt AC 1 ms 1024 KB
s1_05.txt AC 24 ms 1920 KB
s1_06.txt AC 44 ms 2560 KB
s2_07.txt AC 7 ms 1280 KB
s2_08.txt AC 45 ms 2560 KB
s2_09.txt AC 33 ms 2304 KB
s2_10.txt AC 31 ms 2176 KB
s2_11.txt AC 42 ms 2560 KB
s2_12.txt AC 41 ms 2560 KB
s3_13.txt AC 7 ms 1280 KB
s3_14.txt AC 42 ms 2560 KB
s3_15.txt AC 32 ms 2304 KB
s3_16.txt AC 31 ms 2176 KB
s3_17.txt AC 41 ms 2560 KB
s3_18.txt AC 39 ms 2560 KB
s4_19.txt AC 7 ms 1280 KB
s4_20.txt AC 41 ms 2560 KB
s4_21.txt AC 33 ms 2304 KB
s4_22.txt AC 32 ms 2176 KB
s4_23.txt AC 43 ms 2560 KB
s4_24.txt AC 42 ms 2560 KB