This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N = 200010;
vector <int> v[N];
int p[N];
int mark[N];
vector <int> g[N];
int mark2[N];
int matching[N];
int off[N];
int mark3[N];
bool kuhn(int v){
if(mark2[v]){
return false;
}
mark2[v] = true;
for(auto x : g[v]){
if(off[x]) continue;
if(matching[x] == -1){
matching[x] = v;
return true;
}
if(kuhn(matching[x])){
matching[x] = v;
return true;
}
}
return false;
}
int main(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> p[i];
v[i].push_back(p[i]);
}
for(int i = 2;i <= n;i++){
set <int> s;
for(auto x : v[i]){
s.insert(x);
}
for(auto x : v[i/2]){
s.insert(x);
}
v[i/2].clear();
v[i].clear();
for(auto x : s){
v[i/2].push_back(x);
v[i].push_back(x);
}
}
for(int i = 1;i <= n;i++){
for(auto x : v[i]){
g[x+n].push_back(i);
g[i].push_back(x+n);
}
}
for(int i = 1;i <= 2*n;i++){
sort(g[i].begin(), g[i].end());
}
vector <pair <int, int>> res;
if(n > 1e3){
for(int i = 1;i <= n-2e3;i++){
for(auto x : g[i]){
if(off[x]) continue;
off[i] = 1;
off[x] = 1;
res.push_back({i, x});
}
}
}
for(int i = 1;i <= n;i++){
if(off[i]) continue;
for(auto x : g[i]){
if(off[x]) continue;
off[i] = 1;
off[x] = 1;
memset(matching, -1, sizeof matching);
memset(mark3, 0, sizeof mark3);
for(int i = 1;i <= n;i++){
if(off[i]) continue;
vector <int> idx;
for(auto x : g[i]){
if(off[x]) continue;
if(matching[x] == -1){
matching[x] = i;
mark3[i] = 1;
break;
}
}
}
for(int i = 1;i <= n;i++){
if(off[i]) continue;
if(mark3[i]) continue;
memset(mark2, 0, sizeof mark2);
kuhn(i);
}
bool aux = true;
for(int i = n+1;i <= 2*n;i++){
if(off[i]) continue;
if(matching[i] == -1) aux = false;
}
if(aux){
res.push_back({i, x});
break;
}
off[i] = 0;
off[x] = 0;
}
}
for(auto x : res){
cout << x.second-n << ' ';
}
cout << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |