Submission #3313634
Source Code Expand
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <string> #include <sstream> #include <complex> #include <vector> #include <list> #include <queue> #include <deque> #include <stack> #include <map> #include <set> using namespace std; #define mod 1000000007 #define FOR(x,to) for(int x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) #define long long long inline int rei(){int x;cin>>x;return x;} inline long rel(){long x;cin>>x;return x;} inline string res(){string x;cin>>x;return x;} //------------------------------------------------------- struct UnionFind{ vector<int> par; UnionFind(int N){ par.resize(N); for(int i=0;i<N;i++){ par[i] = i; } } void Union(int x,int y){ par[Get(x)] = Get(y); } bool Same(int x,int y){ return Get(x) == Get(y); } int Get(int x){ if(x != par[x]){ par[x] = Get(par[x]); } return par[x]; } }; pair<long,pair<int,int>> Edge[100000]; void Calc(){ int N = rei(); int M = rei(); int K = rei(); UnionFind U(N+K); for(int i=0;i<N;i++){ int c = rei()-1; if(c != -1){ U.Union(i,c+N); } } for(int i=0;i<M;i++){ int f = rei()-1; int t = rei()-1; long c = rel(); Edge[i] = {c,{f,t}}; } sort(Edge,Edge+M); long ans = 0; K--; for(int i=0;i<M;i++){ if(!U.Same(Edge[i].second.first,Edge[i].second.second)){ ans += Edge[i].first; U.Union(Edge[i].second.first,Edge[i].second.second); K--; if(K == 0){ cout << ans << endl; return; } } } cout << -1 << endl; } int main(int argc,char** argv){ ios::sync_with_stdio(false), cin.tie(0); cout.tie(0); Calc(); return 0; }
Submission Info
Submission Time | |
---|---|
Task | A - Colorful MST |
User | leign |
Language | C++14 (GCC 5.4.1) |
Score | 700 |
Code Size | 1930 Byte |
Status | AC |
Exec Time | 58 ms |
Memory | 2560 KB |
Judge Result
Set Name | Sample | Subtask1 | Subtask2 | Subtask3 | Subtask4 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 100 / 100 | 100 / 100 | 200 / 200 | 300 / 300 | ||||||||||
Status |
|
|
|
|
|
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 | 256 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 | 47 ms | 2560 KB |
s1_03.txt | AC | 1 ms | 256 KB |
s1_04.txt | AC | 1 ms | 256 KB |
s1_05.txt | AC | 23 ms | 1664 KB |
s1_06.txt | AC | 58 ms | 2560 KB |
s2_07.txt | AC | 7 ms | 512 KB |
s2_08.txt | AC | 48 ms | 2560 KB |
s2_09.txt | AC | 33 ms | 1792 KB |
s2_10.txt | AC | 31 ms | 1792 KB |
s2_11.txt | AC | 43 ms | 2304 KB |
s2_12.txt | AC | 42 ms | 2176 KB |
s3_13.txt | AC | 6 ms | 512 KB |
s3_14.txt | AC | 43 ms | 2560 KB |
s3_15.txt | AC | 32 ms | 1920 KB |
s3_16.txt | AC | 32 ms | 1920 KB |
s3_17.txt | AC | 43 ms | 2560 KB |
s3_18.txt | AC | 40 ms | 2304 KB |
s4_19.txt | AC | 7 ms | 512 KB |
s4_20.txt | AC | 42 ms | 2304 KB |
s4_21.txt | AC | 34 ms | 1920 KB |
s4_22.txt | AC | 35 ms | 1920 KB |
s4_23.txt | AC | 49 ms | 2560 KB |
s4_24.txt | AC | 43 ms | 2304 KB |