#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]){
if(check(i, dep - 1)){
fx = 1;
ans[node] = dep;
}
}
else if(ans[i]){
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++){
string s;
cin>>s;
if(s[1] == '='){
merge(s[0] - '0', s[2] - '0');
}
else{
type.pb({s[0] - '0', s[2] - '0'});
}
}
vector<int> q;
vector<int> start(m+1, 1);
sort(type.begin(), type.end());
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];
//cout<<k<<" cikiyom "<<endl;
for(auto itr: adj[k]){
val[itr] = max(val[itr], val[k] + 1);
//cout<<k<<" dan -> "<<itr<<endl;
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:80: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]
80 | for(int i = 0; i < type.size(); i++){
| ~~^~~~~~~~~~~~~
kovanice.cpp:101: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]
101 | for(int i = 0; i < q.size(); i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
19292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
31448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
23496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
136 ms |
43104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |