Submission #230699


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace AtCoderBeginner014
{
	class Program
	{
		public static bool[] check;

		static void Main(string[] args)
		{
			int N = int.Parse(Console.ReadLine());
			int[] x = new int[ N ];
			int[] y = new int[ N ];
			for (int i = 0; i < N - 1; ++i) {
				string[] input = Console.ReadLine().Split();
				x[ i ] = int.Parse(input[ 0 ]) - 1;
				y[ i ] = int.Parse(input[ 1 ]) - 1;
			}
			int Q = int.Parse(Console.ReadLine());
			int[] a = new int[ Q ];
			int[] b = new int[ Q ];
			for (int i = 0; i < Q; ++i) {
				string[] input = Console.ReadLine().Split();
				a[ i ] = int.Parse(input[ 0 ]) - 1;
				b[ i ] = int.Parse(input[ 1 ]) - 1;
			}

			for (int i = 0; i < Q; ++i) {
				x[ N - 1 ] = a[ i ];
				y[ N - 1 ] = b[ i ];

				check = new bool[ N ];
				Stack<int> stack = new Stack<int>();
				stack.Push(x[ N - 1 ]);
				int result = recurse(N, x, y, 0, -1, x[ N - 1 ]);
				Console.WriteLine(result);
			}

			Console.ReadLine();
		}

		public static int recurse(int N, int[] x, int[] y, int sum, int from, int now) {

			Stack<int> stack = new Stack<int>();

			// vを訪れる
			int v = now;
			check[ v ] = true;
			++sum;

			// vから繋がる頂点を追加
			for (int i = 0; i < N; ++i) {
				for (int j = 0; j < 2; ++j) {
					int origin = -1;
					int target = -1;
					if (j == 0) {
						origin = x[ i ];
						target = y[ i ];
					} else {
						origin = y[ i ];
						target = x[ i ];
					}
					if (origin == v) { // vから繋がる頂点を発見
						if (target == x[ N - 1 ] && target != from) { // 最初に帰った
							return sum;
						}
						if (!check[ target ]) {
							stack.Push(target);
						}
					}
				}
			}

			while (stack.Count > 0) {
				int to = stack.Pop();
				int ret = recurse(N, x, y, sum, v, to);
				if (ret > 0) { // スタート地点に到達した
					return ret;
				}
			}

			return 0;
		}
	}
}

Submission Info

Submission Time
Task D - 閉路
User ValGrowth
Language C# (Mono 2.10.8.1)
Score 0
Code Size 2047 Byte
Status TLE
Exec Time 2037 ms
Memory 11184 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 0 / 30 0 / 70
Status
AC × 3
AC × 4
TLE × 8
AC × 8
TLE × 19
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
Subtask1 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 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
Case Name Status Exec Time Memory
subtask0_sample01.txt AC 113 ms 8960 KB
subtask0_sample02.txt AC 111 ms 8932 KB
subtask0_sample03.txt AC 115 ms 8936 KB
subtask1_01.txt TLE 2033 ms 9836 KB
subtask1_02.txt TLE 2034 ms 9924 KB
subtask1_03.txt AC 112 ms 8928 KB
subtask1_04.txt AC 109 ms 8988 KB
subtask1_05.txt AC 124 ms 9056 KB
subtask1_06.txt AC 115 ms 9052 KB
subtask1_07.txt TLE 2034 ms 9772 KB
subtask1_08.txt TLE 2033 ms 9740 KB
subtask1_09.txt TLE 2034 ms 9740 KB
subtask1_10.txt TLE 2033 ms 9772 KB
subtask1_11.txt TLE 2034 ms 9740 KB
subtask1_12.txt TLE 2033 ms 9744 KB
subtask2_01.txt TLE 2033 ms 11184 KB
subtask2_02.txt TLE 2033 ms 11116 KB
subtask2_03.txt AC 1197 ms 10588 KB
subtask2_04.txt TLE 2033 ms 10400 KB
subtask2_05.txt TLE 2034 ms 10272 KB
subtask2_06.txt TLE 2034 ms 10396 KB
subtask2_07.txt TLE 2037 ms 11020 KB
subtask2_08.txt TLE 2035 ms 11096 KB
subtask2_09.txt TLE 2034 ms 11024 KB
subtask2_10.txt TLE 2034 ms 11024 KB
subtask2_11.txt TLE 2033 ms 11048 KB
subtask2_12.txt TLE 2033 ms 11020 KB