Submission #233728

# Submission time Handle Problem Language Result Execution time Memory
233728 2020-05-21T14:45:30 Z Mamedov KOVANICE (COI15_kovanice) C++14
50 / 100
2000 ms 20996 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back

using namespace std;

const int up = 3e5 + 5;
int n;
int par[up], type[up], inDegree[up], root[up];
vector <pii> equals, lesses;
vector <vector<int>> g;

void extract(string &ACB, int &A, int &B, char &C) {
    A = B = 0;
    int i = 0;
    while(isdigit(ACB[i])) {
        A = 10 * A + ACB[i] - '0';
        ++i;
    }
    C = ACB[i];
    ++i;
    while(ACB[i] != '\0') {
        B = 10 * B + ACB[i] - '0';
        ++i;
    }
}

int Find(int u) {
    return (par[u] < 0 ? u : par[u] = Find(par[u]));
}

void Union(int u, int v) {
    u = Find(u);
    v = Find(v);
    if(u != v) {
        if(par[u] > par[v]) {
            swap(u, v);
        }
        par[u] += par[v];
        par[v] = u;
    }
}

void dfs(int node, int depth) {
    if(depth == n) {
        type[node] = n;
    }
    if(type[node]) return;
    for(int to : g[node]) {
        dfs(to, depth + 1);
        if(type[to]) {
            type[node] = type[to] - 1;
        }
    }
}
void solve() {
    int m, v, A, B;
    char C;
    string ACB;
    cin >> n >> m >> v;
    for(int i = 0; i < v; ++i) {
        cin >> ACB;
        extract(ACB, A, B, C);
        if(C == '=') {
            equals.pb({A, B});
        }else {
            lesses.pb({A, B});
        }
    }
    fill(par + 1, par + m + 1, -1);
    for(pii e : equals) {
        Union(e.f, e.s);
    }
    for(int i = 1; i <= m; ++i) {
        root[i] = Find(i);
    }
    g.resize(m + 1);
    for(pii l : lesses) {
        g[root[l.f]].pb(root[l.s]);
        ++inDegree[root[l.s]];
    }
    for(int i = 1; i <= m; ++i) {
        if(i == root[i] && !inDegree[i]) dfs(i, 1);
    }
    for(int i = 1; i <= m; ++i) {
        if(!type[root[i]]) {
            cout << '?' << '\n';
        }else {
            cout << 'K' << type[root[i]] << '\n';
        }
    }
}
int main()
{
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 12012 KB Output is correct
2 Correct 123 ms 12136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2086 ms 2540 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2090 ms 20996 KB Time limit exceeded
2 Halted 0 ms 0 KB -