제출 #1344495

#제출 시각아이디문제언어결과실행 시간메모리
1344495gkos5678KOVANICE (COI15_kovanice)C++20
100 / 100
134 ms48228 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(v) v.begin(), v.end()

typedef vector<int> vi;

const int maxn = 1e6 + 7;

struct ve{
    int le, ri;
    char nj;
};

int n, m, v, c[maxn], ans[maxn];
bool b[maxn], vis[maxn];
vi jd[maxn], trc, cf, ou[maxn], in[maxn], ts;
ve sv[maxn];

bool num(char ch){
    return (ch >= '0' && ch <= '9');
}

ve sep(string s){
    ve ret;
    ret.le = 0;
    int i = 0;
    for (; num(s[i]); i++){
        ret.le *= 10;
        ret.le += s[i] - '0';
    }
    ret.nj = s[i];
    i++;
    ret.ri = 0;
    for (; i < s.size(); i++){
        ret.ri *= 10;
        ret.ri += s[i] - '0';
    }
    return ret;
}

void dfs1(int cv){
    if (vis[cv]) return;
    vis[cv] = 1;
    trc.pb(cv);
    for (int ss : jd[cv])
        dfs1(ss);
}

void dfs2(int cv){
    if (vis[cv]) return;
    vis[cv] = 1;
    for (int ss : in[cv])
        dfs2(ss);
    ts.pb(cv);
}

void dfs3(int cv){
    if (b[cv]) return;
    b[cv] = 1;
    for (int ss : in[cv])
        if (ans[ss] == (ans[cv] - 1))
            dfs3(ss);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n >> m >> v;
    for (int i = 1; i <= v; i++){
        string s;
        cin >> s;
        sv[i] = sep(s);
        if (sv[i].nj == '='){
            jd[sv[i].le].pb(sv[i].ri);
            jd[sv[i].ri].pb(sv[i].le);
        }
    }

    for (int i = 1; i <= m; i++){
        trc.clear();
        dfs1(i);
        for (int el : trc)
            c[el] = i;
        if (c[i] == i)
            cf.pb(i);
    }

    for (int i = 1; i <= v; i++){
        if (sv[i].nj == '=') continue;
        ou[c[sv[i].le]].pb(c[sv[i].ri]);
        in[c[sv[i].ri]].pb(c[sv[i].le]);
    }
    for (int i = 0; i < cf.size(); i++)
        vis[cf[i]] = 0;
    for (int i = 0; i < cf.size(); i++)
        dfs2(cf[i]);
    for (int i = 0; i < cf.size(); i++){
        int tr = ts[i];
        ans[tr] = 1;
        for (int ss : in[tr])
            ans[tr] = max(ans[tr], ans[ss] + 1);
    }
    for (int i = 0; i < cf.size(); i++)
        if (ans[cf[i]] == n)
            dfs3(cf[i]);

    for (int i = 1; i <= m; i++){
        if (b[c[i]])
            cout << 'K' << ans[c[i]] << "\n";
        else
            cout << "?\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...