結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー kotatsugame
提出日時 2021-04-21 00:01:36
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 565 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 77,484 KB
実行使用メモリ 18,044 KB
最終ジャッジ日時 2024-07-04 05:32:16
合計ジャッジ時間 5,001 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:26:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   26 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int N;
vector<int>G[2<<17];
pair<int,int>f(int s)
{
	vector<int>d(N,1e9);
	d[s]=0;
	queue<int>P;
	P.push(s);
	pair<int,int>ret;
	while(!P.empty())
	{
		int u=P.front();P.pop();
		ret=make_pair(u,d[u]);
		for(int v:G[u])if(d[v]>d[u]+1)
		{
			d[v]=d[u]+1;
			P.push(v);
		}
	}
	return ret;
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int a,b;cin>>a>>b;
		a--,b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	pair<int,int>p=f(0);
	pair<int,int>q=f(p.first);
	cout<<2*N-q.second-2<<endl;
}
0