| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 
 | #include<bits/stdc++.h>#define ll long long
 #define ms(a,b) memset(a,b,sizeof(a))
 const int maxn=1e6+10;
 const int maxm=1e3+10;
 const int inf=(1<<30);
 const ll INF=(1LL*1<<60);
 using namespace std;
 int f[maxn];
 struct Edge
 {
 int u,v,w;
 }edge[maxn];
 int tol;
 void add(int u,int v,int w)
 {
 edge[tol].u=u;
 edge[tol].v=v;
 edge[tol++].w=w;
 }
 bool cmp(Edge a,Edge b)
 {
 return a.w<b.w;
 }
 int Find(int x)
 {
 if(f[x]==x)
 return x;
 else
 return f[x]=Find(f[x]);
 }
 ll kru(int n)
 {
 for(int i=0;i<=n;i++)
 f[i]=i;
 sort(edge,edge+tol,cmp);
 int cnt=0;
 ll ans=0;
 for(int i=0;i<tol;i++)
 {
 int u=edge[i].u;
 int v=edge[i].v;
 int w=edge[i].w;
 int tOne=Find(u);
 int tTwo=Find(v);
 if(tOne!=tTwo)
 {
 ans+=1LL*w;
 f[tOne]=tTwo;
 cnt++;
 }
 }
 if(cnt<n-1)
 return -1;
 else
 return ans;
 }
 int main()
 {
 ios::sync_with_stdio(false);cin.tie(0);
 int n,m,s;
 cin>>n>>m>>s;
 int res=0;
 for(int i=0;i<m;i++)
 {
 int x,y,d;
 cin>>x>>y>>d;
 res=max(max(x,y),res);
 add(x,y,0);
 add(y,x,0);
 }
 int a1=kru(m);
 for(int i=0;i<s;i++)
 {
 int x,y,d;
 cin>>x>>y>>d;
 add(x,y,d);
 add(y,x,d);
 }
 if(kru(n)==-1)
 cout<<"Concubines can't do it."<<endl;
 else
 cout<<1LL*kru(n)<<endl;
 return 0;
 }
 
 |