This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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});
}
for(auto& x:edges){
x[0]=find(x[0]),x[1]=find(x[1]);
v[x[0]].pb(x[1]);
v2[x[1]].pb(x[0]);
}
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |