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>
#define int long long
using namespace std;
const int N = 3e5 + 1;
vector<int> g[N], Kn;
int p[N], res[N], out[N], in[N], n, m, k;
int _find(int u) {
return (p[u] < 0 ? u : p[u] = _find(p[u]));
}
void _union(int u, int v) {
if (u == v) return;
if (p[u] > p[v]) swap(u, v);
p[u] += p[v]; p[v] = u;
}
void dfs1(int u) {
if (out[u]) return;
out[u] = 1;
for (int v : g[u]) {
dfs1(v);
out[u] = max(out[u], out[v] + 1);
}
}
void dfs2(int u) {
if (res[u]) return;
res[u] = out[u];
for (int v : g[u])
if (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;
u = _find(u);
v = _find(v);
if (c == '=') _union(u, v);
else if (c == '>') g[u].push_back(v), in[v]++;
else g[v].push_back(u), in[u]++;
}
for (int i = 1; i <= m; i++)
if (i == _find(i) && !in[i])
dfs1(i);
for (int i = 1; i <= m; i++)
if (out[i] == n)
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... |