Submission #970582

#TimeUsernameProblemLanguageResultExecution timeMemory
970582jcelinIli (COI17_ili)C++14
100 / 100
476 ms1464 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<pll> vpll;

#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define all(x) (x).begin(), (x).end()

const int mod = 1e9 + 7; //998244353;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int logo = 20;
const int MAXN = 2e4 + 7;
const int off = 1 << logo;
const int trsz = off << 1;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int ans[MAXN], n, m;
vi g[MAXN];
bool bio[MAXN], in[MAXN];

void dfs(int u){
	if(bio[u]) return;
	bio[u] = 1;
	if(u < n) return;
	for(auto &x : g[u]) dfs(x);
}

void solve(){
	cin >> n >> m;
	for(int i=0; i<n+m; i++) ans[i] = 2;
	for(int i=n; i<n+m; i++){
		char x;
		cin >> x;
		if(x == '1') ans[i] = 1;
		if(x == '0') ans[i] = 0;
	}
	
	for(int i=n; i<n+m; i++){
		for(int j=0; j<2; j++){
			char x;
			cin >> x;
			int vl;
			cin >> vl;
			vl--;
			if(x == 'c') vl += n;
			g[i].PB(vl);
		}
	}
	
	for(int i=n; i<n+m; i++) if(ans[i] == 0){
		fill(bio, bio + n + m, 0);
		dfs(i);
		for(int j=0; j<n+m; j++) if(bio[j]) ans[j] = 0;
	}
	
	for(int i=n; i<n+m; i++){
		int su = 0;
		for(auto &x : g[i]) su += ans[x];
		if(su == 0) ans[i] = 0;
	}
	
	for(int i=n; i<n+m; i++){
		if(ans[i] != 2) continue;
		fill(bio, bio + n + m, 0);
		fill(in, in + n + m, 0);
		dfs(i);
		for(int j=0; j<n; j++) if(ans[j] == 0 or bio[j]) in[j] = 1;
		for(int j=n; j<n+m; j++){
			in[j] = 1;
			for(auto &x : g[j]) in[j] &= in[x];
		}
		
		for(int j=0; j<n+m; j++) if(in[j] and ans[j] == 1) ans[i] = 1;
	}
	
	for(int i=n; i<n+m; i++){
		if(ans[i] == 2) cout << "?";
		else cout << ans[i];
	}
	cout << "\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...