This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author - alimammadzade
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1;
vector<int> g[N][2], Kn;
int p[N], res[N], out[N], n, m, k;
bool vis[N];
int _find(int u) {
return (p[u] < 0 ? u : p[u] = _find(p[u]));
}
void _union(int u, int v) {
u = _find(u), v = _find(v);
if (u == v) return;
if (p[u] > p[v]) swap(u, v);
p[u] += p[v]; p[v] = u;
}
void dfs1(int u) {
vis[u] = 1;
for (int v : g[u][0])
if (!vis[v]) {
dfs1(v);
out[u] = max(out[u], out[v] + 1);
}
}
void dfs2(int u) {
vis[u] = 1;
res[u] = out[u];
for (int v : g[u][0])
if (!vis[v] && out[v] == out[u] - 1)
dfs2(v);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
// system("cls"), freopen("in.txt", "r", stdin);
fill(p, p + N, -1);
fill(out, out + N, 1);
cin >> n >> m >> k;
while (k--) {
int u, v;
char c;
cin >> u >> c >> v;
if (c == '=') _union(u, v);
else if (c == '>') g[_find(u)][0].push_back(_find(v)), g[_find(v)][1].push_back(_find(u));
else g[_find(v)][0].push_back(_find(u)), g[_find(u)][1].push_back(_find(v));
}
// for (int i = 1; i <= m; i++) cout << _find(i) << ' ';
// cout << '\n';
for (int i = 1; i <= m; i++)
if (i == _find(i) && g[i][1].empty()) {
if (!vis[i]) dfs1(i);
if (out[i] == n) Kn.push_back(i);
}
fill(vis, vis + N, false);
for (int i : Kn)
dfs2(i);
for (int i = 1; i <= m; i++)
if (res[_find(i)]) cout << 'K' << res[_find(i)] << '\n';
else cout << "?\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |