This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;
const int sz = 3e5 + 5;
int n, m, v, p[sz], a[sz], b[sz], dp[sz], ans[sz];
char c[sz];
vector<int> g[sz];
int par(int x)
{
if(p[x] < 0) return x;
return p[x] = par(p[x]);
}
bool join(int u, int v)
{
u = par(u), v = par(v);
if(u == v) return 0;
if(p[u] > p[v]) swap(u, v);
p[u] += p[v];
p[v] = u;
return 1;
}
void dfs(int v)
{
dp[v] = 1;
for(int i : g[v])
{
if(!dp[i]) dfs(i);
dp[v] = max(dp[v], dp[i] + 1);
}
}
void dfs(int v, int dist)
{
ans[v] = dist;
for(int i : g[v])
{
if(dp[i] == dp[v] - 1)
{
dfs(i, dist + 1);
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> v;
fill(p, p + sz, -1);
for(int i = 1; i <= v; i++)
{
cin >> a[i] >> c[i] >> b[i];
if(c[i] == '=')
{
join(a[i], b[i]);
}
}
for(int i = 1; i <= v; i++)
{
if(c[i] == '<')
{
g[par(a[i])].push_back(par(b[i]));
}
}
for(int i = 1; i <= m; i++)
{
if(p[par(i)] < 0 && !dp[par(i)])
{
dfs(par(i));
}
}
for(int i = 1; i <= m; i++)
{
if(p[par(i)] < 0 && dp[par(i)] == n && !ans[par(i)])
{
dfs(par(i), 1);
}
}
for(int i = 1; i <= m; i++)
{
if(ans[par(i)]) cout << "K" << ans[par(i)];
else cout << "?";
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... |