Submission #2107780


Source Code Expand

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

struct union_find{
    VI par;

    void init(int n){
        par.resize(n);
        REP(i,n) par[i] = i;
    }

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

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

    void merge(int x, int y){
        x = find(x);
        y = find(y);
        if (x == y) return;
        par[x] = y;
    }
};

int main() {
    int n, m, k;
    cin >> n >> m >> k;

    VI c(n);
    REP(i,n){
        scanf("%d", &c[i]);
        c[i]--;
    }

    VI a(m), b(m);
    VL w(m);
    REP(i,m){
        scanf("%d %d %lld", &a[i], &b[i], &w[i]);
        a[i]--;
        b[i]--;
    }

    VI emp(n, -1);
    int x = k;
    REP(i,n){
        if (c[i] == -1){
            emp[i] = x++;
        }
    }

    VI ind(m);
    REP(i,m) ind[i] = i;

    sort(ALL(ind), [&](int x, int y){ return w[x] < w[y]; });

    union_find uf;
    uf.init(x);

    ll ans = 0;
    x = 0;

    REP(i,m){
        int p = (c[a[ind[i]]] == -1 ? emp[a[ind[i]]] : c[a[ind[i]]]);
        int q = (c[b[ind[i]]] == -1 ? emp[b[ind[i]]] : c[b[ind[i]]]);
        if (!uf.same(p, q)){
            x++;
            ans += w[ind[i]];
            uf.merge(p, q);

            if (x == k-1){
                cout << ans << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;

    return 0;
}

Submission Info

Submission Time
Task A - Colorful MST
User TangentDay
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2108 Byte
Status AC
Exec Time 57 ms
Memory 3712 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:61:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &c[i]);
                           ^
./Main.cpp:68:49: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %lld", &a[i], &b[i], &w[i]);
                                                 ^

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 6 ms 768 KB
00_example_02.txt AC 1 ms 256 KB
00_example_03.txt AC 1 ms 256 KB
00_example_04.txt AC 1 ms 256 KB
s1_01.txt AC 1 ms 256 KB
s1_02.txt AC 53 ms 3328 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 26 ms 2176 KB
s1_06.txt AC 53 ms 3328 KB
s2_07.txt AC 8 ms 640 KB
s2_08.txt AC 55 ms 3328 KB
s2_09.txt AC 37 ms 2432 KB
s2_10.txt AC 35 ms 2176 KB
s2_11.txt AC 48 ms 3072 KB
s2_12.txt AC 47 ms 3072 KB
s3_13.txt AC 7 ms 768 KB
s3_14.txt AC 52 ms 3712 KB
s3_15.txt AC 36 ms 2688 KB
s3_16.txt AC 36 ms 2560 KB
s3_17.txt AC 53 ms 3712 KB
s3_18.txt AC 46 ms 3456 KB
s4_19.txt AC 8 ms 640 KB
s4_20.txt AC 48 ms 3200 KB
s4_21.txt AC 37 ms 2560 KB
s4_22.txt AC 40 ms 2432 KB
s4_23.txt AC 57 ms 3456 KB
s4_24.txt AC 49 ms 3200 KB