#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
const int mxn = 2e5+10;
int arr[mxn][2];
vector<int> pos[mxn];
set<int> spare;
int N,M;
priority_queue<pii,vector<pii>,greater<pii>> pq;
bitset<mxn> done;
vector<pii> ans;
void add(int col){
if(!col)return;
assert(pos[col].size() == 2);
int p1 = pos[col][0],p2 = pos[col][1];
if(p1&1)swap(p1,p2);
if((p1&1)&&(p2&1))pq.push(pii(3,col));
else if(!(p1&1)&&(p2&1)){
if((arr[p1>>1][1]))pq.push(pii(2,col));
else pq.push(pii(0,col));
}
else if(!(p1&1)&&!(p2&1)){
if(arr[p1>>1][1]&&arr[p2>>1][1])pq.push(pii(4,col));
else if(arr[p1>>1][1]||arr[p2>>1][1])pq.push(pii(1,col));
else pq.push(pii(0,col));
}
else assert(false);
}
void mv(int a,int b){
//cerr<<"MV: "<<a<<' '<<b<<endl;
ans.push_back(pii(a,b));
assert(spare.find(a) == spare.end());
assert(arr[a][0]&&!arr[b][1]);
int pa = (a<<1)|(arr[a][1]?1:0);
int pb = (b<<1)|(!arr[b][0]?0:1);
int col = arr[pa>>1][pa&1];
assert(!(pb&1)||arr[pa>>1][pa&1] == arr[pb>>1][0]);
swap(arr[pb>>1][pb&1],arr[pa>>1][pa&1]);
pos[col].erase(find(pos[col].begin(),pos[col].end(),pa));
pos[col].push_back(pb);
assert(pos[col].size() == 2);
if(!arr[pa>>1][0])spare.insert(pa>>1);
add(arr[pa>>1][0]);
add(col);
add(arr[pb>>1][0]);
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M;
for(int i = 1;i<=M;i++){
for(int j = 0;j<2;j++){
cin>>arr[i][j];
if(arr[i][j])pos[arr[i][j]].push_back((i<<1)|j);
}
if(!arr[i][0])spare.insert(i);
}
for(int i = 1;i<=N;i++){
assert(pos[i].size() == 2);
if((pos[i][0]^pos[i][1]) == 1)done[i] = true;
else{
add(i);
}
}
while(!pq.empty()){
auto [tp,col] = pq.top();pq.pop();
if(done[col])continue;
assert(pos[col].size() == 2);
int p1 = pos[col][0],p2 = pos[col][1];
if(p1&1)swap(p1,p2);
//cerr<<tp<<' '<<col<<':'<<p1<<' '<<p2<<endl;
if(tp == 4||tp == 3||tp == 2){
if(spare.empty()){
cout<<"-1\n";
return 0;
}
auto emp = *spare.begin();
spare.erase(emp);
assert(!arr[emp][0]);
mv(p1>>1,emp);
}
else if(tp == 1){
if(arr[p1>>1][1])swap(p1,p2);
if(spare.empty()){
cout<<"-1\n";
return 0;
}
auto emp = *spare.begin();
spare.erase(emp);
assert(!arr[emp][0]);
mv(p2>>1,emp);
}
else if(tp == 0){
mv(p2>>1,p1>>1);
done[col] = true;
}
}
//cerr<<"ANS: "<<endl;
for(int i = 1;i<=M;i++){
//cerr<<arr[i][0]<<' '<<arr[i][1]<<endl;
}
for(int i = 1;i<=M;i++)assert(arr[i][0] == arr[i][1]);
//cerr<<endl<<endl;
cout<<ans.size()<<'\n';
for(auto [a,b]:ans)cout<<a<<' '<<b<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4952 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4952 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4952 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4952 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
104 ms |
17928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4952 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4952 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4952 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4952 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Incorrect |
104 ms |
17928 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |