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: longvu
* created: 10/28/22 21:23:23
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
const int INF = numeric_limits<int>::max();
const int nax = (int)(390001);
const int mod = 1e9 + 7;
template<class X, class Y>
bool maximize(X& x, const Y y) {
if (y > x) {x = y; return true;}
return false;
}
template<class X, class Y>
bool minimize(X& x, const Y y) {
if (y < x) {x = y; return true;}
return false;
}
struct DSU {
int pa[nax], color[nax];
DSU(int n) {
for (int i = 1; i <= n; ++i) {
pa[i] = i;
color[i] = 0;
}
}
int find(int u) {
return pa[u] = ((u != pa[u]) ? find(pa[u]) : pa[u]);
}
bool join(int u, int v) {
u = find(u), v = find(v);
if (u == v) return false;
if (v < u) {
swap(u, v);
}
pa[v] = u;
return true;
}
};
int U[nax], T[nax], V[nax], vis[nax], ans[nax];
vector<int> adj[nax];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
string s;
cin >> s;
int j = 0;
while (s[j] >= '0' && s[j] <= '9') {
U[i] = 10 * U[i] + (s[j] - '0');
j++;
}
if (s[j] == '<') {
T[i] = 0;
}
if (s[j] == '>') {
T[i] = 1;
}
if (s[j] == '=') {
T[i] = 2;
}
j++;
while (j < sz(s)) {
V[i] = 10 * V[i] + (s[j] - '0');
j++;
}
}
for (int i = 1; i <= k; ++i) {
// cout << U[i] << " " << V[i] << " " << T[i] << '\n';
if (!T[i]) {
adj[U[i]].push_back(V[i]);
}
if (T[i] == 1) {
adj[V[i]].push_back(U[i]);
}
}
function<bool(int, int)> dfs = [&](int u, int d) {
vis[u] = 1;
if (d == n) {
ans[u] = d;
return true;
}
bool ok = 0;
for (auto v : adj[u]) {
if (!vis[v]) {
ok |= dfs(v, d + 1);
}
}
if (ok) {
ans[u] = d;
}
return ok;
};
for (int i = 1; i <= m; ++i) {
if (!vis[i]) {
dfs(i, 1);
}
}
for (int i = 1; i <= k; ++i) {
if (T[i] == 2) {
if (ans[U[i]]) {
ans[V[i]] = ans[U[i]];
} else {
ans[U[i]] = ans[V[i]];
}
}
}
DSU dsu(m);
for (int i = 1; i <= k; ++i) {
if (T[i] == 2) {
dsu.join(U[i], V[i]);
}
}
for (int i = 1; i <= m; ++i) {
if (ans[i]) {
dsu.color[dsu.find(i)] = ans[i];
}
}
for (int i = 1; i <= m; ++i) {
int x = dsu.find(i);
if (dsu.color[x]) {
cout << "K" << dsu.color[x] << '\n';
} else {
cout << "?" << '\n';
}
}
return 0;
}
# | 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... |