Submission #1084536

# Submission time Handle Problem Language Result Execution time Memory
1084536 2024-09-06T11:20:17 Z vjudge1 KOVANICE (COI15_kovanice) C++17
50 / 100
2000 ms 35908 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;
}

bool vis[300005];

int dfs(int teme, int klk_pred) {
    int k=0;
    for (auto next:G[teme]) {
        if (!vis[next])
            k=max(k, 1+dfs(next, klk_pred-1));
    }
    if (klk_pred==k) rez[teme]=n-klk_pred;
    else rez[teme]=-1;
    return k;
}

int main()
{
    cin >> n >> m >> v;
    string merenja[v];
    for (int i=0;i<v;i++)
        cin >> merenja[i];
    for (int i=1;i<=m;i++)
        parent[i]=i, sz[i]=1;
    int br1[v], br2[v];
    char znak[v];
    for (int j=0;j<v;j++) {
        br1[j]=br2[j]=0;
        znak[j]='.';
        string x=merenja[j];
        for (int i=0;i<x.size();i++) {
            if (isdigit(x[i])&&znak[j]=='.') br1[j]*=10, br1[j]+=x[i]-'0';
            else if (isdigit(x[i])) br2[j]*=10, br2[j]+=x[i]-'0';
            else znak[j]=x[i];
        }
        if (znak[j]=='=') Union(br1[j], br2[j]);
    }
    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])]++;
        }
    }
    set<int> roots;
    for (int i=1;i<=m;i++)
        if (inDegree[Find(i)]==0) roots.insert(Find(i));
    for (auto teme:roots)
        dfs(teme, n-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] << endl;
        else cout << '?' << endl;
    }

    return 0;
}

Compilation message

kovanice.cpp: In function 'int main()':
kovanice.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for (int i=0;i<x.size();i++) {
      |                      ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7516 KB Output is correct
2 Correct 5 ms 7544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 280 ms 23136 KB Output is correct
2 Correct 274 ms 23312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2063 ms 12888 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2066 ms 35908 KB Time limit exceeded
2 Halted 0 ms 0 KB -