結果

問題 No.277 根掘り葉掘り
ユーザー naimonon77naimonon77
提出日時 2016-02-15 22:17:15
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,282 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 80,012 KB
実行使用メモリ 23,968 KB
最終ジャッジ日時 2023-10-22 05:52:33
合計ジャッジ時間 5,672 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,028 KB
testcase_01 AC 4 ms
9,028 KB
testcase_02 AC 4 ms
9,028 KB
testcase_03 AC 5 ms
9,036 KB
testcase_04 AC 3 ms
9,036 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 4 ms
9,028 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int distance_from_node_to_leef(int)’:
main.cpp:111:1: warning: control reaches end of non-void function [-Wreturn-type]
  111 | }
      | ^
main.cpp: In function ‘int main()’:
main.cpp:120:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  120 |     scanf("%d %d",&x,&y);
      |     ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <cstring>
#include <algorithm>
#include <limits.h>
#include <cmath>
#include <cstdio>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <cstdlib>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define NIL -1

struct Root {
  int parent,right,left,depth;
};

Root tree[100005];
queue<int> qu;
set<int> way[100005];
//               root
int distance_min = INT_MAX;
int target;

void create_tree() {
  bool visited[100005] = {};
  int node = 1;
  tree[node].depth = 0;
  visited[node] = true;
  qu.push(node);
  while( ! qu.empty() ) {
    node = qu.front(); qu.pop();
    set<int>::iterator itr = way[node].begin();
    set<int>::iterator past_itr = itr;
    for(; itr != way[node].end(); itr++) {
      if( ! visited[*itr] ) {
        tree[node].left = *itr;
        tree[*itr].depth = tree[node].depth+1;
        tree[*itr].parent = node;
        past_itr = itr;
        break;
      }
    }
    for(; itr != way[node].end(); itr++) {
      if( ! visited[*itr] ) {
        tree[*itr].depth = tree[node].depth+1;
        tree[*itr].parent = node;
        tree[*past_itr].right = *itr;
        past_itr = itr;
      }
    }
    itr--;
    tree[*itr].right = NIL;
    itr = way[node].begin();
    for(; itr != way[node].end(); itr++) {
      if( ! visited[*itr] ) {
        visited[*itr] = true;
        qu.push(*itr);
      }
    }
  }
}

int distance_from_node_to_leef(int node) {
  int test = 0;
  if(test) printf("        node %d\n",node);
  if(tree[node].left == NIL) return 0;
  if(test) printf("tree[node].left = %d\n",tree[node].left);
  int distance[100005];
  bool visited[100005] = {};
  visited[node] = true;
  distance[node] = 0;
  qu.push(node);
  while( ! qu.empty() ) {
    node = qu.front(); qu.pop();
    int child = tree[node].left;
    for(; child != NIL; child = tree[child].right) {
      if(test) printf("child = %d\n",child);
      if(visited[child] == false) {
        if(tree[child].left == NIL) {
          while (!qu.empty()) qu.pop();
          return distance[node]+1;
        }
        else if(visited[child] == false){
          visited[child] = true;
          distance[child] = distance[node]+1;
          qu.push(child);
          if(test) printf("qu.push(%d)\n",child);
        }
      }
    }
    int parent = tree[node].parent;
    if(parent != NIL
       && visited[parent] == false) {
      visited[parent] = true;
      distance[parent] = distance[node]+1;
      qu.push(parent);
    }
  }
}

int main() {
  int i,j,k;
  int N;
  // freopen("data.txt","r",stdin);
  cin >> N;
  rep(i,N-1) {
    int x,y;
    scanf("%d %d",&x,&y);
    way[x].insert(y);
    way[y].insert(x);
  }
  rep(i,N+1) {
    tree[i].parent = NIL;
    tree[i].right = NIL;
    tree[i].left = NIL;
  }
  create_tree();
  puts("0");
  for(i=2;i<N+1;i++) {
    int r,l;
    /*
    printf("node%d : ",i);
    printf("parent = %2d ",tree[i].parent);
    printf("left = %2d ",tree[i].left);
    printf("right = %2d",tree[i].right);
    puts("");*/
    r = tree[i].depth;
    l = distance_from_node_to_leef(i);
    printf("%d\n",min(l,r));
  }
}
0