#include <bits/stdc++.h>
#define endl "\n"
#define pb push_back
#define int long long
using namespace std;
const int inf = 2e18 + 5;
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
vector<int> par(N);
vector<int> adj[N], radj[N];
vector<int> ans(N, -1);
map<int, int> gvis;
int find(int a){
if(a == par[a]) return a;
return par[a] = find(par[a]);
}
void merge(int a, int b){
a = find(a), b = find(b);
par[a] = b;
}
bool check(int node, int dep){
if(radj[node].size() == 0 && dep == 1){
ans[node] = 1;
return 1;
}
if(ans[node] > 0) return 1;
gvis[node] = 1;
bool fx = 0;
for(auto itr: radj[node]){
int i = find(itr);
if((!gvis[i] && check(i, dep-1)) || ans[i] != -1){
fx = 1;
ans[node] = dep;
}
}
return fx;
}
int32_t main(){
//freopen("in.txt","r", stdin);
int n, m, v;
cin>>n>>m>>v;
for(int i = 1; i <= m+5; i++) par[i] = i;
vector<array<int, 2> > type;
for(int i = 0; i < v; i++){
int a, b;
char c;
cin>>a>>c>>b;
if(c == '='){
merge(a, b);
}
else{
type.pb({a, b});
}
}
vector<int> q;
vector<int> start(m+1, 1);
for(int i = 0; i < type.size(); i++){
int x = find(type[i][0]), y = find(type[i][1]);
adj[x].pb(y);
radj[y].pb(x);
start[y] = 0;
}
vector<int> vis(m+1), val(m+1);
for(int i = 1; i <= m; i++){
int x = find(i);
if(start[x] && !vis[x]){
q.pb(x);
vis[x] = 1;
val[x] = 1;
}
}
vector<int> mxchain;
vector<int> vis2(m+1);
for(int i = 0; i < q.size(); i++){
int k = q[i];
for(auto itr: adj[k]){
val[itr] = max(val[itr], val[k] + 1);
if(!vis2[itr] && val[itr] == n){
vis2[itr] = 1;
mxchain.pb(itr);
}
if(!vis[itr]){
q.pb(itr);
vis[itr] = 1;
}
}
}
for(auto itr: mxchain){
check(find(itr), n);
}
for(int i = 1; i <= m; i++){
int x = find(i);
if(ans[x] == -1) cout<<"?"<<endl;
else cout<<"K"<<ans[x]<<endl;
}
return 0;
}
Compilation message
kovanice.cpp: In function 'int32_t main()':
kovanice.cpp:73:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0; i < type.size(); i++){
| ~~^~~~~~~~~~~~~
kovanice.cpp:94:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int i = 0; i < q.size(); i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19288 KB |
Output is correct |
2 |
Correct |
6 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
35988 KB |
Output is correct |
2 |
Correct |
122 ms |
36744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
22724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
232 ms |
43956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |