#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;
}
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;
}
}
}
return fx;
}
int32_t main(){
//freopen("in.txt","r", stdin);
int n, m, v;
cin>>n>>m>>v;
for(int i = 1; i <= m; 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];
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);
gvis.clear();
}
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:72: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]
72 | for(int i = 0; i < type.size(); i++){
| ~~^~~~~~~~~~~~~
kovanice.cpp:93: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]
93 | for(int i = 0; i < q.size(); i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
19288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
54 ms |
31424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
23232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
136 ms |
45104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |