結果

問題 No.806 木を道に
ユーザー ahe100ahe100
提出日時 2019-03-22 22:41:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,980 bytes
コンパイル時間 1,761 ms
コンパイル使用メモリ 175,140 KB
実行使用メモリ 9,268 KB
最終ジャッジ日時 2023-10-19 09:54:19
合計ジャッジ時間 7,214 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,468 KB
testcase_01 AC 4 ms
8,468 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 3 ms
8,468 KB
testcase_08 AC 3 ms
8,468 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

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,visit[2][100010]={},used[100010]={},d[2][100010],inf=1e18,ans=0;
vector<ll> v[100010];
queue<ll> Q;

void reset(ll p){
	queue<ll> Q;
	Q.push(p);
	d[0][p]=inf;
	d[1][p]=inf;
	while(!Q.empty()){
		p=Q.front();Q.pop();
		for(ll q:v[p]){
			if(used[q]==0 && d[0][q]!=inf){
				d[0][q]=inf;
				d[1][q]=inf;
				Q.push(q);
			}
		}
	}
	// cout<<p<<" reset"<<endl;
}

void f(ll p,ll m){
	queue<ll> Q;
	Q.push(p);
	d[m][p]=0;
	while(!Q.empty()){
		p=Q.front();Q.pop();
		for(ll q:v[p]){
			if(used[q]==0 && d[m][q]>d[m][p]+1){
				d[m][q]=d[m][p]+1;
				Q.push(q);
			}
		}
	}
	// cout<<p<<" dist"<<endl;
}

ll g(ll p,ll m){
	ll largest=p;
	queue<ll> Q;
	Q.push(p);
	while(!Q.empty()){
		p=Q.front();Q.pop();
		for(ll q:v[p]){
			if(used[q]==0){
				if(d[m][largest]<d[m][q]){
					largest=q;
					Q.push(q);
				}
			}
		}
	}
	// cout<<p<<" largest"<<endl;
	return largest;
}

void h(ll p){
	// reg(i,1,n){
	// 	cout<<i<<": "<<d[0][i]<<","<<d[1][i]<<endl;
	// }
	queue<ll> R;
	R.push(p);
	while(!R.empty()){
		p=R.front();R.pop();
		used[p]=1;
		// cout<<p<<endl;
		for(ll q:v[p]){
			if(used[q]==0){
				if(d[0][p]+d[1][p]==d[0][q]+d[1][q]){
					R.push(q);
				}else{
					//それ以外の点
					Q.push(q);
					ans++;
				}
			}
		}
	}
	// cout<<p<<" used"<<endl;
}

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);
	}
	ll p=1;
	Q.push(1);
	while(!Q.empty()){
		ll p=Q.front();Q.pop();
		reset(p);
		f(p,0);
		ll q=g(p,0);//最遠点1
		reset(q);
		f(q,0);
		ll r=g(q,0);//最遠点2
		f(r,1);
		h(q);
		// cout<<ans<<endl;
	}
	cout<<ans<<endl;
	return 0;
}
0