#include <bits/stdc++.h>
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
vector<pair<pair<int,int>,int>> edge;
vector<vector<vector<int>>> memo;
vector<vector<vector<int>>> place;
int l=0;
int l2=0;
int n,m;
bool dp(int i, int r, int r2){
if(memo[i][r][r2]>-1) return memo[i][r][r2];
if(i==m){
if(r-l>=n-1&&r2-l2>=n-1) return 1;
return 0;
}
bool tk=false;
bool ntk=false;
if(edge[i].first.first-1<=r){
tk=dp(i+1,max(edge[i].first.second,r),r2);
}
if(edge[i].first.first-1<=r2){
ntk=dp(i+1,r,max(edge[i].first.second,r2));
}
if(tk) place[i][r][r2]=0;
else if(ntk) place[i][r][r2]=1;
return (memo[i][r][r2]=max(tk,ntk));
}
vector<int> out;
void prnt(int i, int r, int r2){
if(i>=m) return;
out[edge[i].second]=place[i][r][r2];
if(place[i][r][r2]==0){
prnt(i+1,max(edge[i].first.second,r),r2);
}else{
prnt(i+1,r,max(edge[i].first.second,r2));
}
return;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
edge.resize(m);
out.resize(m);
memo.resize(m+1,vector<vector<int>>(n*2,vector<int>(n*2,-1)));
place.resize(m+1,vector<vector<int>>(n*2,vector<int>(n*2,-1)));
for (int i = 0; i < m; i++)
{
int a,b; cin >> a >> b; a--; b--;
if(b<a) b+=n;
edge[i]={{a,b},i};
}
sort(all(edge));
l=edge[0].first.first;
int r=edge[0].first.second;
for (int i = 1; i < m; i++)
{
if(edge[i-1].first.first-1<=r) r=max(edge[i-1].first.second,r);
l2=edge[i].first.first;
if(dp(i+1,r,edge[i].first.second)){
for (int j = 0; j < i; j++) out[edge[j].second]=0;
out[edge[i].second]=1;
prnt(i+1,r,edge[i].first.second);
for (int j = 0; j < m; j++) cout << out[j];
cout << "\n";
return 0;
}
for (int j = 0; j <= m; j++)
{
for (int k = 0; k < 2*n; k++)
{
for (int _l = 0; _l < 2*n; _l++)
{
memo[j][k][_l]=-1;
place[j][k][_l]=-1;
}
}
}
}
cout << "impossible\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... |