結果

問題 No.806 木を道に
ユーザー ahe100ahe100
提出日時 2019-03-22 22:55:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 949 bytes
コンパイル時間 1,722 ms
コンパイル使用メモリ 173,984 KB
実行使用メモリ 12,944 KB
最終ジャッジ日時 2023-10-19 10:10:44
合計ジャッジ時間 3,865 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,448 KB
testcase_01 AC 3 ms
6,448 KB
testcase_02 AC 3 ms
6,448 KB
testcase_03 AC 3 ms
6,448 KB
testcase_04 AC 3 ms
6,448 KB
testcase_05 AC 3 ms
6,452 KB
testcase_06 AC 3 ms
6,448 KB
testcase_07 AC 3 ms
6,448 KB
testcase_08 AC 3 ms
6,448 KB
testcase_09 AC 3 ms
6,448 KB
testcase_10 AC 20 ms
7,256 KB
testcase_11 AC 18 ms
7,176 KB
testcase_12 AC 84 ms
9,868 KB
testcase_13 AC 58 ms
8,820 KB
testcase_14 AC 77 ms
9,680 KB
testcase_15 AC 83 ms
9,900 KB
testcase_16 AC 28 ms
7,588 KB
testcase_17 AC 70 ms
9,396 KB
testcase_18 AC 9 ms
6,752 KB
testcase_19 AC 22 ms
7,300 KB
testcase_20 AC 69 ms
9,392 KB
testcase_21 AC 37 ms
7,984 KB
testcase_22 AC 100 ms
10,528 KB
testcase_23 AC 99 ms
10,532 KB
testcase_24 AC 41 ms
10,604 KB
testcase_25 AC 60 ms
12,944 KB
testcase_26 AC 22 ms
7,548 KB
testcase_27 AC 68 ms
11,172 KB
testcase_28 AC 5 ms
6,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll mod = 1e9+7;
//LLONG_MIN

/*
やばい
*/

ll n,d[100010],inf=1e18,ans=0,s=1;
vector<ll> v[100010];
queue<ll> Q;

void f(ll p){
	if(v[p].size()>1)ans+=v[p].size()-2;
	for(ll q:v[p]){
		if(d[q]==d[p]+1){
			f(q);
		}
	}
}

int main(void){
	cin>>n;
	rep(i,n-1){
		ll a,b;
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	reg(i,1,n){
		if(v[i].size()==1){
			s=i;
			break;
		}
	}
	reg(i,1,n)d[i]=inf;
	queue<ll> Q;
	Q.push(s);
	d[s]=0;
	while(!Q.empty()){
		ll p=Q.front();Q.pop();
		for(ll q:v[p]){
			if(d[q]>d[p]+1){
				d[q]=d[p]+1;
				Q.push(q);
			}
		}
	}
	// cout<<s<<endl;
	f(s);
	cout<<ans<<endl;
	return 0;
}
0