AtCoder Beginner Contest 014

Submission #4229349

Source codeソースコード

import java.util.*;
import java.util.ArrayDeque;
import java.util.Deque;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] nums = new int[n][n - 1];
        int[] lengths = new int[n];
        int tmpX = 0;
        int tmpY = 0;
        for (int i = 0; i < n - 1; i++) {
            tmpX = sc.nextInt();
            tmpY = sc.nextInt();
            nums[tmpX - 1][lengths[tmpX - 1]] = tmpY;
            lengths[tmpX - 1]++;
            nums[tmpY - 1][lengths[tmpY - 1]] = tmpX;
            lengths[tmpY - 1]++;
        }
        int q = sc.nextInt();
        int nowPlace;
        int oldPlace;
        int nextPlace=0;
        int way;
        int tmp;
        int[] parents = new int[n];
        int[] height = new int[n];
        int lookedPoint=0;
        boolean comeBack = false;
        Deque<Integer> place = new ArrayDeque<>();

        
        nowPlace = 1;
        parents[0]=0;
        height[0]=0;
        oldPlace = 0;
        way = 0;
        
        while (true) {
            if (oldPlace == nums[nowPlace - 1][lengths[nowPlace - 1] - 1]) {

                oldPlace = nowPlace;
                nowPlace = place.pop();
                comeBack = true;
                way--;
            } else {
                if (!comeBack) {
                    place.push(nowPlace);
                    tmp = oldPlace;
                    oldPlace = nowPlace;
                    nowPlace = nums[nowPlace - 1][0] == tmp ? nums[nowPlace - 1][1] : nums[nowPlace - 1][0];
                    way++;
                    parents[nowPlace - 1] = place.peek();
                    height[nowPlace - 1] = way;
                    lookedPoint++;

                    
                } else {
                    for (int j = 0; j < lengths[nowPlace-1]; j++) {
                        if (nums[nowPlace - 1][j] == oldPlace) {
                            nextPlace = nums[nowPlace - 1][j+1];
                            break;
                        }

                    }
                    place.push(nowPlace);
                    oldPlace = nowPlace;
                    nowPlace = nextPlace;
                    way++;
                    parents[nowPlace - 1] = place.peek();
                    height[nowPlace - 1] = way;
                    lookedPoint++;

                }
                comeBack = false;
            }
            if(lookedPoint==n-1){
                break;
            }
        }
        
        int tmpQX;
        int tmpQY;
        int tmpTmpQX;
        int tmpTmpQY;
        way=0;
        flag:for(int i=0;i<q;i++){
            tmpQX=sc.nextInt();
            tmpQY=sc.nextInt();
            way=0;
            if(height[tmpQX-1]>height[tmpQY-1]){
                tmpTmpQX=tmpQX;
                for(int j=0;j<height[tmpTmpQX-1]-height[tmpQY-1];j++){
                    tmpQX=parents[tmpQX-1];
                    way++;
                }
            }else if(height[tmpQY-1]>height[tmpQX-1]){
                tmpTmpQY=tmpQY;
                for(int j=0;j<height[tmpTmpQY-1]-height[tmpQX-1];j++){
                    tmpQY=parents[tmpQY-1];
                    way++;
                }
                
            }
            tmpTmpQX=tmpQX;
            for(int j=0;j<height[tmpTmpQX-1]+1;j++){
                if(tmpQX==tmpQY){
                    System.out.println(way+2*j+1);
                    continue flag;
                }
                tmpQX=parents[tmpQX-1];
                tmpQY=parents[tmpQY-1];
            }
        }
        
    }

}

Submission

Task問題 D - 閉路
User nameユーザ名 nuhunune
Created time投稿日時
Language言語 Java8 (OpenJDK 1.8.0)
Status状態 RE
Score得点 0
Source lengthソースコード長 3724 Byte
File nameファイル名
Exec time実行時間 ms
Memory usageメモリ使用量 -

Test case

Set

Set name Score得点 / Max score Cases
Sample - subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt
Subtask1 0 / 30 subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt
Subtask2 0 / 70 subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask2_01.txt,subtask2_02.txt,subtask2_03.txt,subtask2_04.txt,subtask2_05.txt,subtask2_06.txt,subtask2_07.txt,subtask2_08.txt,subtask2_09.txt,subtask2_10.txt,subtask2_11.txt,subtask2_12.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask0_sample01.txt AC 97 ms 20308 KB
subtask0_sample02.txt AC 96 ms 19796 KB
subtask0_sample03.txt AC 99 ms 19412 KB
subtask1_01.txt MLE
subtask1_02.txt MLE
subtask1_03.txt RE
subtask1_04.txt RE
subtask1_05.txt RE
subtask1_06.txt RE
subtask1_07.txt MLE
subtask1_08.txt MLE
subtask1_09.txt MLE
subtask1_10.txt MLE
subtask1_11.txt MLE
subtask1_12.txt MLE
subtask2_01.txt MLE
subtask2_02.txt MLE
subtask2_03.txt RE
subtask2_04.txt RE
subtask2_05.txt RE
subtask2_06.txt RE
subtask2_07.txt MLE
subtask2_08.txt MLE
subtask2_09.txt MLE
subtask2_10.txt MLE
subtask2_11.txt MLE
subtask2_12.txt MLE