답안 #1084602

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084602 2024-09-06T13:12:26 Z vjudge1 KOVANICE (COI15_kovanice) C++17
100 / 100
112 ms 32848 KB
#include <bits/stdc++.h>

using namespace std;

int n, m, v;
int parent[300005], sz[300005], rez[300005];
vector<int> G[300005];

int Find(int x) {
    if (x==parent[x])
        return x;
    return parent[x]=Find(parent[x]);
}

void Union(int a, int b) {
    a=Find(a), b=Find(b);
    if (a==b) return;
    if (sz[a]<sz[b])
        swap(a, b);
    sz[a]+=sz[b];
    parent[b]=a;
}

int dolz[300005];

int dfs1(int teme) {
    if (dolz[teme]!=-1) return dolz[teme];
    dolz[teme]=1;
    for (auto next:G[teme])
        dolz[teme]=max(dolz[teme], dfs1(next)+1);
    return dolz[teme];
}

void dfs2(int teme, int tezina) {
    if (rez[teme]) return;
    rez[teme]=tezina;
    for (auto next:G[teme]) {
        if (rez[next]==0&&dolz[teme]==dolz[next]+1)
            dfs2(next, tezina+1);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> v;
    int br1[v], br2[v];
    char znak[v];
    for (int i=1;i<=m;i++)
        parent[i]=i, sz[i]=1;
    for (int i=0;i<v;i++) {
        cin >> br1[i] >> znak[i] >> br2[i];
        if (znak[i]=='=')
            Union(br1[i], br2[i]);
    }
    int inDegree[m+1]={0};
    for (int i=0;i<v;i++) {
        if (znak[i]=='<') {
            G[Find(br1[i])].push_back(Find(br2[i]));
            inDegree[Find(br2[i])]++;
        }
    }
    vector<int> roots;
    memset(dolz, -1, sizeof(dolz));
    for (int i=1;i<=m;i++) {
        if (inDegree[i]==0&&i==Find(i)) {
            int d=dfs1(i);
            if (d==n) dfs2(i,1);
        }
    }
    for (int i=1;i<=m;i++)
        rez[i]=rez[Find(i)];
    for (int i=1;i<=m;i++) {
        if (rez[i]>0) cout << 'K' << rez[i] << '\n';
        else cout << '?' << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8540 KB Output is correct
2 Correct 4 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 15656 KB Output is correct
2 Correct 39 ms 15700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 10844 KB Output is correct
2 Correct 14 ms 10844 KB Output is correct
3 Correct 15 ms 10956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 25012 KB Output is correct
2 Correct 96 ms 24748 KB Output is correct
3 Correct 86 ms 24660 KB Output is correct
4 Correct 108 ms 26208 KB Output is correct
5 Correct 112 ms 32848 KB Output is correct