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];
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);
vector<array<int, 2>> less;
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 less.push_back({ u, v });
}
for (auto [u, v] : less) {
u = _find(u);
v = _find(v);
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";
}
Compilation message (stderr)
kovanice.cpp: In function 'int main()':
kovanice.cpp:52:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for (auto [u, v] : less) {
| ^
# | 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... |