#include <bits/stdc++.h>
#define N 300005
using namespace std;
int par[N];
int find(int a){
if(a == par[a])return a;
return par[a] = find(par[a]);
}
bool merge(int a,int b){
a = find(a);
b = find(b);
if(a == b)return 0;
par[a] = b;
return 1;
}
vector<int> adj1[N];
vector<int> adj2[N];
int a[N],b[N];
char c[N];
int l[N],r[N];
int ans[N];
int n;
void dfs1(int v){
if(l[v] != 1)return;
for(auto u:adj1[v]){
dfs1(u);
l[v] = max(l[v],l[u]+1);
}
}
void dfs2(int v){
if(r[v] != n)return;
for(auto u:adj2[v]){
dfs2(u);
r[v] = min(r[v],r[u]-1);
}
}
void solve(){
int m,v;
cin >> n >> m >> v;
for(int i = 1;i<=m;i++){
par[i] = i;
l[i] = 1,r[i] = n;
ans[i] = -1;
}
for(int i = 1;i<=v;i++){
cin >> a[i] >> c[i] >> b[i];
if(c[i] == '=')
merge(a[i],b[i]);
}
for(int i = 1;i<=v;i++){
a[i] = find(a[i]);
b[i] = find(b[i]);
if(c[i] == '<'){
adj1[b[i]].push_back(a[i]);
adj2[a[i]].push_back(b[i]);
}
}
for(int i = 1;i<=m;i++){
if(i == find(i)){
dfs1(i);
dfs2(i);
if(l[i] == r[i])
ans[i] = l[i];
}
}
for(int i = 1;i<=m;i++){
if(ans[find(i)] == -1){
cout << "?" << endl;
}
else cout << "K" << ans[find(i)] << endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--){
solve();
}
#ifdef Local
cout << endl << fixed << setprecision(2) << 1000.0*clock()/CLOCKS_PER_SEC << " milliseconds.";
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
21360 KB |
Output is correct |
2 |
Correct |
292 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
16468 KB |
Output is correct |
2 |
Correct |
25 ms |
16460 KB |
Output is correct |
3 |
Correct |
29 ms |
16508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
565 ms |
30560 KB |
Output is correct |
2 |
Correct |
566 ms |
33668 KB |
Output is correct |
3 |
Correct |
572 ms |
33488 KB |
Output is correct |
4 |
Correct |
596 ms |
37604 KB |
Output is correct |
5 |
Correct |
584 ms |
38268 KB |
Output is correct |