#include <bits/stdc++.h>
using namespace std;
const int mxN=3e5;
int n, m, v, p[mxN], r[mxN], a[mxN], b[mxN], dp[2][mxN];
vector<int> adj[2][mxN];
int root(int x) {
return p[x]==x?x:(p[x]=root(p[x]));
}
void join(int x, int y) {
if((x=root(x))==(y=root(y)))
return;
if(r[x]<r[y])
p[x]=y;
else
p[y]=x;
r[x]+=r[x]==r[y];
}
int cdp(int a, int u) {
if(!dp[a][u]) {
dp[a][u]=a?n:1;
for(int v : adj[a][u])
dp[a][u]=a?min(cdp(a, v)-1, dp[a][u]):max(cdp(a, v)+1, dp[a][u]);
}
return dp[a][u];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> v;
for(int i=0; i<m; ++i)
p[i]=i;
for(int i=0; i<v; ++i) {
char qt;
cin >> a[i] >> qt >> b[i], --a[i], --b[i];
if(qt=='=') {
join(a[i], b[i]);
a[i]=-1;
} else if(qt=='>')
swap(a[i], b[i]);
}
for(int i=0; i<v; ++i) {
if(a[i]!=-1) {
adj[0][root(b[i])].push_back(root(a[i]));
adj[1][root(a[i])].push_back(root(b[i]));
}
}
for(int i=0; i<m; ++i)
cout << (cdp(0, root(i))==cdp(1, root(i))?"K"+to_string(cdp(0, root(i))):"?") << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
14588 KB |
Output is correct |
2 |
Correct |
14 ms |
14696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
22764 KB |
Output is correct |
2 |
Correct |
141 ms |
24368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
24368 KB |
Output is correct |
2 |
Correct |
36 ms |
24368 KB |
Output is correct |
3 |
Correct |
37 ms |
24368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
344 ms |
39116 KB |
Output is correct |
2 |
Correct |
347 ms |
42452 KB |
Output is correct |
3 |
Correct |
310 ms |
46044 KB |
Output is correct |
4 |
Correct |
414 ms |
54228 KB |
Output is correct |
5 |
Correct |
442 ms |
58748 KB |
Output is correct |