Submission #768771

#TimeUsernameProblemLanguageResultExecution timeMemory
768771MetalPowerDijamant (COI16_dijament)C++14
100 / 100
260 ms4584 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define fi first
#define se second

const int MX = 1e3 + 10;
const int INF = 1e9 + 7;

int N, tim = 0; int src[MX]; bool f = true;
map<string, int> mp;
vector<int> adj[MX];

void dfs(int u){
	for(int nx : adj[u]){
		if(src[nx] == 0){
			src[nx] = u;
			dfs(nx);
		}else if(src[nx] != u){
			f = false;
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> N;
	for(int x = 0; x < N; x++){

		string s, tmp; vector<string> nx;
		cin >> s >> tmp >> tmp;

		for(; tmp != ";"; ){
			nx.push_back(tmp);
			cin >> tmp;
		}

		f = true;

		if(f) { // First rule #1 : s must not be declared before
			if(mp[s] != 0) f = false;
		}

		if(f) { // Second rule #2 : all in nx must already be declared before
			for(string st : nx){
				if(mp[st] == 0){
					f = false;
					break;
				}
			}
		}

		if(f) { // Third rule #3 : no diamonds
			for(int i = 0; i <= tim; i++) src[i] = 0;
			for(string st : nx){
				if(f) 
					dfs(mp[st]);
			}
		}

		if(f) {
			mp[s] = ++tim;
			for(string st : nx){ // if the node is in a line only enter the last one
				if(src[mp[st]] == 0) adj[tim].push_back(mp[st]);
			}
		}

		if(f) cout << "ok\n";
		else cout << "greska\n";
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...