Submission #1318643

#TimeUsernameProblemLanguageResultExecution timeMemory
1318643hashimzaderashidKOVANICE (COI15_kovanice)C++20
100 / 100
553 ms39392 KiB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll>p(3e5);
vector<ll>cl(3e5);
vector<ll>ch(3e5);
vector<vector<ll>>g;
vector<vector<ll>>rg;
ll findp(ll a){
    if(p[a] == a){
        return a;
    }
    return p[a] = findp(p[a]);
}
ll dfs_g(ll k){
    if(ch[k]){
        return ch[k];
    }
    ch[k] = 1;
    for(auto it : g[k]){
        ch[k] = max(ch[k],dfs_g(it)+1);
    }
    return ch[k];
}
ll dfs_rg(ll k){
    if(cl[k]){
        return cl[k];
    }
    cl[k] = 1;
    for(auto it : rg[k]){
        cl[k] = max(cl[k],dfs_rg(it)+1);
    }
    return cl[k];
}
int main(){
    ll a,b,c,d,e,f,g1;
    cin>>a>>b>>c;
    for(int i = 1;i<=b;i++){
        p[i] = i;
    }
    vector<pair<ll,ll>>v;
    for(int i = 0;i < c;i++){
        ll x,y;
        char chh;
        cin>>x>>chh>>y;
        if(chh == '<'){
            v.push_back({x,y});
        }
        else{
            p[findp(x)] = findp(y);
        }
    }
    g.assign(b+1,vector<ll>());
    rg.assign(b+1,vector<ll>());
    for(int i = 0;i<v.size();i++){
        ll x = findp(v[i].first);
        ll y = findp(v[i].second);
        if(x != y){
            g[x].push_back(y);
            rg[y].push_back(x);
        }
    }
    for(int i = 1;i<=b;i++){
        if(p[i] == i){
            dfs_g(i);
            dfs_rg(i);
        }
    }
    for(int i = 1;i<=b;i++){
        ll r = findp(i);
        if(cl[r]+ch[r]-1 == a){
            cout<<"K"<<cl[r]<<endl;
        }
        else{
            cout<<"?"<<endl;
        }
    }
}
//By Rashid_Hashimzade
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...