Submission #233731

# Submission time Handle Problem Language Result Execution time Memory
233731 2020-05-21T14:47:06 Z Mamedov KOVANICE (COI15_kovanice) C++14
50 / 100
2000 ms 17124 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() {
    ios_base::sync_with_stdio(false);
    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 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 10732 KB Output is correct
2 Correct 58 ms 10860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2069 ms 1688 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2080 ms 17124 KB Time limit exceeded
2 Halted 0 ms 0 KB -