結果

問題 No.994 ばらばらコイン
ユーザー LCA_TrimLCA_Trim
提出日時 2020-02-21 22:24:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 1,147 bytes
コンパイル時間 1,096 ms
コンパイル使用メモリ 102,368 KB
実行使用メモリ 30,796 KB
最終ジャッジ日時 2024-04-17 07:42:52
合計ジャッジ時間 2,981 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 125 ms
27,008 KB
testcase_04 AC 126 ms
25,984 KB
testcase_05 AC 64 ms
15,360 KB
testcase_06 AC 162 ms
30,796 KB
testcase_07 AC 117 ms
26,112 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 112 ms
24,576 KB
testcase_10 AC 79 ms
19,200 KB
testcase_11 AC 109 ms
24,192 KB
testcase_12 AC 103 ms
23,936 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iostream>
#include <deque>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int main()
{
	int n, k;
	cin >> n >> k;

	if (k > n)
	{
		cout << -1 << endl;
		return 0;
	}

	vector<unordered_set<int>> edges(n);

	for (int i = 0; i < n - 1; ++i)
	{
		int a, b;
		cin >> a >> b;
		edges.at(a - 1).emplace(b - 1);
		edges.at(b - 1).emplace(a - 1);
	}

	deque<int> searchingNode;
	for (int node : edges.at(0))
	{
		searchingNode.emplace_back(node);
	}

	deque<int> nextCandidateNode;

	unordered_set<int> reachedNode;
	reachedNode.emplace(0);

	int depth = 0;
	long long result = 0;

	while (k > 1)
	{
		++depth;
		for (int node : searchingNode)
		{
			--k;
			++result;

			if( k <= 1)
			{
				break;
			}

			for (int nextNode : edges.at(node))
			{
				if (reachedNode.find(nextNode) == reachedNode.end())
				{
					nextCandidateNode.emplace_back(nextNode);
					reachedNode.emplace(nextNode);
				}
			}
		}
		searchingNode.clear();
		swap(searchingNode, nextCandidateNode);
	}
	cout << result << endl;
}
0