#include <bits/stdc++.h>
using namespace std;
int n, m, v;
int parent[300005], sz[300005], rez[300005];
vector<int> G[300005];
int Find(int x) {
if (x==parent[x])
return x;
return parent[x]=Find(parent[x]);
}
void Union(int a, int b) {
a=Find(a), b=Find(b);
if (a==b) return;
if (sz[a]<sz[b])
swap(a, b);
sz[a]+=sz[b];
parent[b]=a;
}
int posle[300005];
int dfs(int teme, int klk_pred) {
if (posle[teme]) return posle[teme];
int k=0;
for (auto next:G[teme]) {
k=max(k, 1+dfs(next, klk_pred-1));
}
if (klk_pred==k) rez[teme]=n-klk_pred;
else rez[teme]=-1;
return posle[teme]=k;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> v;
int br1[v], br2[v];
char znak[v];
for (int i=1;i<=m;i++)
parent[i]=i, sz[i]=1;
for (int i=0;i<v;i++) {
cin >> br1[i] >> znak[i] >> br2[i];
if (znak[i]=='=')
Union(br1[i], br2[i]);
}
int inDegree[m+1]={0};
for (int i=0;i<v;i++) {
if (znak[i]=='<') {
G[Find(br1[i])].push_back(Find(br2[i]));
inDegree[Find(br2[i])]++;
}
}
vector<int> roots;
for (int i=1;i<=m;i++)
if (inDegree[i]==0&&i==Find(i))
roots.push_back(i);
for (auto teme:roots)
dfs(teme, n-1);
for (int i=1;i<=m;i++)
rez[i]=rez[Find(i)];
for (int i=1;i<=m;i++) {
if (rez[i]>0) cout << 'K' << rez[i] << '\n';
else cout << '?' << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7516 KB |
Output is correct |
2 |
Correct |
4 ms |
7512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
15824 KB |
Output is correct |
2 |
Correct |
38 ms |
15820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
9560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
25032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |