Submission #846205

# Submission time Handle Problem Language Result Execution time Memory
846205 2023-09-07T12:27:33 Z vjudge1 KOVANICE (COI15_kovanice) C++17
100 / 100
512 ms 68280 KB
#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=0,b=0;
    int go=-1;

    for(int j=0;j<sz(s);j++){
      if(!isdigit(s[j])){
      	go=j;
      	break;
      }
      int cr=s[j]-'0';
      a*=10;
      a+=cr;
    }

    for(int j=go+1;j<sz(s);j++){
      int cr=s[j]-'0';
      b*=10;
      b+=cr;
    }


    if(s[go]=='=') 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;
  	else ans[find(i)]=-1;
  }
  
  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 Correct 8 ms 26712 KB Output is correct
2 Correct 7 ms 26716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 37580 KB Output is correct
2 Correct 90 ms 38092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 28884 KB Output is correct
2 Correct 28 ms 29488 KB Output is correct
3 Correct 34 ms 30928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 55416 KB Output is correct
2 Correct 349 ms 55632 KB Output is correct
3 Correct 327 ms 55360 KB Output is correct
4 Correct 512 ms 68280 KB Output is correct
5 Correct 499 ms 66376 KB Output is correct