競技プログラミングの鉄則
次の問題へ
前の問題へ
B66 Typhoon
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 2");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
WillReturn.Add("4");
WillReturn.Add("2 2 3");
WillReturn.Add("1 2");
WillReturn.Add("2 1 3");
WillReturn.Add("1 1");
//Yes
//No
}
else if (InputPattern == "Input2") {
WillReturn.Add("12 7");
WillReturn.Add("8 11");
WillReturn.Add("1 7");
WillReturn.Add("10 12");
WillReturn.Add("1 4");
WillReturn.Add("4 8");
WillReturn.Add("5 9");
WillReturn.Add("3 5");
WillReturn.Add("12");
WillReturn.Add("2 6 8");
WillReturn.Add("1 6");
WillReturn.Add("2 10 12");
WillReturn.Add("1 1");
WillReturn.Add("1 5");
WillReturn.Add("1 3");
WillReturn.Add("2 3 5");
WillReturn.Add("1 7");
WillReturn.Add("2 3 6");
WillReturn.Add("1 4");
WillReturn.Add("1 2");
WillReturn.Add("2 9 11");
//No
//Yes
//Yes
//No
//No
}
else if (InputPattern == "Input3") {
WillReturn.Add("4 3");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
WillReturn.Add("3 4");
WillReturn.Add("7");
WillReturn.Add("2 1 2");
WillReturn.Add("2 1 3");
WillReturn.Add("2 1 4");
WillReturn.Add("1 2");
WillReturn.Add("2 1 2");
WillReturn.Add("2 1 3");
WillReturn.Add("2 1 4");
//Yes
//Yes
//Yes
//Yes
//No
//No
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mN;
static int mM;
struct EdgeDef
{
internal int FromNode;
internal int ToNode;
}
static List<EdgeDef> mEdgeList = new List<EdgeDef>();
class QueryDef
{
internal int Type;
internal int CutEdgeInd;
internal int FromNode;
internal int ToNode;
internal string Answer;
}
static List<QueryDef> mQueryList = new List<QueryDef>();
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
mN = wkArr[0];
mM = wkArr[1];
foreach (string EachStr in InputList.Skip(1).Take(mM)) {
SplitAct(EachStr);
EdgeDef WillAdd;
WillAdd.FromNode = wkArr[0];
WillAdd.ToNode = wkArr[1];
mEdgeList.Add(WillAdd);
}
foreach (string EachStr in InputList.Skip(1 + mM + 1)) {
SplitAct(EachStr);
var WillAdd = new QueryDef();
WillAdd.Type = wkArr[0];
if (WillAdd.Type == 1) {
WillAdd.CutEdgeInd = wkArr[1] - 1;
}
if (WillAdd.Type == 2) {
WillAdd.FromNode = wkArr[1];
WillAdd.ToNode = wkArr[2];
}
mQueryList.Add(WillAdd);
}
var CutEdgeSet = new HashSet<int>();
foreach (QueryDef EachQuery in mQueryList) {
if (EachQuery.Type == 1) {
CutEdgeSet.Add(EachQuery.CutEdgeInd);
}
}
var InsUnionFind = new UnionFind();
for (int I = 1; I <= mN; I++) {
InsUnionFind.MakeSet(I);
}
for (int I = 0; I <= mEdgeList.Count - 1; I++) {
if (CutEdgeSet.Contains(I) == false) {
InsUnionFind.Unite(mEdgeList[I].FromNode, mEdgeList[I].ToNode);
}
}
for (int I = mQueryList.Count - 1; 0 <= I; I--) {
if (mQueryList[I].Type == 1) {
int Ind = mQueryList[I].CutEdgeInd;
InsUnionFind.Unite(mEdgeList[Ind].FromNode, mEdgeList[Ind].ToNode);
}
if (mQueryList[I].Type == 2) {
int FromNode = mQueryList[I].FromNode;
int ToNode = mQueryList[I].ToNode;
int Root1 = InsUnionFind.FindSet(FromNode);
int Root2 = InsUnionFind.FindSet(ToNode);
if (Root1 == Root2) {
mQueryList[I].Answer = "Yes";
}
else {
mQueryList[I].Answer = "No";
}
}
}
for (int I = 0; I <= mQueryList.Count - 1; I++) {
if (mQueryList[I].Type == 2) {
Console.WriteLine(mQueryList[I].Answer);
}
}
}
}
#region UnionFind
// UnionFindクラス
internal class UnionFind
{
private class NodeInfoDef
{
internal int ParentNode;
internal int Rank;
}
private Dictionary<int, NodeInfoDef> mNodeInfoDict =
new Dictionary<int, NodeInfoDef>();
// 要素が1つである木を森に追加
internal void MakeSet(int pNode)
{
NodeInfoDef WillAdd = new NodeInfoDef();
WillAdd.ParentNode = pNode;
WillAdd.Rank = 0;
mNodeInfoDict[pNode] = WillAdd;
}
// 合併処理
internal void Unite(int pX, int pY)
{
int XNode = FindSet(pX);
int YNode = FindSet(pY);
int XRank = mNodeInfoDict[XNode].Rank;
int YRank = mNodeInfoDict[YNode].Rank;
if (XRank > YRank) {
mNodeInfoDict[YNode].ParentNode = XNode;
}
else {
mNodeInfoDict[XNode].ParentNode = YNode;
if (XRank == YRank) {
mNodeInfoDict[YNode].Rank++;
}
}
}
// ノードを引数として、木の根を取得
internal int FindSet(int pTargetNode)
{
// 根までの経路上のノードのList
var PathNodeList = new List<int>();
int CurrNode = pTargetNode;
while (CurrNode != mNodeInfoDict[CurrNode].ParentNode) {
PathNodeList.Add(CurrNode);
CurrNode = mNodeInfoDict[CurrNode].ParentNode;
}
// 経路圧縮 (親ポインタの付け替え)
foreach (int EachPathNode in PathNodeList) {
mNodeInfoDict[EachPathNode].ParentNode = CurrNode;
}
return CurrNode;
}
internal void DebugPrint()
{
foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
EachPair.Key, EachPair.Value.ParentNode);
}
}
}
#endregion
解説
UnionFindの、切断処理は、計算量が悪いので、
クエリを先読みして、逆から解を求めてます。