AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC160-D Line++
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 2 4");
//5
//4
//1
//0
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 1 3");
//3
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("7 3 7");
//7
//8
//4
//2
//0
//0
}
else if (InputPattern == "Input4") {
WillReturn.Add("10 4 8");
//10
//12
//10
//8
//4
//1
//0
//0
//0
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct JyoutaiDef
{
internal int Level;
internal int CurrNode;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int N = wkArr[0];
int X = wkArr[1];
int Y = wkArr[2];
int UB = N;
int[,] KyoriArr = new int[UB + 1, UB + 1];
for (int I = 1; I <= N; I++) {
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.Level = 0;
WillEnqueue.CurrNode = I;
Que.Enqueue(WillEnqueue);
var VisitedSet = new HashSet<int>();
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
if (VisitedSet.Add(Dequeued.CurrNode) == false) {
continue;
}
KyoriArr[I, Dequeued.CurrNode] = Dequeued.Level;
Action<int> EnqueueAct = pNewNode =>
{
if (pNewNode < 1 || N < pNewNode) return;
if (VisitedSet.Contains(pNewNode)) return;
WillEnqueue.Level = Dequeued.Level + 1;
WillEnqueue.CurrNode = pNewNode;
Que.Enqueue(WillEnqueue);
};
EnqueueAct(Dequeued.CurrNode + 1);
EnqueueAct(Dequeued.CurrNode - 1);
if (Dequeued.CurrNode == X) {
EnqueueAct(Y);
}
if (Dequeued.CurrNode == Y) {
EnqueueAct(X);
}
}
}
var CntDict = new Dictionary<int, int>();
for (int I = 1; I <= UB; I++) {
for (int J = 1; J <= UB; J++) {
int Kyori = KyoriArr[I, J];
if (CntDict.ContainsKey(Kyori) == false) {
CntDict[Kyori] = 0;
}
CntDict[Kyori]++;
}
}
for (int I = 1; I <= N - 1; I++) {
if (CntDict.ContainsKey(I)) {
Console.WriteLine(CntDict[I] / 2);
}
else {
Console.WriteLine(0);
}
}
}
}
解説
ワーシャルフロイド法を使ったらTLEしました。
全ての辺の距離が1なので、幅優先探索で解きました。