#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
const int MAX = 2e5 + 10;
int n, m;
int x, y;
int arr[MAX][2];
vector <int> boja[MAX];
vector <int> prazno;
vector <pii> sol;
int veze[MAX];
int bio[MAX];
vector <pii> v;
void rek(int pos) {
if (!veze[pos]) return;
if (bio[veze[pos]]) return;
bio[veze[pos]] = 1;
rek(veze[pos]);
}
void rek2(int pos) {
if (!veze[pos]) return;
v.push_back({pos, veze[pos]});
rek2(veze[pos]);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
arr[i][0] = x;
arr[i][1] = y;
if (x == 0) prazno.push_back(i);
boja[x].push_back(i);
boja[y].push_back(i);
}
for (int i = 1; i <= n; i++) {
x = boja[i][0];
y = boja[i][1];
if (x == y) continue;
if (arr[x][1] == i && arr[y][1] == i) {
if (prazno.empty()) {cout << "-1\n"; return 0;}
arr[prazno.back()][0] = i;
arr[prazno.back()][1] = i;
arr[x][1] = 0;
arr[y][1] = 0;
sol.push_back({x, prazno.back()});
sol.push_back({y, prazno.back()});
prazno.pop_back();
}
}
for (int i = 1; i <= n; i++) {
x = boja[i][0];
y = boja[i][1];
if (x == y) continue;
if (arr[x][1] == i && arr[y][0] == i) veze[x] = y;
if (arr[x][0] == i && arr[y][1] == i) veze[y] = x;
}
for (int i = 1; i <= n; i++) {
if (bio[i]) continue;
if (!veze[i]) continue;
rek(i);
if (bio[i]) {
if (prazno.empty()) {cout << "-1\n"; return 0;}
arr[prazno.back()][0] = i;
arr[i][2] = 0;
sol.push_back({i, prazno.back()});
prazno.pop_back();
}
}
memset(veze, 0, sizeof(veze));
for (int i = 1; i <= n; i++) {
x = boja[i][0];
y = boja[i][1];
if (x == y) continue;
if (arr[x][1] == i && arr[y][0] == i) veze[x] = y;
if (arr[x][0] == i && arr[y][1] == i) veze[y] = x;
}
for (int i = 1; i <= n; i++) {
if (bio[i]) continue;
if (!veze[i]) continue;
v.clear();
rek2(i);
reverse(v.begin(), v.end());
for (auto e : v) {
x = e.fi;
y = e.se;
arr[y][1] = arr[x][1];
arr[x][1] = 0;
sol.push_back({x, y});
}
}
for (int i = 1; i <= m; i++) boja[i].clear();
for (int i = 1; i <= m; i++) {
x = arr[i][0];
y = arr[i][1];
boja[x].push_back(i);
boja[y].push_back(i);
}
for (int i = 1; i <= n; i++) {
x = boja[i][0];
y = boja[i][1];
if (x == y) continue;
if (arr[x][0] == i && arr[y][0] == i) {
arr[y][1] = i;
arr[x][0] = 0;
sol.push_back({x, y});
}
}
cout << sol.size() << "\n";
for (auto e : sol) {
cout << e.fi << " " << e.se << "\n";
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:93:21: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
93 | arr[i][2] = 0;
| ~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
3 ms |
6848 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
86 ms |
16492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
3 ms |
6848 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |