Skip to content

Instantly share code, notes, and snippets.

@taross-f
Created January 6, 2017 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taross-f/1c09ae1c6bbf5d5cc3edd27c34f9d198 to your computer and use it in GitHub Desktop.
Save taross-f/1c09ae1c6bbf5d5cc3edd27c34f9d198 to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Web.dll</Reference>
<NuGetReference>Microsoft.Net.Http</NuGetReference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>Sendgrid</NuGetReference>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Bson</Namespace>
<Namespace>Newtonsoft.Json.Converters</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>Newtonsoft.Json.Schema</Namespace>
<Namespace>Newtonsoft.Json.Serialization</Namespace>
<Namespace>SendGrid.SmtpApi</Namespace>
<Namespace>System.IO</Namespace>
<Namespace>System.Net</Namespace>
<Namespace>System.Web.Mail</Namespace>
</Query>
// 提出用using
//using System;
//using System.Linq;
//using System.Collections.Generic;
public class DFS
{
public static void Main()
{
var n = int.Parse(Console.ReadLine());
var u = Enumerable.Range(0, n)
.Select(_ => Console.ReadLine());
var builtupNodes = Build(u);
var explorer = new Explorer();
while (builtupNodes.Any(x => x.FoundTime == 0))
{
explorer.Explore(builtupNodes.First(x => x.FoundTime == 0));
}
// 出力用 ForEach用にToList()
builtupNodes.ToList()
.ForEach(x => Console.WriteLine(x.Id + " " + x.FoundTime + " " + x.FinishedTime));
}
static IEnumerable<Node> Build(IEnumerable<string> u)
{
var nodeAndInputs = u.Select(x => x.Split(' '))
.Select(x => x.Select(int.Parse))
.Select(x => new
{
node = new Node(x.First()),
input = x
})
.ToList();
// Nodeを一旦つくってから子供を入れている。ループ2回回しててあんまりうまくないです。
return nodeAndInputs.Select(nodeinput =>
{
nodeinput.node.Children = nodeAndInputs
.Select(x => x.node)
.Where(x => nodeinput.input.Skip(2).Contains(x.Id));
return nodeinput.node;
})
.OrderBy(x => x.Id);
}
class Explorer
{
private int time = 0;
public void Explore(Node node)
{
node.FoundTime = ++time;
node.Children.ToList().ForEach(x => { if (x.FoundTime == 0) Explore(x); });
node.FinishedTime = ++time;
}
}
class Node
{
public int Id { get; private set; }
public IEnumerable<Node> Children { get; set; }
public int FoundTime { get; set; }
public int FinishedTime { get; set; }
public Node(int id) { Id = id; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment