Created
January 6, 2017 10:38
-
-
Save taross-f/1c09ae1c6bbf5d5cc3edd27c34f9d198 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\System.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Net.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference> | |
<Reference><RuntimeDirectory>\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