Submission #1317919

#TimeUsernameProblemLanguageResultExecution timeMemory
1317919noobsolver24KOVANICE (COI15_kovanice)C++20
60 / 100
286 ms31608 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

const int sz = 3e5 + 5, inf = 1e18, mod = 1e9 + 7, lg = 23;

int a[sz], b[sz], in[sz];
int ans[sz];
char op[sz];
vector<int> g[sz];

struct DSU {
	int n;
	vector<int> par;
	void init(int _n) {
		n = _n;
		par.assign(n + 1, -1);
	}
	int find(int a){
		if (par[a] < 0) return a;
		return par[a] = find(par[a]);
	}
	bool unite(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b) return 0;
		if (par[a] < par[b]) swap(a, b);
		par[b] += par[a];
		par[a] = b;
		return 1;
	}
};

void _(){
    int n, m, v;
    cin >> n >> m >> v;
    DSU dsu;
    dsu.init(m);
	vector<pair<int, int>> ed;
    for (int i = 1; i <= v; i++) {
        cin >> a[i] >> op[i] >> b[i];
        if (op[i] == '=') {
			dsu.unite(a[i], b[i]);
		} else {
			ed.push_back({a[i], b[i]});
		}
    }
    for (auto [x, y] : ed) {
		x = dsu.find(x);
		y = dsu.find(y);
		if (x == y) continue;
		g[x].push_back(y);
		in[y]++;
	}
	vector<int> roots;
	for (int i = 1; i <= m; i++) {
		if (dsu.find(i) == i) {
			roots.push_back(i);
		}
	}
	queue<int> q;
	for (auto x : roots) {
		if (!in[x]) {
			if (g[x].size()) {
				ans[x] = 1;
				q.push(x);
			} else ans[x] = -1;
		}
	}
	while (q.size()) {
		int cur = q.front();
		q.pop();
		for (int to : g[cur]) {
			ans[to] = max(ans[to], ans[cur] + 1);
			if (--in[to] == 0) {
				q.push(to);
			}
		}
	}
	for (auto x : roots) {
		if (in[x] > 0) ans[x] = -1;
	}
	for (int i = 1; i <= m; i++) {
		ans[i] = ans[dsu.find(i)];
		if (ans[i] == -1 or ans[i] == 0) cout << "?" << endl;
		else cout << "K" << ans[i] << endl;
	}
}

signed main(){
    cin.tie(nullptr)->sync_with_stdio(0);
    int T = 1;
    // cin >> T;    
    while (T--) _();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...