#pragma GCC optimize("unroll-loops,Ofast,O3")
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define spc << " " <<
#define all(x) x.begin(), x.end()
#define ll long long
#define int long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define st first
#define nd second
#define inf 1000000009
#define MOD 998244353
using namespace std;
int dad[300005];
string ans[300005];
int dep[300005];
vi edges[300005];
int find(int x){
if(dad[x]==x) return x;
dad[x]=find(dad[x]);
return dad[x];
}
void unite(int x, int y){
dad[find(x)]=find(y);
}
void dfs(int node){
if(dep[find(node)]!=-1) return;
dep[find(node)]=1;
for(auto i:edges[find(node)]){
dfs(find(i));
dep[find(node)]=max(dep[find(node)], dep[find(i)]+1);
}
}
void mark(int node){
if(ans[find(node)]!="?") return;
ans[find(node)] = "K";
ans[find(node)] += to_string(dep[find(node)]);
for(auto i:edges[find(node)]){
if(dep[find(i)]==dep[find(node)]-1){
mark(find(i));
}
}
}
void solve(){
int n, m, v; cin >> n >> m >> v;
for(int i=1; i<=m; i++){
dad[i]=i;
ans[i]="?";
dep[i]=-1;
}
vector<string> later;
for(int i=1; i<=v; i++){
string s; cin >> s;
int a=0, b=0, ptr=0;
while(s[ptr]!='=' && s[ptr]!='<'){
a*=10;
a+=s[ptr]-'0';
ptr++;
}
char comp=s[ptr];
ptr++;
while(ptr<s.size()){
b*=10;
b+=s[ptr]-'0';
ptr++;
}
if(comp=='='){
unite(find(a),find(b));
}
else later.pb(s);
}
for(auto s:later){
int a=0, b=0, ptr=0;
while(s[ptr]!='=' && s[ptr]!='<'){
a*=10;
a+=s[ptr]-'0';
ptr++;
}
char comp=s[ptr];
ptr++;
while(ptr<s.size()){
b*=10;
b+=s[ptr]-'0';
ptr++;
}
edges[find(b)].pb(find(a));
}
for(int i=1; i<=m; i++){
dfs(find(i));
}
for(int i=1; i<=m; i++){
if(dep[find(i)]==-1) cerr << "wtf" << endl;
if(dep[find(i)]==n) mark(find(i));
}
for(int i=1; i<=m; i++){
cout << ans[find(i)] << endl;
}
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out","w",stdout);
#endif
ll t=1;
//cin >> t;
while(t--) solve();
}
Compilation message
kovanice.cpp: In function 'void solve()':
kovanice.cpp:84:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | while(ptr<s.size()){
| ~~~^~~~~~~~~
kovanice.cpp:103:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | while(ptr<s.size()){
| ~~~^~~~~~~~~
kovanice.cpp:101:14: warning: unused variable 'comp' [-Wunused-variable]
101 | char comp=s[ptr];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
21080 KB |
Output is correct |
2 |
Correct |
6 ms |
21080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
24772 KB |
Output is correct |
2 |
Correct |
268 ms |
25384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
26832 KB |
Output is correct |
2 |
Correct |
18 ms |
26576 KB |
Output is correct |
3 |
Correct |
20 ms |
27088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
435 ms |
34488 KB |
Output is correct |
2 |
Correct |
433 ms |
35036 KB |
Output is correct |
3 |
Correct |
413 ms |
34996 KB |
Output is correct |
4 |
Correct |
464 ms |
38952 KB |
Output is correct |
5 |
Correct |
485 ms |
52424 KB |
Output is correct |