Submission #53114915


Source Code Expand

// #pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf (ll)1e18
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define fi first
#define se second
#define lll __int128

const int N = 1e6 + 5;
ll fa[N];
ll find(ll x){
	if(fa[x] == x) return x;
	return fa[x] = find(fa[x]);
}
void merge(ll x, ll y){
	fa[find(x)] = find(y);
}
void solve() {
	ll n, m, k, c;
	cin >> n >> m;
	for (ll i = 1; i <= n; i++)
		fa[i] = i;
	vector<pair<ll, vl>> es;
	while (m--) {
		cin >> k >> c;
		vl t(k);
		for (ll i = 0; i < k; i++)
			cin >> t[i];
		es.push_back({c, t});
	}
	sort(es.begin(), es.end());
	ll ans = 0;
	for (auto [c, t] : es) {
		for (ll i = 1; i < t.size(); i++) {
			ll r1 = find(t[i - 1]), r2 = find(t[i]);
			if (r1 != r2) {
				// cout << t[i - 1] << " " << t[i] << endl;
				merge(t[i - 1], t[i]);
				ans += c;
			}
		}
	}
	set<ll> rs;
	for (ll i = 1; i <= n; i++) {
		rs.insert(find(i));
	}
	if (rs.size() != 1)
		cout << -1 << endl;
	else
		cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	cout.tie(0);
	solve();
	return 0;
}

Submission Info

Submission Time
Task E - Clique Connect
User TopCloser
Language C++ 20 (gcc 12.2)
Score 450
Code Size 1221 Byte
Status AC
Exec Time 93 ms
Memory 18704 KiB

Compile Error

Main.cpp: In function ‘void solve()’:
Main.cpp:38:34: warning: comparison of integer expressions of different signedness: ‘long long int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   38 |                 for (ll i = 1; i < t.size(); i++) {
      |                                ~~^~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 52
Set Name Test Cases
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
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3404 KiB
00_sample_01.txt AC 1 ms 3344 KiB
00_sample_02.txt AC 1 ms 3492 KiB
01_random_00.txt AC 51 ms 10908 KiB
01_random_01.txt AC 64 ms 12504 KiB
01_random_02.txt AC 74 ms 14180 KiB
01_random_03.txt AC 85 ms 17224 KiB
01_random_04.txt AC 93 ms 18704 KiB
02_random2_00.txt AC 38 ms 8880 KiB
02_random2_01.txt AC 39 ms 8840 KiB
02_random2_02.txt AC 39 ms 8836 KiB
02_random2_03.txt AC 38 ms 8828 KiB
02_random2_04.txt AC 38 ms 8780 KiB
02_random2_05.txt AC 48 ms 9844 KiB
02_random2_06.txt AC 47 ms 9724 KiB
02_random2_07.txt AC 47 ms 9688 KiB
02_random2_08.txt AC 45 ms 9836 KiB
02_random2_09.txt AC 46 ms 9840 KiB
02_random2_10.txt AC 54 ms 12400 KiB
02_random2_11.txt AC 55 ms 12316 KiB
02_random2_12.txt AC 58 ms 12328 KiB
02_random2_13.txt AC 54 ms 12252 KiB
02_random2_14.txt AC 53 ms 12284 KiB
02_random2_15.txt AC 62 ms 12128 KiB
02_random2_16.txt AC 64 ms 11972 KiB
02_random2_17.txt AC 63 ms 11976 KiB
02_random2_18.txt AC 61 ms 11956 KiB
02_random2_19.txt AC 60 ms 12040 KiB
02_random2_20.txt AC 69 ms 13164 KiB
02_random2_21.txt AC 70 ms 13232 KiB
02_random2_22.txt AC 69 ms 13204 KiB
02_random2_23.txt AC 67 ms 13168 KiB
02_random2_24.txt AC 67 ms 13256 KiB
02_random2_25.txt AC 78 ms 17160 KiB
02_random2_26.txt AC 78 ms 17140 KiB
02_random2_27.txt AC 80 ms 17160 KiB
02_random2_28.txt AC 75 ms 17156 KiB
02_random2_29.txt AC 75 ms 17076 KiB
02_random2_30.txt AC 83 ms 17048 KiB
02_random2_31.txt AC 85 ms 17080 KiB
02_random2_32.txt AC 85 ms 17028 KiB
02_random2_33.txt AC 81 ms 17140 KiB
02_random2_34.txt AC 80 ms 17032 KiB
02_random2_35.txt AC 89 ms 17140 KiB
02_random2_36.txt AC 90 ms 17128 KiB
02_random2_37.txt AC 90 ms 17204 KiB
02_random2_38.txt AC 88 ms 17060 KiB
02_random2_39.txt AC 86 ms 17220 KiB
03_handmade_00.txt AC 1 ms 3404 KiB
03_handmade_01.txt AC 13 ms 10240 KiB
03_handmade_02.txt AC 13 ms 10344 KiB
03_handmade_03.txt AC 80 ms 17304 KiB