結果

問題 No.806 木を道に
ユーザー lapilapi
提出日時 2019-08-04 20:37:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,442 bytes
コンパイル時間 1,016 ms
コンパイル使用メモリ 112,992 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2023-09-23 01:48:24
合計ジャッジ時間 5,380 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,364 KB
testcase_01 AC 3 ms
5,320 KB
testcase_02 AC 3 ms
5,372 KB
testcase_03 AC 3 ms
5,144 KB
testcase_04 AC 4 ms
5,176 KB
testcase_05 AC 4 ms
5,388 KB
testcase_06 AC 3 ms
5,180 KB
testcase_07 AC 4 ms
5,156 KB
testcase_08 AC 3 ms
5,392 KB
testcase_09 AC 3 ms
5,180 KB
testcase_10 AC 11 ms
6,240 KB
testcase_11 AC 10 ms
5,940 KB
testcase_12 AC 39 ms
7,960 KB
testcase_13 AC 27 ms
7,412 KB
testcase_14 AC 35 ms
8,260 KB
testcase_15 AC 37 ms
8,284 KB
testcase_16 AC 13 ms
6,416 KB
testcase_17 AC 33 ms
7,776 KB
testcase_18 AC 6 ms
5,692 KB
testcase_19 AC 10 ms
6,192 KB
testcase_20 AC 33 ms
7,744 KB
testcase_21 AC 18 ms
6,744 KB
testcase_22 AC 44 ms
8,592 KB
testcase_23 AC 44 ms
8,640 KB
testcase_24 AC 18 ms
7,272 KB
testcase_25 AC 26 ms
8,104 KB
testcase_26 AC 10 ms
6,788 KB
testcase_27 AC 28 ms
9,344 KB
testcase_28 AC 4 ms
5,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <numeric>
#include <regex>
#include <tuple>
#include <iomanip>
#include <cmath>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1000000007 // 10^9 + 7
#define INF 1000000000 // 10^9
#define LLINF 1LL<<60

vector<vector<int>> G(100009); // 重みなし

bool isfound[100009];
int startpos() {
	int res = 1;
	isfound[1] = true;
	bool flag = true;
	while (flag) {
		flag = false;

		// 次の点を探す
		for (int i = 0; i < G[res].size() && !flag; i++) {
			int nxt = G[res][i];
			if (!isfound[nxt]) {
				isfound[nxt] = true;
				flag = true;
				res = nxt;
			}
		}

		// 無かったら終わり
	}

	return res;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N; cin >> N;
	for (int i = 0; i < N-1; i++) {
		int a, b; cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}

	int start = startpos();

	for (int i = 1; i <= N; i++) isfound[i] = false;
	int cnt = 0;
	stack<int> S;
	S.push(start);
	isfound[start] = true;

	while (!S.empty()) {
		int pos = S.top();
		S.pop();
		int num = 0;
		for (int i = 0; i < G[pos].size(); i++) {
			int nxt = G[pos][i];
			if (!isfound[nxt]) {
				isfound[nxt] = true;
				num++;
				S.push(nxt);
			}
		}

		if (num > 0) cnt += num - 1;
	}

	cout << cnt << endl;


	return 0;
}
0