File tree Expand file tree Collapse file tree 1 file changed +10
-11
lines changed Expand file tree Collapse file tree 1 file changed +10
-11
lines changed Original file line number Diff line number Diff line change @@ -98,17 +98,8 @@ int main() {
98
98
ios::sync_with_stdio(0);
99
99
cin.tie(0);
100
100
int n, m;
101
- cin >> n >> m;
102
- edges.clear();
103
- g.assign(n, vector<int > ());
104
- used.assign(m + 1, false);
105
- res.clear();
106
- for (int i = 0; i < m; i++) {
107
- int u, v;
108
- cin >> u >> v;
109
- u--, v--;
110
- add_edge(u, v);
111
- }
101
+ // reading the graph
102
+
112
103
int v1 = -1, v2 = -1;
113
104
bool bad = false;
114
105
for (int i = 0; i < n; i++) {
@@ -122,18 +113,24 @@ int main() {
122
113
}
123
114
}
124
115
}
116
+
125
117
int first = 0;
126
118
while (first < n && g[first].empty()) {
127
119
first++;
128
120
}
121
+
129
122
if (bad || (v1 != -1 && v2 == -1) || first == n) {
130
123
cout << -1 << '\n';
131
124
return 0;
132
125
}
126
+
133
127
if (v1 != -1) {
134
128
add_edge(v1, v2);
135
129
}
130
+
131
+ used.assign((int) edges.size(), false);
136
132
dfs(first);
133
+
137
134
if (v1 != -1) {
138
135
for (int i = 0; i + 1 < (int) res.size(); i++) {
139
136
if ((res[i] == v1 && res[i + 1] == v2) ||
@@ -150,12 +147,14 @@ int main() {
150
147
}
151
148
}
152
149
}
150
+
153
151
for (int i = 0; i < n; i++) {
154
152
if (!g[i].empty()) {
155
153
cout << -1 << '\n';
156
154
return 0;
157
155
}
158
156
}
157
+
159
158
for (int x : res) {
160
159
cout << x + 1 << ' ';
161
160
}
You can’t perform that action at this time.
0 commit comments