#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
const int N = 3e5 + 5;
int par[N],siz[N],ans[N];
int find(int a){
if(par[a]==a) return a;
return par[a]=find(par[a]);
}
void merge(int a,int b){
a=find(a),b=find(b);
if(a==b) return;
if(siz[a]>siz[b]) swap(a,b);
siz[b]+=siz[a];
par[a]=b;
}
vector<int> v[N],vis(N),v2[N],vis2(N);
int dp[N],dp2[N];
void dfs(int c){
if(vis[c]) return;
vis[c]=1;
for(int x:v[c]){
dfs(x);
dp[c]=max(dp[c],dp[x]+1);
}
}
void dfs2(int c){
if(vis2[c]) return;
vis2[c]=1;
for(int x:v2[c]){
dfs2(x);
dp2[c]=max(dp2[c],dp2[x]+1);
}
}
void solve(){
iota(par,par+N,0LL);
fill(siz,siz+N,1LL);
memset(ans,-1,sizeof(ans));
int c,n,m;
cin >> c >> n >> m;
vector<array<int,2>> edges;
for(int i=1;i<=m;i++){
string s;
cin >> s;
int a=s[0]-'0';
int b=s[2]-'0';
if(s[1]=='=') merge(a,b);
else edges.pb({b,a});
}
map<array<int,2>,int> mp;
for(array<int,2> x:edges){
int a=x[0],b=x[1];
a=find(a),b=find(b);
if(mp[{a,b}]) continue;
mp[{a,b}]=1;
v[a].pb(b);
v2[b].pb(a);
}
for(int i=1;i<=n;i++){
dfs(find(i));
dfs2(find(i));
}
for(int i=1;i<=n;i++){
int ub=c-dp2[find(i)];
int lb=1+dp[find(i)];
if(ub==lb) ans[find(i)]=ub;
}
for(int i=1;i<=n;i++){
if(ans[find(i)]==-1) cout << '?' << endl;
else cout << 'K' << ans[find(i)] << endl;
}
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
26712 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
28884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
28884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
35524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |