結果
問題 | No.994 ばらばらコイン |
ユーザー | KKT89 |
提出日時 | 2020-02-21 21:34:34 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 5 ms / 2,000 ms |
コード長 | 843 bytes |
コンパイル時間 | 805 ms |
コンパイル使用メモリ | 80,004 KB |
最終ジャッジ日時 | 2025-01-09 01:27:58 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
#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; } cout << k-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; }