Language: C++

// #include <iostream>
// #include <vector>
// #include <algorithm>
// #include <queue>
// #include <cstring>




// using namespace std;




// typedef pair<int,int> pii;




// const int MAX_V=10001;
// const int INF=1000000000;




// struct edge{
// &#160; int to;
// &#160; int cost;
// };




// vector<edge> G[MAX_V];
// bool used[MAX_V];
// int prim(int V){
// &#160; memset(used,0,sizeof(used));
// &#160; int res=0;
// &#160; priority_queue<pii,vector<pii>,greater<pii> > pq;
// &#160; // 次の場所をコストとともに登録
// &#160; for(int i=0;i<G[0].size();i++)
// &#160; &#160; pq.push(pii(G[0][i].cost,G[0][i].to));
// &#160; used[0]=true;
// &#160; while(pq.size()){
// &#160; &#160; pii p=pq.top();pq.pop();
// &#160; &#160; int cost=p.first;
// &#160; &#160; int node=p.second;
// &#160; &#160; // すでに登録済みノード
// &#160; &#160; if(used[node])continue;
// &#160; &#160; // 今回のエッジを採用
// &#160; &#160; used[node]=true;
// &#160; &#160; res+=cost;
// &#160; &#160; // 今回登録したノードからいける場所を登録
// &#160; &#160; for(int i=0;i<G[node].size();i++){
// &#160; &#160; &#160; edge &e=G[node][i];
// &#160; &#160; &#160; if(used[e.to])continue;
// &#160; &#160; &#160; pq.push(pii(G[node][i].cost,G[node][i].to));
// &#160; &#160; }
// &#160; }
// &#160; return res;
// }




#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>




using namespace std;




typedef pair<int,int> pii;




const int MAX_V=10001;
const int INF=1000000000;








class UnionFindTree{
public:
&#160; // indexの数の親ノード(ある集合の親を見つけたいときはここを直接参照せず、findを使う)
&#160; vector<int> par;
&#160; // indexを根とする木のランク
&#160; vector<int> rank;
&#160; // 木の最大値
&#160; int treeSize;
&#160; vector<bool> isBase;
&#160; UnionFindTree(int initTreeSize = 1000){
&#160; &#160; // 引数で与えられた値を最大格納数とするUnionFindTreeの作成
&#160; &#160; treeSize = initTreeSize;
&#160; &#160; init();
&#160; }
&#160; void init(){
&#160; &#160; for(int i = 0; i < treeSize; i++){
&#160; &#160; &#160; par.push_back(i);
&#160; &#160; &#160; rank.push_back(0);
&#160; &#160; &#160; isBase.push_back(false);
&#160; &#160; }
&#160; }
&#160; // 与えられた数が格納されている木のルートを探索
&#160; int find(int x){ &#160;
&#160; &#160; if(par[x]==x)return x;
&#160; &#160; else return par[x] = find(par[x]);
&#160; }
&#160; // 和集合をとる。ただしここでは各集合の親の付け替えは起こらない
&#160; void unite(int x,int y){
&#160; &#160; x = find(x);
&#160; &#160; y = find(y);
&#160; &#160; if(x == y)
&#160; &#160; &#160; return;
&#160; &#160; if(rank[x] < rank[y]){
&#160; &#160; &#160; par[x] = y;
&#160; &#160; &#160; if(isBase[find(x)]||isBase[find(y)])
	isBase[find(x)]=true;
&#160; &#160; }
&#160; &#160; else{
&#160; &#160; &#160; par[y] = x;
&#160; &#160; &#160; if(isBase[find(x)]||isBase[find(y)])
	isBase[find(x)]=true;
&#160; &#160; &#160; if(rank[y] == rank[x])rank[x]++;
&#160; &#160; }
&#160; }
&#160; bool same(int x,int y){
&#160; &#160; return find(x) == find(y);
&#160; }
};




struct edge{
&#160; int to;
&#160; int cost;
&#160; int idx;
};




vector<edge> G[MAX_V];
vector<int> bases;
bool used[MAX_V];
int n,t,k;
void prim(int V){
&#160; priority_queue<int> cand;
&#160; UnionFindTree uft(V+1);
&#160; for(int i=0;i<MAX_V;i++)used[i]=false;
&#160; for(int i=0;i<bases.size();i++)uft.isBase[bases[i]]=true;
&#160; priority_queue<pair<pii,pii> > pq;
&#160; for(int i=0;i<bases.size();i++){
&#160; &#160; // 次の場所をコストとともに登録
&#160; &#160; for(int j=0;j<G[bases[i]].size();j++){
&#160; &#160; &#160; edge &e=G[bases[i]][j];
&#160; &#160; &#160; if(used[e.idx])continue;
&#160; &#160; &#160; used[e.idx]=true;
&#160; &#160; &#160; pq.push(make_pair(pii(e.cost,e.idx),pii(e.to,bases[i])));
&#160; &#160; }
&#160; }
&#160; while(pq.size()){
&#160; &#160; pair<pii,pii> p=pq.top();pq.pop();
&#160; &#160; int cost=p.first.first;
&#160; &#160; int idx=p.first.second;
&#160; &#160; int node=p.second.first;
&#160; &#160; int from=p.second.second;
&#160; &#160; // 今回のエッジを採用
&#160; &#160; if(uft.isBase[uft.find(from)]
&#160; &#160; &#160; &#160;&&uft.isBase[uft.find(node)])cand.push(-cost);
&#160; &#160; uft.unite(from,node);
&#160; &#160; // 今回登録したノードからいける場所を登録
&#160; &#160; for(int i=0;i<G[node].size();i++){
&#160; &#160; &#160; edge &e=G[node][i];
&#160; &#160; &#160; if(used[e.idx])continue;
&#160; &#160; &#160; used[e.idx]=true;
&#160; &#160; &#160; pq.push(make_pair(pii(e.cost,e.idx),pii(e.to,node)));
&#160; &#160; }
&#160; }
&#160; int res=0;
&#160; for(int i=0;i<k;i++){
&#160; &#160; res+=-cand.top();cand.pop();
&#160; }
&#160; cout<<res<<endl;
&#160; cout<<flush;
}




int main(){
&#160; int cnt=1;
&#160; while(cin>>n>>t>>k&&(n|t|k)){
&#160; &#160; for(int i=0;i<MAX_V;i++)G[i].clear();
&#160; &#160; for(int i=0;i<n-1;i++){
&#160; &#160; &#160; edge e;
&#160; &#160; &#160; int a,b,c;
&#160; &#160; &#160; cin>>a>>b>>c;
&#160; &#160; &#160; e.to=a;
&#160; &#160; &#160; e.cost=c;
&#160; &#160; &#160; e.idx=i;
&#160; &#160; &#160; G[b].push_back(e);
&#160; &#160; &#160; e.to=b;
&#160; &#160; &#160; G[a].push_back(e);
&#160; &#160; }
&#160; &#160; bases.clear();
&#160; &#160; for(int i=0;i<t;i++){
&#160; &#160; &#160; int a;
&#160; &#160; &#160; cin>>a;
&#160; &#160; &#160; bases.push_back(a);
&#160; &#160; }
&#160; &#160; cout<<"Case "<<cnt<<": ";
&#160; &#160; prim(n);
&#160; &#160; cnt++;
&#160; }




&#160; return 0;
}

Compiler output:
a.cc:97:2: error: stray '#' in program
   97 | &#160; // indexの数の親ノード(ある集合の親を見つけたいときはここを直接参照せず、findを使う)
      |  ^
a.cc:98:2: error: stray '#' in program
   98 | &#160; vector<int> par;
      |  ^
a.cc:99:2: error: stray '#' in program
   99 | &#160; // indexを根とする木のランク
      |  ^
a.cc:100:2: error: stray '#' in program
  100 | &#160; vector<int> rank;
      |  ^
a.cc:101:2: error: stray '#' in program
  101 | &#160; // 木の最大値
      |  ^
a.cc:102:2: error: stray '#' in program
  102 | &#160; int treeSize;
      |  ^
a.cc:103:2: error: stray '#' in program
  103 | &#160; vector<bool> isBase;
      |  ^
a.cc:104:2: error: stray '#' in program
  104 | &#160; UnionFindTree(int initTreeSize = 1000){
      |  ^
a.cc:105:2: error: stray '#' in program
  105 | &#160; &#160; // 引数で与えられた値を最大格納数とするUnionFindTreeの作成
      |  ^
a.cc:105:9: error: stray '#' in program
  105 | &#160; &#160; // 引数で与えられた値を最大格納数とするUnionFindTreeの作成
      |         ^
a.cc:106:2: error: stray '#' in program
  106 | &#160; &#160; treeSize = initTreeSize;
      |  ^
a.cc:106:9: error: stray '#' in program
  106 | &#160; &#160; treeSize = initTreeSize;
      |         ^
a.cc:107:2: error: stray '#' in program
  107 | &#160; &#160; init();
      |  ^
a.cc:107:9: error: stray '#' in program
  107 | &#160; &#160; init();
      |         ^
a.cc:108:2: error: stray '#' in program
  108 | &#160; }
      |  ^
a.cc:109:2: error: stray '#' in program
  109 | &#160; void init(){
      |  ^
a.cc:110:2: error: stray '#' in program
  110 | &#160; &#160; for(int i = 0; i < treeSize; i++){
      |  ^
a.cc:110:9: error: stray '#' in program
  110 | &#160; &#160; for(int i = 0; i < treeSize; i++){
      |         ^
a.cc:111:2: error: stray '#' in program
  111 | &#160; &#160; &#160; par.push_back(i);
      |  ^
a.cc:111:9: error: stray '#' in program
  111 | &#160; &#160; &#160; par.push_back(i);
      |         ^
a.cc:111:16: error: stray '#' in program
  111 | &#160; &#160; &#160; par.push_back(i);
      |                ^
a.cc:112:2: error: stray '#' in program
  112 | &#160; &#160; &#160; rank.push_back(0);
      |  ^
a.cc:112:9: error: stray '#' in program
  112 | &#160; &#160; &#160; rank.push_back(0);
      |         ^
a.cc:112:16: error: stray '#' in program
  112 | &#160; &#160; &#160; rank.push_back(0);
      |                ^
a.cc:113:2: error: stray '#' in program
  113 | &#160; &#160; &#160; isBase.push_back(false);
      |  ^
a.cc:113:9: error: stray '#' in program
  113 | &#160; &#160; &#160; isBase.push_back(false);
      |         ^
a.cc:113:16: error: stray '#' in program
  113 | &#160; &#160; &#160; isBase.push_back(false);
      |                ^
a.cc:114:2: error: stray '#' in program
  114 | &#160; &#160; }
      |  ^
a.cc:114:9: error: stray '#' in program
  114 | &#160; &#160; }
      |         ^
a.cc:115:2: error: stray '#' in program
  115 | &#160; }
      |  ^
a.cc:116:2: error: stray '#' in program
  116 | &#160; // 与えられた数が格納されている木のルートを探索
      |  ^
a.cc:117:2: error: stray '#' in program
  117 | &#160; int find(int x){ &#160;
      |  ^
a.cc:117:26: error: stray '#' in program
  117 | &#160; int find(int x){ &#160;
      |                          ^
a.cc:118:2: error: stray '#' in program
  118 | &#160; &#160; if(par[x]==x)return x;
      |  ^
a.cc:118:9: error: stray '#' in program
  118 | &#160; &#160; if(par[x]==x)return x;
      |         ^
a.cc:119:2: error: stray '#' in program
  119 | &#160; &#160; else return par[x] = find(par[x]);
      |  ^
a.cc:119:9: error: stray '#' in program
  119 | &#160; &#160; else return par[x] = find(par[x]);
      |         ^
a.cc:120:2: error: stray '#' in program
  120 | &#160; }
      |  ^
a.cc:121:2: error: stray '#' in program
  121 | &#160; // 和集合をとる。ただしここでは各集合の親の付け替えは起こらない
      |  ^
a.cc:122:2: error: stray '#' in program
  122 | &#160; void unite(int x,int y){
      |  ^
a.cc:123:2: error: stray '#' in program
  123 | &#160; &#160; x = find(x);
      |  ^
a.cc:123:9: error: stray '#' in program
  123 | &#160; &#160; x = find(x);
      |         ^
a.cc:124:2: error: stray '#' in program
  124 | &#160; &#160; y = find(y);
      |  ^
a.cc:124:9: error: stray '#' in program
  124 | &#160; &#160; y = find(y);
      |         ^
a.cc:125:2: error: stray '#' in program
  125 | &#160; &#160; if(x == y)
      |  ^
a.cc:125:9: error: stray '#' in program
  125 | &#160; &#160; if(x == y)
      |         ^
a.cc:126:2: error: stray '#' in program
  126 | &#160; &#160; &#160; return;
      |  ^
a.cc:126:9: error: stray '#' in program
  126 | &#160; &#160; &#160; return;
      |         ^
a.cc:126:16: error: stray '#' in program
  126 | &#160; &#160; &#160; return;
      |                ^
a.cc:127:2: error: stray '#' in program
  127 | &#160; &#160; if(rank[x] < rank[y]){
      |  ^
a.cc:127:9: error: stray '#' in program
  127 | &#160; &#160; if(rank[x] < rank[y]){
      |         ^
a.cc:128:2: error: stray '#' in program
  128 | &#160; &#160; &#160; par[x] = y;
      |  ^
a.cc:128:9: error: stray '#' in program
  128 | &#160; &#160; &#160; par[x] = y;
      |         ^
a.cc:128:16: error: stray '#' in program
  128 | &#160; &#160; &#160; par[x] = y;
      |                ^
a.cc:129:2: error: stray '#' in program
  129 | &#160; &#160; &#160; if(isBase[find(x)]||isBase[find(y)])
      |  ^
a.cc:129:9: error: stray '#' in program
  129 | &#160; &#160; &#160; if(isBase[find(x)]||isBase[find(y)])
      |         ^
a.cc:129:16: error: stray '#' in program
  129 | &#160; &#160; &#160; if(isBase[find(x)]||isBase[find(y)])
      |                ^
a.cc:131:2: error: stray '#' in program
  131 | &#160; &#160; }
      |  ^
a.cc:131:9: error: stray '#' in program
  131 | &#160; &#160; }
      |         ^
a.cc:132:2: error: stray '#' in program
  132 | &#160; &#160; else{
      |  ^
a.cc:132:9: error: stray '#' in program
  132 | &#160; &#160; else{
      |         ^
a.cc:133:2: error: stray '#' in program
  133 | &#160; &#160; &#160; par[y] = x;
      |  ^
a.cc:133:9: error: stray '#' in program
  133 | &#160; &#160; &#160; par[y] = x;
      |         ^
a.cc:133:16: error: stray '#' in program
  133 | &#160; &#160; &#160; par[y] = x;
      |                ^
a.cc:134:2: error: stray '#' in program
  134 | &#160; &#160; &#160; if(isBase[find(x)]||isBase[find(y)])
      |  ^
a.cc:134:9: error: stray '#' in program
  134 | &#160; &#160; &#160; if(isBase[find(x)]||isBase[find(y)])
      |         ^
a.cc:134:16: error: stray '#' in program
  134 | &#160; &#160; &#160; if(isBase[find(x)]||isBase[find(y)])
      |                ^
a.cc:136:2: error: stray '#' in program
  136 | &#160; &#160; &#160; if(rank[y] == rank[x])rank[x]++;
      |  ^
a.cc:136:9: error: stray '#' in program
  136 | &#160; &#160; &#160; if(rank[y] == rank[x])rank[x]++;
      |         ^
a.cc:136:16: error: stray '#' in program
  136 | &#160; &#160; &#160; if(rank[y] == rank[x])rank[x]++;
      |                ^
a.cc:137:2: error: stray '#' in program
  137 | &#160; &#160; }
      |  ^
a.cc:137:9: error: stray '#' in program
  137 | &#160; &#160; }
      |         ^
a.cc:138:2: error: stray '#' in program
  138 | &#160; }
      |  ^
a.cc:139:2: error: stray '#' in program
  139 | &#160; bool same(int x,int y){
      |  ^
a.cc:140:2: error: stray '#' in program
  140 | &#160; &#160; return find(x) == find(y);
      |  ^
a.cc:140:9: error: stray '#' in program
  140 | &#160; &#160; return find(x) == find(y);
      |         ^
a.cc:141:2: error: stray '#' in program
  141 | &#160; }
      |  ^
a.cc:148:2: error: stray '#' in program
  148 | &#160; int to;
      |  ^
a.cc:149:2: error: stray '#' in program
  149 | &#160; int cost;
      |  ^
a.cc:150:2: error: stray '#' in program
  150 | &#160; int idx;
      |  ^
a.cc:161:2: error: stray '#' in program
  161 | &#160; priority_queue<int> cand;
      |  ^
a.cc:162:2: error: stray '#' in program
  162 | &#160; UnionFindTree uft(V+1);
      |  ^
a.cc:163:2: error: stray '#' in program
  163 | &#160; for(int i=0;i<MAX_V;i++)used[i]=false;
      |  ^
a.cc:164:2: error: stray '#' in program
  164 | &#160; for(int i=0;i<bases.size();i++)uft.isBase[bases[i]]=true;
      |  ^
a.cc:165:2: error: stray '#' in program
  165 | &#160; priority_queue<pair<pii,pii> > pq;
      |  ^
a.cc:166:2: error: stray '#' in program
  166 | &#160; for(int i=0;i<bases.size();i++){
      |  ^
a.cc:167:2: error: stray '#' in program
  167 | &#160; &#160; // 次の場所をコストとともに登録
      |  ^
a.cc:167:9: error: stray '#' in program
  167 | &#160; &#160; // 次の場所をコストとともに登録
      |         ^
a.cc:168:2: error: stray '#' in program
  168 | &#160; &#160; for(int j=0;j<G[bases[i]].size();j++){
      |  ^
a.cc:168:9: error: stray '#' in program
  168 | &#160; &#160; for(int j=0;j<G[bases[i]].size();j++){
      |         ^
a.cc:169:2: error: stray '#' in program
  169 | &#160; &#160; &#160; edge &e=G[bases[i]][j];
      |  ^
a.cc:169:9: error: stray '#' in program
  169 | &#160; &#160; &#160; edge &e=G[bases[i]][j];
      |         ^
a.cc:169:16: error: stray '#' in program
  169 | &#160; &#160; &#160; edge &e=G[bases[i]][j];
      |                ^
a.cc:170:2: error: stray '#' in program
  170 | &#160; &#160; &#160; if(used[e.idx])continue;
      |  ^
a.cc:170:9: error: stray '#' in program
  170 | &#160; &#160; &#160; if(used[e.idx])continue;
      |         ^
a.cc:170:16: error: stray '#' in program
  170 | &#160; &#160; &#160; if(used[e.idx])continue;
      |                ^
a.cc:171:2: error: stray '#' in program
  171 | &#160; &#160; &#160; used[e.idx]=true;
      |  ^
a.cc:171:9: error: stray '#' in program
  171 | &#160; &#160; &#160; used[e.idx]=true;
      |         ^
a.cc:171:16: error: stray '#' in program
  171 | &#160; &#160; &#160; used[e.idx]=true;
      |                ^
a.cc:172:2: error: stray '#' in program
  172 | &#160; &#160; &#160; pq.push(make_pair(pii(e.cost,e.idx),pii(e.to,bases[i])));
      |  ^
a.cc:172:9: error: stray