結果

問題 No.277 根掘り葉掘り
ユーザー Kmcode1Kmcode1
提出日時 2015-09-04 22:37:04
言語 C++11
(gcc 11.4.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,720 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 104,328 KB
実行使用メモリ 33,264 KB
最終ジャッジ日時 2023-09-26 05:41:13
合計ジャッジ時間 6,916 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int dfs(int, int)’:
main.cpp:52:15: warning: control reaches end of non-void function [-Wreturn-type]
  map<int,int> s;
               ^

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
#include<functional>
#include<climits>
#include<cassert>
#include<iterator>
using namespace std;
#define MAX 100002
int n;
vector<int> v[MAX];
bool use[MAX];
int ans[MAX];
int dep[MAX];
inline void dfs2(int a){
	use[a] = true;
	dep[a] = INT_MAX;
	int countt = 0;
	for (int i = 0; i < v[a].size(); i++){
		if (use[v[a][i]] == false){
			dfs2(v[a][i]);
			dep[a] = min(dep[a], dep[v[a][i]] + 1);
			countt++;
		}
	}
	if (countt== 0){
		dep[a] = 0;
	}
	return;
}
inline int dfs(int a, int b){
	use[a] = false;
	ans[a] = INT_MAX;
	map<int,int> s;
	s.clear();
	int countt = 0;
	for (int i = 0; i < v[a].size(); i++){
		if (use[v[a][i]] == false){
			continue;
		}
		countt++;
		s[dep[v[a][i]]+1]++;
	}
	if (countt == 0){
		ans[a] = 0;
		return 0;
	}
	s[b]++;
	map<int, int>::iterator ite;
	ite = s.begin();
	ans[a] = (*ite).first;
	for (int i = 0; i < v[a].size(); i++){
		if (use[v[a][i]] == false){
			continue;
		}
		int val = dep[v[a][i]]+1;
		s[val]--;
		if (s[val] == 0){
			s.erase(val);
		}
		ite = s.begin();
		dfs(v[a][i], (*ite).first+1);
		s[val]++;
	}

}
int main(){
	scanf("%d", &n);
	for (int i = 1; i < n; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		a--;
		b--;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for (int i = 0; i < n; i++){
		ans[i] = INT_MAX;
	}
	dfs2(0);
	dfs(0, 0);
	for (int i = 0; i < n; i++){
		printf("%d\n", ans[i]);
	}
	return 0;
}
0