-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEIPRF.cs
164 lines (140 loc) · 4.19 KB
/
EIPRF.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace DiscreteMath2
{
class EICONP
{
static Vertex root;
static StringBuilder result = new StringBuilder("");
static Stack<int> stack = new Stack<int>();
static int count = 0;
static int n = 0;
static void Main(string[] args)
{
n = NextInt();
var m = NextInt();
var nodes = ReadGraph(n, m);
for (int i = 0; i < n; i++)
{
nodes[i].children.Sort();
}
root = nodes[0];
Console.WriteLine(DFS(root));
}
static bool bol = false;
static StringBuilder DFS(Vertex u)
{
if (bol)
{
return result;
}
else
{
u.IsVisited = true;
count++;
result.Append(u.id + " ");
foreach (var item in u.children)
{
if(item.children.Count == 0)
{
item.IsVisited = true;
}
else if (!item.IsVisited)
{
return DFS(item);
}
else if (item.id == 0)
{
bol = true;
return result;
}
}
}
if (bol)
{
return result;
}
return result;
}
static Vertex[] ReadGraph(int nVertices, int nEdges)
{
Vertex[] nodes = new Vertex[nVertices];
for (var i = 0; i < nVertices; i++)
{
nodes[i] = new Vertex(i);
}
for (var i = 0; i < nEdges; i++)
{
var vi = NextInt();
var vj = NextInt();
// Directed Graph
nodes[vi].AddChild(nodes[vj]);
}
foreach (var node in nodes)
{
node.children.Sort();
}
return nodes;
}
class Vertex : IComparable<Vertex>
{
public int id = -1;
public List<Vertex> children = new List<Vertex>();
public bool IsVisited;
public int Count { get { return children.Count; } }
public Vertex(int id)
{
this.id = id;
}
public void AddChild(Vertex child)
{
children.Add(child);
}
public int CompareTo(Vertex other)
{
return id - other.id;
}
}
static int s_index = 0;
static List<string> s_tokens;
private static string Next()
{
while (s_tokens == null || s_index == s_tokens.Count)
{
s_tokens = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
s_index = 0;
}
return s_tokens[s_index++];
}
private static bool HasNext()
{
while (s_tokens == null || s_index == s_tokens.Count)
{
s_tokens = null;
s_index = 0;
var nextLine = Console.ReadLine();
if (nextLine != null)
{
s_tokens = nextLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
else
{
return false;
}
}
return s_tokens.Count > 0;
}
private static int NextInt()
{
return Int32.Parse(Next());
}
private static long NextLong()
{
return Int64.Parse(Next());
}
}
}