Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<queue>
- using namespace std;
- typedef long long int ll;
- struct edge{
- int u,v;
- ll dif;
- bool operator < (const edge& rhs)const
- {
- return dif > rhs.dif;
- }
- };
- int set[100010];
- int find(int x){
- if(set[x] == x)return x;
- return set[x] = find(set[x]);
- }
- void unions(int x,int y){
- int rx = find(x);
- int ry = find(y);
- set[rx] = ry;
- }
- bool isset(int x,int y){
- return find(x) == find(y);
- }
- int main()
- {
- for(int i=0;i<=100000;i++)set[i] = i;
- int n,m;
- scanf("%d",&n);
- int str[n+1];
- for(int i=1;i<=n;i++)scanf("%lld",&str[i]);
- scanf("%d",&m);
- priority_queue<edge> pq;
- for(int i=1;i<=m;i++){
- int u,v;
- scanf("%d %d",&u,&v);
- pq.push({u,v,str[u] + str[v]});
- }
- ll sum = 0;
- // printf("%d\n",pq.size());
- while(!pq.empty()){
- int u = pq.top().u;
- int v = pq.top().v;
- ll t = pq.top().dif;
- pq.pop();
- // printf("Test %d %d %lld\n",u,v,t);
- if(isset(u,v))continue;
- sum += t;
- unions(u,v);
- }
- printf("%lld",sum);
- }
Advertisement
Add Comment
Please, Sign In to add comment