結果
問題 |
No.3087 University Coloring
|
ユーザー |
![]() |
提出日時 | 2025-04-04 21:57:08 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 101 ms / 2,000 ms |
コード長 | 1,948 bytes |
コンパイル時間 | 4,137 ms |
コンパイル使用メモリ | 292,544 KB |
実行使用メモリ | 14,092 KB |
最終ジャッジ日時 | 2025-04-04 21:57:17 |
合計ジャッジ時間 | 8,502 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include <bits/stdc++.h> using namespace std; template <class T> using V=vector<T>; template <class T> using VV=V<V<T>>; class Dis{ public: V<ll> rank,p,siz; int N; Dis(int s){ rank.resize(s,0); p.resize(s,0); siz.resize(s,1); rep(i,s) makeSet(i); N=s; } void makeSet(int x){ p[x]=x; rank[x]=0; } bool same(int x,int y){ return root(x)==root(y); } void unite(int x,int y){ if(same(x,y)) return; link(root(x),root(y)); } void link(int x,int y){ if(rank[x]>rank[y]){ p[y]=x; siz[x]+=siz[y]; } else{ p[x]=y; siz[y]+=siz[x]; if(rank[x]==rank[y]) rank[y]++; } } int root(int x){ if(x != p[x]) p[x]=root(p[x]); return p[x]; } int size(int x){ return siz[root(x)]; } VV<int> groups(){ V<int> root_buf(N),group_size(N); rep(i,N){ root_buf[i]=root(i); group_size[root_buf[i]]++; } VV<int> result(N); rep(i,N) result[i].reserve(group_size[i]); rep(i,N) result[root_buf[i]].push_back(i); result.erase(remove_if(ALL(result),[&](const V<int>& v){return v.empty();}),result.end()); return result; } }; using Edge=pair<ll,pair<int,int>>; ll kruskal(int n,vector<Edge> edges){ sort(ALL(edges)); reverse(ALL(edges)); ll res=0; int m=edges.size(); Dis dset=Dis(n); rep(i,m){ ll w=edges[i].first; int u=edges[i].second.first; int v=edges[i].second.second; if(dset.same(u,v)) continue; res+=w; dset.unite(u,v); } return res; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n,m; cin>>n>>m; vector<Edge> edges(m); rep(i,m){ int u,v; ll w; cin>>u>>v>>w; u--,v--; edges[i]=Edge(w,{u,v}); } cout<<2*kruskal(n,edges)<<endl; return 0; }