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>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
#define ull unsigned long long
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 19972207, pr = 31;
const int mxn = 2e4 + 5, mxq = 1e5 + 5, sq = 500, mxv = 10005;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
int n, m;
bool vis[20005];
bitset<mxv>reach[10005], one;
bool can0[mxn + 1], can1[mxn + 1];
int lson[mxn + 1], rson[mxn + 1];
vt<int>adj[mxn + 1];
int id(string s){
if(s[0] == 'x')return(s[1] - '0');
else return(s[1] - '0' + n);
}
void dfs(int s){
vis[s] = 1;
if(s > n){
if(!vis[lson[s]])dfs(lson[s]);
if(!vis[rson[s]])dfs(rson[s]);
}
}
void dfsreach(int s, int root){
if(s <= n)reach[root][s] = 1;
else{
dfsreach(lson[s], root);
dfsreach(rson[s], root);
}
}
void dfs2(int s){
vis[s] = 1;
for(auto i: adj[s]){
if(!vis[i]){
dfs2(i);
}
}
}
void solve(){
cin >> n >> m;
string s; cin >> s; s = '.' + s;
for(int i = n + 1; i <= n + m; i++){
string s1, s2; cin >> s1 >> s2;
lson[i] = id(s1); rson[i] = id(s2);
adj[lson[i]].pb(i); adj[rson[i]].pb(i);
}
for(int i = n + m; i >= n + 1; i--){
if(!vis[i] && s[i - n] == '0'){
dfs(i);
}
}
for(int i = 1; i <= n; i++){
if(!vis[i])one[i] = 1;
}
for(int i = 1; i <= m; i++){
dfsreach(i + n, i);
}
for(int i = 1; i <= m; i++){
if((one & reach[i]).count()){
can1[i] = 1; //can still be zero
}
else{
can0[i] = 1; //certain is 0;
}
}
for(int i = 1; i <= m; i++){
if(s[i] != '?')cout << s[i];
else if(can0[i] == 1){
cout << 0;
}else{
memset(vis, 0, sizeof(vis));
bool bad = 0;
for(int j = 1; j <= n; j++){
if(one[j] == 1 && !reach[i][j]){
dfs2(j);
}
}
for(int j = 1; j <= m; j++){
if(!vis[j + n] && s[j] == '1'){
bad = 1;
break;
}
}
if(bad)cout << 1;
else cout << '?';
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("i.inp", "r", stdin);
//freopen("i.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
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... |