forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eulerian-Tour(Digraph).cpp
112 lines (102 loc) · 1.38 KB
/
Eulerian-Tour(Digraph).cpp
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
#include <cstdio>
#define NIL 0
using namespace std;
struct vertex
{
int first, ideg, odeg;
bool vis;
}V[100010];
struct edge
{
int op, endp, next;
}E[200010];
int ec = 1, ivc, iec, cnt = 0;
int path[200010], pc;
void add_edge(int u, int v)
{
V[u].odeg++;
V[v].ideg++;
E[ec].next = V[u].first;
V[u].first = ec;
E[ec].op = u;
E[ec].endp = v;
ec++;
}
void del_edge(int ord)
{
V[E[ord].op].first = E[ord].next;
}
void connect(int u)
{
if (V[u].vis == false)
{
cnt++;
V[u].vis = true;
for (int cur = V[u].first; cur != 0; cur = E[cur].next)
{
connect(E[cur].endp);
}
}
}
void DFS(int u, int start)
{
int first = V[u].first;
del_edge(first);
if (E[first].endp != start)
{
DFS(E[first].endp, start);
}
path[pc++] = first;
while (V[u].first != NIL)
{
DFS(u, u);
}
}
int main()
{
int k, u, v;
scanf("%d%d%d", &k, &ivc, &iec);
for (int i = 0; i < iec; i++)
{
scanf("%d%d", &u, &v);
add_edge(u, v);
}
for (int i = 1; i <= ivc; i++)
{
if (V[i].ideg != V[i].odeg)
{
printf("NO");
return 0;
}
if (V[i].ideg == 0)
{
cnt++;
}
}
int vhe;
for (int i = 1; i <= ivc; i++)
{
if (V[i].ideg != 0)
{
connect(i);
vhe = i;
break;
}
}
if (cnt < ivc)
{
printf("NO");
return 0;
}
printf("YES\n");
if (iec == 0)
{
return 0;
}
DFS(vhe, vhe);
for (int i = pc - 1; i >= 0; i--)
{
printf("%d ", path[i]);
}
return 0;
}