結果

問題 No.277 根掘り葉掘り
ユーザー 158b158b
提出日時 2015-09-04 23:19:18
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,482 bytes
コンパイル時間 1,004 ms
コンパイル使用メモリ 77,352 KB
実行使用メモリ 4,880 KB
最終ジャッジ日時 2023-09-26 06:41:39
合計ジャッジ時間 4,841 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,376 KB
testcase_07 WA -
testcase_08 AC 1 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 202 ms
4,604 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <climits>
#include <vector>
#include <numeric>
#include <complex>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
using namespace std;

//#define __int64 long long
#define long __int64
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
const int Vecy[4] = {0,-1,0,1};
const int Vecx[4] = {1,0,-1,0};
const int modd = 1000000007;

int kyori[100050];
int from[100050];
int to[100050];
bool leaf[100050];

//デフォルト昇順
void combsort(int max){
	bool flg;
	int h;
	
	h = max / 1.3;
	while(true){
		flg = true;
		
		for(int i=0; i+h<max; i++){
			if(to[i] > to[i+h]){		//ソート基準変更位置
				//swap
				swap(from[i], from[i+h]);
				swap(to[i], to[i+h]);
				
				flg = false;
			}
		}
		
		if(h <= 1){
			if(flg){
				break;
			}
		}else{
			h /= 1.3;
		}
	}
}


int main(){
	int n;
	cin >> n;
	
	//初期化
	for(int i=1; i<=n; i++){
		kyori[i] = INT_MAX / 2;
		leaf[i] = true;
	}
	kyori[1] = 0;
	
	rep(i,n-1){
		cin >> from[i] >> to[i];
		leaf[from[i]] = false;
		kyori[to[i]] = kyori[from[i]] + 1;
	}
	
	//一応ソート
	combsort(n-1);
	
	//100回やってみる。
	for(int cnti=0; cnti<100; cnti++){
		for(int i=n-2; i>=0; i--){
			if(leaf[ to[i] ]){
				kyori[ to[i] ] = 0;
			}
			
			kyori[ from[i] ] = min(kyori[ from[i] ], kyori[ to[i] ] + 1);
		}
	}
	
	for(int i=1; i<=n; i++){
		cout << kyori[i] << endl;
	}
	
    return 0;
}
0