結果
問題 |
No.994 ばらばらコイン
|
ユーザー |
|
提出日時 | 2020-02-21 21:31:49 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 804 bytes |
コンパイル時間 | 877 ms |
コンパイル使用メモリ | 84,092 KB |
最終ジャッジ日時 | 2025-01-09 01:27:23 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 7 WA * 16 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; typedef long long int ll; int d[100100]; vector<int> g[100100]; void dfs(int s,int p){ for(int t:g[s]){ if(t!=p){ d[t]=d[s]+1; dfs(t,s); } } } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,k; cin >> n >> k; if(n<k){ cout << -1 << endl; return 0; } for(int i=0;i<n-1;i++){ int x,y; cin >> x >> y; x--; y--; g[x].push_back(y); g[y].push_back(x); } d[0]=0; dfs(0,-1); vector<int> a(n); for(int i=0;i<n;i++){ a[i]=d[i]; } sort(a.begin(),a.end()); ll ans=0; for(int i=0;i<k;i++){ ans+=a[i]; } cout << ans << endl; }