提出 #53110200


ソースコード 拡げる

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <string>

#include <cmath>
#include <queue>
#include <stack>

#include <unordered_set>
#include <unordered_map>
#include <array>
#include <iomanip>

#include <atcoder/all>

using namespace std;

#define LL long long

inline int read() {
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') {
            f = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

inline void write(int x) {
	if (x < 0) {
		putchar('-'); x = -x;
	}
    static int sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48); 
}

inline void writeln(int x) {
	if (x < 0) {
		putchar('-'); x = -x;
	}
    static int sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48); 
	putchar('\n');
}

inline LL readll() {
    LL x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') {
            f = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

inline void writell(LL x) {
	if (x < 0) {
		putchar('-'); x = -x;
	}
    static LL sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48); 
}

inline void writellln(LL x) {
	if (x < 0) {
		putchar('-'); x = -x;
	}
    static LL sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48); 
	putchar('\n');
}
template<class DataType1, class DataType2>
void write(pair<DataType1, DataType2> a, char endSplit = ' ') {
    cout << "(" << a.first << ", " << a.second << ")" << endSplit;
}
template<class DataType>
void write(vector<DataType> &a, char split = ' ', char endSplit = '\n') {
    for (int i = 0; i < a.size(); ++i) {
        cout << a[i] << split;
    }
    cout << endSplit;
}

using mint = atcoder::modint998244353;

vector<int> f;

int gf(int u) {
    return f[u] == u ? u : f[u] = gf(f[u]);
}
void combine(int u, int v) {
    f[gf(u)] = gf(v);
}

class SubGraph {
public:
    int k;
    LL c;
    vector<int> a;

    void input() {
        k = read();
        c = readll();
        a = vector<int>(k);
        for (int i = 0; i < k; ++i) {
            a[i] = read() - 1;
        }
    
    }
};

bool cmp(const SubGraph &a, const SubGraph &b) {
    return a.c < b.c;
}

int main() {
	// std::ios::sync_with_stdio(false);
    // std::cin.tie(0);
    int n = read(), m = read();
    f = vector<int>(n);
    for (int i = 0; i < n; ++i) {
        f[i] = i;
    }

    vector<SubGraph> g(m);
    for (int i = 0; i < m; ++i) {
        g[i].input();
    }

    sort(g.begin(), g.end(), cmp);
    LL ans = 0;
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
        for (int j = 1; j < g[i].k; ++j) {
            if (gf(g[i].a[j]) != gf(g[i].a[0])) {
                combine(g[i].a[j], g[i].a[0]);
                ans += g[i].c;
                ++cnt;
            }
        }
    }

    if (cnt == n - 1) {
        writellln(ans);
    } else {
        writellln(-1);
    }

	return 0;
}

提出情報

提出日時
問題 E - Clique Connect
ユーザ AliceNan
言語 C++ 20 (gcc 12.2)
得点 450
コード長 3663 Byte
結果 AC
実行時間 61 ms
メモリ 17932 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 450 / 450
結果
AC × 3
AC × 52
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 02_random2_20.txt, 02_random2_21.txt, 02_random2_22.txt, 02_random2_23.txt, 02_random2_24.txt, 02_random2_25.txt, 02_random2_26.txt, 02_random2_27.txt, 02_random2_28.txt, 02_random2_29.txt, 02_random2_30.txt, 02_random2_31.txt, 02_random2_32.txt, 02_random2_33.txt, 02_random2_34.txt, 02_random2_35.txt, 02_random2_36.txt, 02_random2_37.txt, 02_random2_38.txt, 02_random2_39.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3480 KiB
00_sample_01.txt AC 1 ms 3580 KiB
00_sample_02.txt AC 1 ms 3516 KiB
01_random_00.txt AC 26 ms 7760 KiB
01_random_01.txt AC 33 ms 9652 KiB
01_random_02.txt AC 43 ms 12388 KiB
01_random_03.txt AC 54 ms 15236 KiB
01_random_04.txt AC 59 ms 17912 KiB
02_random2_00.txt AC 18 ms 6752 KiB
02_random2_01.txt AC 18 ms 6964 KiB
02_random2_02.txt AC 19 ms 6872 KiB
02_random2_03.txt AC 20 ms 6872 KiB
02_random2_04.txt AC 21 ms 6748 KiB
02_random2_05.txt AC 21 ms 8164 KiB
02_random2_06.txt AC 24 ms 8160 KiB
02_random2_07.txt AC 25 ms 8076 KiB
02_random2_08.txt AC 26 ms 8088 KiB
02_random2_09.txt AC 27 ms 8196 KiB
02_random2_10.txt AC 25 ms 9448 KiB
02_random2_11.txt AC 28 ms 9504 KiB
02_random2_12.txt AC 31 ms 9460 KiB
02_random2_13.txt AC 34 ms 9448 KiB
02_random2_14.txt AC 35 ms 9520 KiB
02_random2_15.txt AC 30 ms 11016 KiB
02_random2_16.txt AC 34 ms 11016 KiB
02_random2_17.txt AC 38 ms 10980 KiB
02_random2_18.txt AC 42 ms 11068 KiB
02_random2_19.txt AC 41 ms 10976 KiB
02_random2_20.txt AC 32 ms 12632 KiB
02_random2_21.txt AC 37 ms 12612 KiB
02_random2_22.txt AC 45 ms 12580 KiB
02_random2_23.txt AC 47 ms 12544 KiB
02_random2_24.txt AC 50 ms 12648 KiB
02_random2_25.txt AC 38 ms 14408 KiB
02_random2_26.txt AC 47 ms 14532 KiB
02_random2_27.txt AC 50 ms 14492 KiB
02_random2_28.txt AC 54 ms 14436 KiB
02_random2_29.txt AC 55 ms 14440 KiB
02_random2_30.txt AC 37 ms 16404 KiB
02_random2_31.txt AC 43 ms 16324 KiB
02_random2_32.txt AC 53 ms 16292 KiB
02_random2_33.txt AC 57 ms 16304 KiB
02_random2_34.txt AC 58 ms 16472 KiB
02_random2_35.txt AC 38 ms 17916 KiB
02_random2_36.txt AC 44 ms 17932 KiB
02_random2_37.txt AC 55 ms 17856 KiB
02_random2_38.txt AC 60 ms 17892 KiB
02_random2_39.txt AC 61 ms 17908 KiB
03_handmade_00.txt AC 1 ms 3636 KiB
03_handmade_01.txt AC 5 ms 4716 KiB
03_handmade_02.txt AC 5 ms 4648 KiB
03_handmade_03.txt AC 56 ms 17916 KiB