class union_find{ vector p,rank; public: union_find(int n):p(n),rank(n){ for(int x=0;x rank[y]) swap(x,y); if(rank[x]==rank[y]) rank[y]++; p[x]=y; } }; struct edge{ int u,v; double cost; }; typedef vector< vector > graph; vector Boruvka(graph G){ // グラフは隣接リストで与える int n=G.size(); // n = |V| graph M(n); // MST は隣接リストの形で求める union_find U(n); while(true){ vector F; // このフェイズで M に追加する辺 ( の候補 ) int c=0; vector id(n,-1); // 連結成分の名前 ( 未割り当てなら -1 ) for(int u=0;u C; queue Q; Q.push(u); while(!Q.empty()){ int v=Q.front(); Q.pop(); C.push_back(v); for(int i=0;if.cost) e=f; } } } if(e.u!=-1) F.push_back(e); } if(c==1) break; // 連結成分が一つだけなら終了 // M に F の辺を追加する for(int i=0;i