結果

問題 No.277 根掘り葉掘り
ユーザー kotatsugamekotatsugame
提出日時 2020-02-27 20:48:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 197 ms / 3,000 ms
コード長 686 bytes
コンパイル時間 865 ms
コンパイル使用メモリ 78,776 KB
実行使用メモリ 18,500 KB
最終ジャッジ日時 2024-04-21 17:03:46
合計ジャッジ時間 4,081 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,852 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 2 ms
7,236 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 197 ms
18,500 KB
testcase_10 AC 170 ms
11,172 KB
testcase_11 AC 191 ms
10,684 KB
testcase_12 AC 194 ms
14,564 KB
testcase_13 AC 197 ms
11,000 KB
testcase_14 AC 192 ms
11,208 KB
testcase_15 AC 191 ms
11,516 KB
testcase_16 AC 196 ms
11,096 KB
testcase_17 AC 188 ms
11,264 KB
testcase_18 AC 189 ms
11,056 KB
testcase_19 AC 194 ms
10,936 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   28 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int N;
vector<int>G[1<<17];
int L[1<<17],R[1<<17];
queue<int>P;
void dfs(int u,int p,int d)
{
	R[u]=d;
	bool flag=true;
	for(int v:G[u])
	{
		if(p!=v)
		{
			dfs(v,u,d+1);
			flag=false;
		}
	}
	if(flag)
	{
		L[u]=0;
		P.push(u);
	}
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int u,v;cin>>u>>v;
		u--,v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for(int i=0;i<N;i++)L[i]=114514;
	dfs(0,-1,0);
	while(!P.empty())
	{
		int u=P.front();P.pop();
		for(int v:G[u])
		{
			if(L[v]>L[u]+1)
			{
				L[v]=L[u]+1;
				P.push(v);
			}
		}
	}
	for(int i=0;i<N;i++)cout<<min(L[i],R[i])<<endl;
}
0