-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy patheuler_tour_undirected.cpp
More file actions
174 lines (151 loc) · 4.92 KB
/
Copy patheuler_tour_undirected.cpp
File metadata and controls
174 lines (151 loc) · 4.92 KB
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
165
166
167
168
169
170
171
172
173
174
//
// 無向オイラー路を求める
//
// verified:
// Codeforces 554 DIV2 E. Neko and Flashback
// https://codeforces.com/contest/1152/problem/E
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// edge class (for network-flow)
template<class FLOWTYPE> struct Edge {
int rev, from, to;
FLOWTYPE cap, icap;
Edge(int r, int f, int t, FLOWTYPE c) : rev(r), from(f), to(t), cap(c), icap(c) {}
friend ostream& operator << (ostream& s, const Edge& E) {
if (E.cap > 0) return s << E.from << "->" << E.to << '(' << E.cap << ')';
else return s;
}
};
// graph class (for network-flow)
template<class FLOWTYPE> struct Graph {
vector<vector<Edge<FLOWTYPE> > > list;
Graph(int n = 0) : list(n) { }
void init(int n = 0) { list.clear(); list.resize(n); }
void reset() { for (int i = 0; i < (int)list.size(); ++i) for (int j = 0; j < list[i].size(); ++j) list[i][j].cap = list[i][j].icap; }
inline vector<Edge<FLOWTYPE> >& operator [] (int i) { return list[i]; }
inline const size_t size() const { return list.size(); }
inline Edge<FLOWTYPE> &redge(Edge<FLOWTYPE> e) {
if (e.from != e.to) return list[e.to][e.rev];
else return list[e.to][e.rev + 1];
}
void addedge(int from, int to, FLOWTYPE cap) {
list[from].push_back(Edge<FLOWTYPE>((int)list[to].size(), from, to, cap));
list[to].push_back(Edge<FLOWTYPE>((int)list[from].size() - 1, to, from, 0));
}
void add_undirected_edge(int from, int to, FLOWTYPE cap) {
list[from].push_back(Edge<FLOWTYPE>((int)list[to].size(), from, to, cap));
list[to].push_back(Edge<FLOWTYPE>((int)list[from].size() - 1, to, from, cap));
}
/*
// debug
friend ostream& operator << (ostream& s, const Graph& G) {
s << endl; for (int i = 0; i < G.size(); ++i) { s << i << " : " << G.list[i] << endl; }return s;
}
*/
};
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) { }
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool issame(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y) {
x = root(x); y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) {
return -par[root(x)];
}
};
struct Euler {
vector<int> nextv, walk;
Euler() {}
void dfs(Graph<int> &G, int v) {
for (int &i = nextv[v]; i < (int)G[v].size(); ++i) {
auto &e = G[v][i];
if (e.cap == 0) continue;
--e.cap;
--G.redge(e).cap;
dfs(G, e.to);
}
walk.push_back(v);
}
vector<vector<int> > solve(Graph<int> &G) {
vector<vector<int> > res;
int V = (int)G.size();
UnionFind uf(V);
for (int v = 0; v < V; ++v) for (auto e : G[v]) uf.merge(e.from, e.to);
vector<vector<int> > comps(V);
for (int v = 0; v < V; ++v) {
int r = uf.root(v);
comps[r].push_back(v);
}
nextv.assign(V, 0);
for (int v = 0; v < V; ++v) {
if (comps[v].empty()) continue;
vector<int> odds;
for (auto v2 : comps[v]) if (G[v2].size() & 1) odds.push_back(v2);
if (odds.size() > 2) return vector<vector<int> >();
walk.clear();
int start = (!odds.empty() ? odds[0] : v);
dfs(G, start);
reverse(walk.begin(), walk.end());
res.push_back(walk);
}
return res;
}
};
//------------------------------//
// Examples
//------------------------------//
void solve(const vector<int> &a, const vector<int> &b) {
vector<int> vals;
for (int i = 0; i < a.size(); ++i) {
if (a[i] > b[i]) {
cout << -1 << endl;
return;
}
vals.push_back(a[i]);
vals.push_back(b[i]);
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
Graph<int> G((int)vals.size());
for (int i = 0; i < (int)a.size(); ++i) {
int ai = lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin();
int bi = lower_bound(vals.begin(), vals.end(), b[i]) - vals.begin();
G.add_undirected_edge(ai, bi, 1);
}
Euler el;
auto res = el.solve(G);
if (res.empty() || res.size() > 1) {
cout << -1 << endl;
return;
}
for (int i = 0; i < (int)res[0].size(); ++i) {
if (i) cout << " ";
cout << vals[res[0][i]];
}
cout << endl;
}
int main() {
int N;
while (cin >> N) {
vector<int> a(N-1), b(N-1);
for (int i = 0; i < N-1; ++i) cin >> a[i];
for (int i = 0; i < N-1; ++i) cin >> b[i];
solve(a, b);
}
}