AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC212-E Safety Journey
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("3 1 4");
WillReturn.Add("2 3");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 3 3");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("2 3");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("5 3 100");
WillReturn.Add("1 2");
WillReturn.Add("4 5");
WillReturn.Add("2 3");
//428417047
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
const long Hou = 998244353;
// 隣接リスト
static Dictionary<long, List<long>> mToNodeListDict = new Dictionary<long, List<long>>();
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
SplitAct(InputList[0]);
long N = wkArr[0];
long K = wkArr[2];
long UB = N;
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
long FromNode = wkArr[0];
long ToNode = wkArr[1];
if (mToNodeListDict.ContainsKey(FromNode) == false) {
mToNodeListDict[FromNode] = new List<long>();
}
if (mToNodeListDict.ContainsKey(ToNode) == false) {
mToNodeListDict[ToNode] = new List<long>();
}
mToNodeListDict[FromNode].Add(ToNode);
mToNodeListDict[ToNode].Add(FromNode);
}
// 場合の数[現在位置]なDP表
long[] PrevDP = new long[UB + 1];
for (long I = 1; I <= UB; I++) {
PrevDP[I] = 0;
}
PrevDP[1] = 1;
long PrevDPSum = 1;
for (int I = 1; I <= K; I++) {
long[] CurrDP = new long[UB + 1];
long CurrDPSum = 0;
for (long J = 1; J <= UB; J++) {
CurrDP[J] = PrevDPSum;
CurrDP[J] %= Hou;
// 自己ループは存在しない
CurrDP[J] -= PrevDP[J];
if (CurrDP[J] < 0) {
CurrDP[J] += Hou;
}
// NGな辺の分
if (mToNodeListDict.ContainsKey(J)) {
foreach (long EachFromNode in mToNodeListDict[J]) {
CurrDP[J] -= PrevDP[EachFromNode];
if (CurrDP[J] < 0) {
CurrDP[J] += Hou;
}
}
}
CurrDPSum += CurrDP[J];
CurrDPSum %= Hou;
}
PrevDP = CurrDP;
PrevDPSum = CurrDPSum;
}
Console.WriteLine(PrevDP[1]);
}
}
解説
完全グラフで、使えない辺があると考えて
場合の数[現在位置]なDP表を
貰うDPで更新してます。