[AGC056C] 01 Balanced
Genius_Star · · 题解
好题。
思路:
首先将
令
考虑差分约束,我们建边
除了上面的条件,我们还需要满足
在此基础上,字典序最小,跑 01BFS 求最短路即可;注意这里因为我们是
时间复杂度为
完整代码:
#include<bits/stdc++.h>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
bool Begin;
const int N = 1e6 + 10;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
int n, m, l, r;
int dis[N];
vector<pair<int, int>> E[N];
deque<int> q;
inline void add(int u, int v, int w){
E[u].push_back({v, w});
E[v].push_back({u, w});
}
bool End;
int main(){
// freopen("medium.in", "r", stdin);
// freopen("medium.out", "w", stdout);
n = read(), m = read();
for(int i = 1; i <= n; ++i){
dis[i] = -1;
add(i, i - 1, 1);
}
while(m--){
l = read(), r = read();
add(l - 1, r, 0);
}
q.push_back(0);
while(!q.empty()){
int u = q.front();
q.pop_front();
for(auto t : E[u]){
int v = t.fi, w = t.se;
if(~dis[v])
continue;
dis[v] = dis[u] + w;
if(!w)
q.push_front(v);
else
q.push_back(v);
}
}
for(int i = n; i >= 1; --i)
dis[i] -= dis[i - 1];
for(int i = 1; i <= n; ++i)
putchar(dis[i] > 0 ? '0' : '1');
//cerr << '\n' << abs(&Begin - &End) / 1048576 << "MB";
return 0;
}