結果

問題 No.806 木を道に
ユーザー lapi
提出日時 2019-08-04 20:37:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,442 bytes
コンパイル時間 1,098 ms
コンパイル使用メモリ 115,936 KB
実行使用メモリ 9,364 KB
最終ジャッジ日時 2024-07-16 02:18:27
合計ジャッジ時間 3,084 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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