Language: C++


//#include "pch.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <vector>
#include <set>
//#include <map>
//#include <iostream>

#define ll long long
#define fri(n) for(i=0;i<(n);i++)
#define frj(n) for(j=0;j<(n);i++)
#define min(p,q) ((p)<(q)?(p):(q))
#define max(p,q) ((p)>(q)?(p):(q))
#define swap(p,q) r=(p);(p)=(q);(q)=r
#define INF 1000000000000//10^12

using namespace std;


static int come[100010];
int solonum=0;
int cnt=0;
static vector<int> graph[100010];
int solo[100010];
static vector<int> subtree[100010];


int search(int x){
	int i;

	if(come[x]==1) return 0;
	
	come[x]=1;

	//中身がなくてもsizeちゃんと定義される？？
	if(graph[x].size()==0){
		solo[solonum]=x;
		solonum++;
		return 1;
	}

	subtree[cnt].push_back(x);

	for(i=0;i<graph[x].size();i++){
		search(graph[x][i]);
	}

	return 0;
}


struct cost_t {
	int idnum;
	int cost;
} ;

int compare_cost(const void *a, const void *b) {
	struct cost_t *A = (struct cost_t *)a;
	struct cost_t *B = (struct cost_t *)b;
	return A->cost - B->cost;
}

struct subtree_min_t {
	int idnum;
	int cost;
} ;

int compare_min(const void *a, const void *b) {
	struct subtree_min_t *A = (struct subtree_min_t *)a;
	struct subtree_min_t *B = (struct subtree_min_t *)b;
	return A->cost - B->cost;
}

int main(void)
{
	//変数の宣言
	int n,m;
	int x,y;
//	vector<int> graph[100010];
//	int solo[100010];
	multiset<int> maintree;
 	static int use[100010];
	static struct cost_t ct[100010];
	static int inv[100010];
//	vector<int> subtree[100010];
//	static int come[100010];
	static struct subtree_min_t subtree_min[100010];
	static struct cost_t memo[100010]; 

	//よく使う変数
	int i,j,k,l;
	int flag=0;
	ll int ans=0;
//	int cnt=0;
	int temp,temp1,temp2;
	int max,min;
	int len;
	int sum=0;



	//データの読み込み


	scanf("%d %d",&n,&m);
//	scanf_s("%d %d",&n,&m);

	for(i=0;i<n;i++){
		scanf("%d",&ct[i].cost);
//		scanf_s("%d",&ct[i].cost);
		ct[i].idnum=i;
	}

	qsort(ct, n, sizeof(struct cost_t), compare_cost);

	for(i=0;i<n;i++){
		inv[ct[i].idnum]=i;
	}

	for(i=0;i<m;i++){
		scanf("%d %d",&x,&y);
//		scanf_s("%d %d",&x,&y);
		graph[inv[x]].push_back(inv[y]);
		graph[inv[y]].push_back(inv[x]);
	}

	for(i=0;i<n+1;i++){
		come[i]=0;
		subtree_min[i].cost=0;
	}

	//	printf("nは%dです\n", n);
	//	printf("データの読み込み終了\n");
	//実際の処理
	
	i=0;
	while(i<n){
		search(i);
		if(graph[i].size()!=0) cnt++;

		while(1==come[i]){
			i++;
		}
	}

	if(cnt==0){
		if(solonum==1){
			printf("0");
		}else if(solonum==2){
			printf("%d",ct[0].cost+ct[1].cost);
		}else{
			printf("Impossible");
		}
		return 0;

	}else{
		for(i=0;i<subtree[0].size();i++){
			maintree.insert(subtree[0][i]);
		}

		for(i=1;i<cnt;i++){
			for(j=0;j<subtree[i].size();j++){
				memo[j].idnum=subtree[i][j];
				memo[j].cost=ct[subtree[i][j]].cost;
			}

			qsort(memo, subtree[i].size(), sizeof(struct cost_t), compare_cost);
			subtree_min[i-1].cost=memo[0].cost;
			subtree_min[i-1].idnum=i;
		}

		qsort(subtree_min,cnt-1,sizeof(int),compare_min);

		set<int>::iterator itr=maintree.begin();
		for(i=0;i<cnt-1;i++){
			temp=0;
//			for(itr=maintree.begin();itr!=maintree.end();itr++){
//				printf("maintree[%d]=%d\n",temp,*itr);
//				temp++;
			}

			itr=maintree.begin();

			ans=ans+ct[*itr].cost+subtree_min[i].cost;

			maintree.erase(itr);

			for(j=1;j<subtree[subtree_min[i].idnum].size();j++){
				maintree.insert(subtree[subtree_min[i].idnum][j]);
			}
		}

		itr=maintree.begin();
		for(i=0;i<solonum;i++){
			if(itr==maintree.end()){
				printf("Impossible");
				return 0;
			}

			ans=ans+ct[*itr].cost+solo[i];
			++itr;
		}
	}

	//	printf("計算部分終了\n");

	//出力

	printf("%lld",ans);

	//	printf("結果の出力終了\n");


	return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:197:17: error: 'itr' was not declared in this scope
  197 |                 itr=maintree.begin();
      |                 ^~~
a.cc: At global scope:
a.cc:213:15: error: expected constructor, destructor, or type conversion before '(' token
  213 |         printf("%lld",ans);
      |               ^
a.cc:218:9: error: expected unqualified-id before 'return'
  218 |         return 0;
      |         ^~~~~~
a.cc:219:1: error: expected declaration before '}' token
  219 | }
      | ^