#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)2e5 + 10;
const int M = (int)1e6 + 10;
set<int> empt;
int A[N][2];
bool vis[N][2];
vector<pii> pos[N];
struct elem{
int idx;
int up_pair;
int cycle;
bool operator< (const elem &ee) const {
if(cycle == ee.cycle)
return up_pair > ee.up_pair;
else
return cycle > ee.cycle;
}
};
struct chain{
vector<pii> C;
int up_pair;
bool cycle;
};
chain E[M];
int id;
vector<pii> trav;
int upper;
void dfs(int u, int v){
vis[u][v]=true;
trav.push_back(mp(u,v));
if(A[u][(v^1)] != 0 && !vis[u][(v^1)]){
dfs(u,(v^1));
return;
}
for(auto x : pos[A[u][v]]){
if(!vis[x.fi][x.se]){
if((v & x.se) == 1) upper ++ ; // upper pair
dfs(x.fi,x.se);
return;
}
}
}
vector<pii> oper;
priority_queue<elem> ee;
void solve_weak_chain(vector<pii> zz){
if(zz.empty()) return;
int l = 0;
int r = (int)zz.size() - 1;
while(l < r){
if(zz[l].se == 0 && zz[l + 1].se == 1){
oper.push_back(mp(zz[l + 1].fi, zz[l].fi)); // zz[l + 1].fi -> zz[l].fi
l += 2 ;
}
else if(zz[r].se == 0 && zz[r - 1].se == 1){
oper.push_back(mp(zz[r - 1].fi, zz[r].fi));
r -= 2 ;
}
else{
break;
}
}
oper.push_back(mp(zz[l].fi, zz[r].fi));
empt.insert(zz[l].fi);
}
int fin(){
if(empt.empty()) {
cout << "-1\n";
exit(0);
return -1;
}
int id = *empt.begin();
empt.erase(empt.begin());
return id;
}
void solve_chain(int id){
vector<pii> pp;
int chk;
for(int i = 0 ; i < E[id].C.size(); i ++ ){
if(i + 1 < E[id].C.size() && E[id].C[i].se == 1 && E[id].C[i + 1].se == 1){
chk = fin();
oper.push_back(mp(E[id].C[i].fi, chk));
oper.push_back(mp(E[id].C[i + 1].fi, chk));
solve_weak_chain(pp);
pp.clear();
i++;
continue;
}
else{
pp.push_back(E[id].C[i]);
}
}
solve_weak_chain(pp);
}
void solve_cycle(int id){
int j;
int chk;
vector<pii> tt;
for(int i = 0 ; i < E[id].C.size(); i ++ ){
j = (i + 1) % E[id].C.size();
if(E[id].C[i].se == 1 && E[id].C[j].se == 1){
chk = fin();
oper.push_back(mp(E[id].C[i].fi, chk));
oper.push_back(mp(E[id].C[j].fi, chk));
for(int add = 1; add + 1 < E[id].C.size(); add ++ ){
tt.push_back(E[id].C[(j + add) % E[id].C.size()]);
}
break;
}
}
pii nw;
if(tt.empty()){
for(int i = 0 ; i < E[id].C.size(); i ++ ){
j = (i + 1) % E[id].C.size();
if(E[id].C[i].se == 1 && E[id].C[j].se == 0){
chk = fin();
oper.push_back(mp(E[id].C[i].fi, chk));
nw = mp(chk, 0);
tt.push_back(nw);
for(int g = 0; g + 1 < E[id].C.size(); g ++ ){
tt.push_back(E[id].C[(j + g) % E[id].C.size()]);
}
break;
}
}
}
int tp = 0;
for(int i = 0 ; i + 1 < tt.size(); i ++ ){
if((tt[i].se & tt[i + 1].se) == 1){
tp ++ ;
}
}
E[id] = {tt, tp, false};
ee.push({id, tp, false});
id ++ ;
}
void solve(int id){
if(E[id].cycle) solve_cycle(id);
else solve_chain(id);
}
int main(){
fastIO;
int n, m;
cin >> n >> m;
int x, y;
for(int i = 1; i <= m; i ++ ){
cin >> x >> y;
if(x == 0 && y == 0) empt.insert(i);
else{
if(x==y) continue; // we dont look at et
A[i][0] = x;
A[i][1] = y;
for(int j = 0 ; j < 2; j ++ ){
if(A[i][j] != 0)pos[A[i][j]].push_back(mp(i,j));
}
}
}
for(int i = 1; i <= m; i ++ ){
if(A[i][0] != 0 && A[i][1] == 0 && !vis[i][0]){
// NO cycle
trav.clear();
upper = 0;
dfs(i,0);
E[id] = {trav, upper, false};
ee.push({id, upper, false});
id ++ ;
}
}
for(int i = 1; i <= m; i ++ ){
if(A[i][0] != 0 && !vis[i][0]){
// cycle?
trav.clear();
upper = 0;
dfs(i,0);
E[id] = {trav, upper, true};
ee.push({id, upper, true});
id ++ ;
}
}
elem U;
while(!ee.empty()){
U = ee.top();
ee.pop();
solve(U.idx);
}
cout << oper.size() << "\n";
for(auto xx : oper){
cout << xx.fi << " " << xx.se << "\n";
}
return 0;
}
Compilation message
Main.cpp: In function 'void solve_chain(int)':
Main.cpp:102:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i = 0 ; i < E[id].C.size(); i ++ ){
| ~~^~~~~~~~~~~~~~~~
Main.cpp:103:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | if(i + 1 < E[id].C.size() && E[id].C[i].se == 1 && E[id].C[i + 1].se == 1){
| ~~~~~~^~~~~~~~~~~~~~~~
Main.cpp: In function 'void solve_cycle(int)':
Main.cpp:123:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for(int i = 0 ; i < E[id].C.size(); i ++ ){
| ~~^~~~~~~~~~~~~~~~
Main.cpp:129:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for(int add = 1; add + 1 < E[id].C.size(); add ++ ){
| ~~~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:137:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(int i = 0 ; i < E[id].C.size(); i ++ ){
| ~~^~~~~~~~~~~~~~~~
Main.cpp:144:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for(int g = 0; g + 1 < E[id].C.size(); g ++ ){
| ~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:152:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | for(int i = 0 ; i + 1 < tt.size(); i ++ ){
| ~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
36336 KB |
Output is correct |
2 |
Correct |
19 ms |
36224 KB |
Output is correct |
3 |
Correct |
21 ms |
36308 KB |
Output is correct |
4 |
Correct |
18 ms |
36308 KB |
Output is correct |
5 |
Correct |
23 ms |
36212 KB |
Output is correct |
6 |
Correct |
17 ms |
36308 KB |
Output is correct |
7 |
Correct |
21 ms |
36324 KB |
Output is correct |
8 |
Correct |
22 ms |
36340 KB |
Output is correct |
9 |
Correct |
17 ms |
36272 KB |
Output is correct |
10 |
Correct |
18 ms |
36244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
50364 KB |
Output is correct |
2 |
Correct |
127 ms |
50764 KB |
Output is correct |
3 |
Correct |
100 ms |
49640 KB |
Output is correct |
4 |
Correct |
123 ms |
49580 KB |
Output is correct |
5 |
Correct |
109 ms |
51032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
36308 KB |
Output is correct |
2 |
Correct |
18 ms |
36216 KB |
Output is correct |
3 |
Correct |
18 ms |
36320 KB |
Output is correct |
4 |
Correct |
19 ms |
36332 KB |
Output is correct |
5 |
Correct |
23 ms |
36308 KB |
Output is correct |
6 |
Correct |
21 ms |
36344 KB |
Output is correct |
7 |
Correct |
18 ms |
36452 KB |
Output is correct |
8 |
Correct |
19 ms |
36444 KB |
Output is correct |
9 |
Correct |
22 ms |
36404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
36308 KB |
Output is correct |
2 |
Correct |
18 ms |
36216 KB |
Output is correct |
3 |
Correct |
18 ms |
36320 KB |
Output is correct |
4 |
Correct |
19 ms |
36332 KB |
Output is correct |
5 |
Correct |
23 ms |
36308 KB |
Output is correct |
6 |
Correct |
21 ms |
36344 KB |
Output is correct |
7 |
Correct |
18 ms |
36452 KB |
Output is correct |
8 |
Correct |
19 ms |
36444 KB |
Output is correct |
9 |
Correct |
22 ms |
36404 KB |
Output is correct |
10 |
Correct |
208 ms |
53080 KB |
Output is correct |
11 |
Correct |
52 ms |
36320 KB |
Output is correct |
12 |
Correct |
138 ms |
47312 KB |
Output is correct |
13 |
Correct |
137 ms |
51736 KB |
Output is correct |
14 |
Correct |
153 ms |
47688 KB |
Output is correct |
15 |
Correct |
130 ms |
47228 KB |
Output is correct |
16 |
Correct |
147 ms |
53012 KB |
Output is correct |
17 |
Correct |
117 ms |
48388 KB |
Output is correct |
18 |
Correct |
141 ms |
52468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
36436 KB |
Output is correct |
2 |
Correct |
19 ms |
36340 KB |
Output is correct |
3 |
Correct |
20 ms |
36304 KB |
Output is correct |
4 |
Correct |
21 ms |
36380 KB |
Output is correct |
5 |
Correct |
16 ms |
36308 KB |
Output is correct |
6 |
Correct |
17 ms |
36308 KB |
Output is correct |
7 |
Correct |
21 ms |
36408 KB |
Output is correct |
8 |
Correct |
22 ms |
36440 KB |
Output is correct |
9 |
Correct |
18 ms |
36436 KB |
Output is correct |
10 |
Correct |
19 ms |
36392 KB |
Output is correct |
11 |
Correct |
17 ms |
36372 KB |
Output is correct |
12 |
Correct |
18 ms |
36420 KB |
Output is correct |
13 |
Correct |
17 ms |
36308 KB |
Output is correct |
14 |
Correct |
17 ms |
36348 KB |
Output is correct |
15 |
Correct |
19 ms |
36308 KB |
Output is correct |
16 |
Correct |
21 ms |
36436 KB |
Output is correct |
17 |
Correct |
17 ms |
36352 KB |
Output is correct |
18 |
Correct |
17 ms |
36384 KB |
Output is correct |
19 |
Correct |
17 ms |
36436 KB |
Output is correct |
20 |
Correct |
23 ms |
36368 KB |
Output is correct |
21 |
Correct |
21 ms |
36288 KB |
Output is correct |
22 |
Correct |
18 ms |
36436 KB |
Output is correct |
23 |
Correct |
23 ms |
36408 KB |
Output is correct |
24 |
Correct |
18 ms |
36504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
36336 KB |
Output is correct |
2 |
Correct |
19 ms |
36224 KB |
Output is correct |
3 |
Correct |
21 ms |
36308 KB |
Output is correct |
4 |
Correct |
18 ms |
36308 KB |
Output is correct |
5 |
Correct |
23 ms |
36212 KB |
Output is correct |
6 |
Correct |
17 ms |
36308 KB |
Output is correct |
7 |
Correct |
21 ms |
36324 KB |
Output is correct |
8 |
Correct |
22 ms |
36340 KB |
Output is correct |
9 |
Correct |
17 ms |
36272 KB |
Output is correct |
10 |
Correct |
18 ms |
36244 KB |
Output is correct |
11 |
Correct |
134 ms |
50364 KB |
Output is correct |
12 |
Correct |
127 ms |
50764 KB |
Output is correct |
13 |
Correct |
100 ms |
49640 KB |
Output is correct |
14 |
Correct |
123 ms |
49580 KB |
Output is correct |
15 |
Correct |
109 ms |
51032 KB |
Output is correct |
16 |
Correct |
18 ms |
36308 KB |
Output is correct |
17 |
Correct |
18 ms |
36216 KB |
Output is correct |
18 |
Correct |
18 ms |
36320 KB |
Output is correct |
19 |
Correct |
19 ms |
36332 KB |
Output is correct |
20 |
Correct |
23 ms |
36308 KB |
Output is correct |
21 |
Correct |
21 ms |
36344 KB |
Output is correct |
22 |
Correct |
18 ms |
36452 KB |
Output is correct |
23 |
Correct |
19 ms |
36444 KB |
Output is correct |
24 |
Correct |
22 ms |
36404 KB |
Output is correct |
25 |
Correct |
208 ms |
53080 KB |
Output is correct |
26 |
Correct |
52 ms |
36320 KB |
Output is correct |
27 |
Correct |
138 ms |
47312 KB |
Output is correct |
28 |
Correct |
137 ms |
51736 KB |
Output is correct |
29 |
Correct |
153 ms |
47688 KB |
Output is correct |
30 |
Correct |
130 ms |
47228 KB |
Output is correct |
31 |
Correct |
147 ms |
53012 KB |
Output is correct |
32 |
Correct |
117 ms |
48388 KB |
Output is correct |
33 |
Correct |
141 ms |
52468 KB |
Output is correct |
34 |
Correct |
21 ms |
36436 KB |
Output is correct |
35 |
Correct |
19 ms |
36340 KB |
Output is correct |
36 |
Correct |
20 ms |
36304 KB |
Output is correct |
37 |
Correct |
21 ms |
36380 KB |
Output is correct |
38 |
Correct |
16 ms |
36308 KB |
Output is correct |
39 |
Correct |
17 ms |
36308 KB |
Output is correct |
40 |
Correct |
21 ms |
36408 KB |
Output is correct |
41 |
Correct |
22 ms |
36440 KB |
Output is correct |
42 |
Correct |
18 ms |
36436 KB |
Output is correct |
43 |
Correct |
19 ms |
36392 KB |
Output is correct |
44 |
Correct |
17 ms |
36372 KB |
Output is correct |
45 |
Correct |
18 ms |
36420 KB |
Output is correct |
46 |
Correct |
17 ms |
36308 KB |
Output is correct |
47 |
Correct |
17 ms |
36348 KB |
Output is correct |
48 |
Correct |
19 ms |
36308 KB |
Output is correct |
49 |
Correct |
21 ms |
36436 KB |
Output is correct |
50 |
Correct |
17 ms |
36352 KB |
Output is correct |
51 |
Correct |
17 ms |
36384 KB |
Output is correct |
52 |
Correct |
17 ms |
36436 KB |
Output is correct |
53 |
Correct |
23 ms |
36368 KB |
Output is correct |
54 |
Correct |
21 ms |
36288 KB |
Output is correct |
55 |
Correct |
18 ms |
36436 KB |
Output is correct |
56 |
Correct |
23 ms |
36408 KB |
Output is correct |
57 |
Correct |
18 ms |
36504 KB |
Output is correct |
58 |
Correct |
160 ms |
52280 KB |
Output is correct |
59 |
Correct |
158 ms |
52568 KB |
Output is correct |
60 |
Correct |
148 ms |
51920 KB |
Output is correct |
61 |
Correct |
137 ms |
51976 KB |
Output is correct |
62 |
Correct |
61 ms |
36288 KB |
Output is correct |
63 |
Correct |
119 ms |
51256 KB |
Output is correct |
64 |
Correct |
102 ms |
47600 KB |
Output is correct |
65 |
Correct |
191 ms |
52152 KB |
Output is correct |
66 |
Correct |
219 ms |
52752 KB |
Output is correct |
67 |
Correct |
116 ms |
47140 KB |
Output is correct |
68 |
Correct |
146 ms |
51908 KB |
Output is correct |
69 |
Correct |
162 ms |
51412 KB |
Output is correct |
70 |
Correct |
138 ms |
47180 KB |
Output is correct |
71 |
Correct |
97 ms |
47492 KB |
Output is correct |
72 |
Correct |
93 ms |
47312 KB |
Output is correct |
73 |
Correct |
207 ms |
52960 KB |
Output is correct |
74 |
Correct |
106 ms |
47208 KB |
Output is correct |
75 |
Correct |
162 ms |
52480 KB |
Output is correct |
76 |
Correct |
188 ms |
52864 KB |
Output is correct |
77 |
Correct |
175 ms |
52316 KB |
Output is correct |
78 |
Correct |
132 ms |
47360 KB |
Output is correct |
79 |
Correct |
155 ms |
52276 KB |
Output is correct |
80 |
Correct |
155 ms |
48400 KB |
Output is correct |
81 |
Correct |
167 ms |
52260 KB |
Output is correct |