Submission #1857215
Source Code Expand
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<bitset>
#include<iomanip>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
//ios::sync_with_stdio(false);
//std::cin.tie(0);
//<< setprecision(20)
const int mod=1e9+7;
const llint big=1e15+100;
const long double pai=3.141592653589793238462643383279502884197;
const long double ena=2.71828182845904523536;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
template <class T> void soun(T& ar)
{sort(ar.begin(),ar.end());ar.erase(unique(ar.begin(),ar.end()),ar.end());}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
int main(void){
int n,m,K,i,j;cin>>n>>m>>K;
vector<int>col(1+K,-1);//その色を持つ最小値
vector<int>tai(n);
for(i=0;i<n;i++){tai[i]=i;}
int nok=K-1;
for(i=0;i<n;i++){
int c;cin>>c;if(c==0){continue;}
if(col[c]==-1){col[c]=i;}
else{tai[i]=col[c];}
}
vector<tuple<llint,int,int>>hen(m);
for(i=0;i<m;i++){
int a,b;llint w;cin>>a>>b>>w;a--;b--;
hen[i]=mt(w,tai[a],tai[b]);
}
vector<int>oya(n);
vector<int>ran(n);
for(i=0;i<n;i++){oya[i]=i;}
sort(hen.begin(),hen.end());
int mita=0;llint ans=0;
while(nok>0){
if(mita==hen.size()){cout<<-1<<endl;return 0;}
int a,b;llint w;tie(w,a,b)=hen[mita];
mita++;
while(a!=oya[a]){a=oya[a];}
while(b!=oya[b]){b=oya[b];}
if(a==b){continue;}
nok--;ans+=w;
if(ran[a]<ran[b]){swap(a,b);}
if(ran[a]==ran[b]){ran[a]++;}
oya[b]=a;
}
cout<<ans<<endl;
}
Submission Info
Submission Time |
|
Task |
A - Colorful MST |
User |
WA_TLE |
Language |
C++14 (GCC 5.4.1) |
Score |
700 |
Code Size |
2168 Byte |
Status |
AC |
Exec Time |
128 ms |
Memory |
3328 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 |
124 ms |
3328 KB |
s1_03.txt |
AC |
1 ms |
256 KB |
s1_04.txt |
AC |
1 ms |
256 KB |
s1_05.txt |
AC |
66 ms |
2432 KB |
s1_06.txt |
AC |
124 ms |
3328 KB |
s2_07.txt |
AC |
17 ms |
640 KB |
s2_08.txt |
AC |
128 ms |
3328 KB |
s2_09.txt |
AC |
95 ms |
2176 KB |
s2_10.txt |
AC |
88 ms |
2048 KB |
s2_11.txt |
AC |
120 ms |
3072 KB |
s2_12.txt |
AC |
120 ms |
3072 KB |
s3_13.txt |
AC |
15 ms |
640 KB |
s3_14.txt |
AC |
113 ms |
3328 KB |
s3_15.txt |
AC |
90 ms |
2304 KB |
s3_16.txt |
AC |
85 ms |
2176 KB |
s3_17.txt |
AC |
111 ms |
3328 KB |
s3_18.txt |
AC |
111 ms |
3072 KB |
s4_19.txt |
AC |
17 ms |
640 KB |
s4_20.txt |
AC |
118 ms |
3072 KB |
s4_21.txt |
AC |
94 ms |
2304 KB |
s4_22.txt |
AC |
89 ms |
2176 KB |
s4_23.txt |
AC |
122 ms |
3328 KB |
s4_24.txt |
AC |
118 ms |
3072 KB |