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;
#define F first
#define S second
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 20050;
int n, m, val[N], val2[N];
bool vis[N];
int adjM[N][2];
vector<int> vc;
void dfs(int u) {
vis[u] = 1;
if (u>n) for (int v:adjM[u]) {
if (vis[v]) continue;
dfs(v);
}
vc.push_back(u);
}
void sfd(int u) {
val[u] = 0;
if (u>n) for (int v:adjM[u]) {
sfd(v);
}
}
void sfd2(int u) {
val2[u] = 0;
if (u>n) for (int v:adjM[u]) {
sfd2(v);
}
}
bool check(int u) {
for (int v = 1; v <= n+m; v++) val2[v] = val[v];
sfd2(u);
for (int v:vc) {
bool f1 = 0, f2 = 0;
if(v>n) for (int x:adjM[v]) {
f1 = f1||(val2[x] == 1);
f2 = f2||(val2[x]);
}
if (f1) val2[v] = 1;
else if (!f2 && v > n) val2[v] = 0;
if (val2[v] != 2 && val[v] != 2 && val[v] != val2[v]) return 0;
}
return 1;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) val[i] = 2;
for (int i = n+1; i <= n+m; i++) {
char c;
cin >> c;
val[i] = (c!='0')+(c=='?');
}
for (int v = n+1; v <= n+m; v++) {
char c1, c2;
int n1, n2;
cin >> c1>>n1 >> c2>>n2;
int u1 = (c1=='c')*n + n1;
int u2 = (c2=='c')*n + n2;
adjM[v][0] = u1;
adjM[v][1] = u2;
}
for (int u = 1; u <= n+m; u++) {
if (!vis[u]) dfs(u);
}
for (int u:vc) {
if (!val[u]) sfd(u);
}
for (int u:vc) {
if (!check(u)) val[u] = 1;
else if (u > n) {
bool flg = 0;
for (int v:adjM[u]) {
flg = flg||(val[v]);
}
if(!flg) val[u] = 0;
}
}
for (int i = 1; i <= m; i++) {
cout << (char)(val[n+i] < 2? '0'+val[n+i] :'?');
}
}
int main() {
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... |