Submission #93541

# Submission time Handle Problem Language Result Execution time Memory
93541 2019-01-09T12:34:33 Z keko37 Pictionary (COCI18_pictionary) C++14
140 / 140
388 ms 14964 KB
#include<bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

int n, m, q;
int a[MAXN], b[MAXN], siz[MAXN], par[MAXN];
int low[MAXN], high[MAXN];
vector <int> v[MAXN];

void precompute () {
    for (int i=1; i<=q; i++) {
        low[i] = 1;
        high[i] = m;
    }
}

void build () {
    for (int i=1; i<=n; i++) {
        par[i] = i;
        siz[i] = 1;
    }
}

int nadi (int x) {
    if (x == par[x]) return x;
    par[x] = nadi(par[x]);
    return par[x];
}

void spoji (int x, int y) {
    x = nadi(x);
    y = nadi(y);
    if (x == y) return;
    if (siz[x] < siz[y]) swap(x, y);
    par[y] = x;
    if (siz[x] == siz[y]) siz[x]++;
}

bool bs () {
    for (int i=1; i<=m; i++) {
        v[i].clear();
    }
    for (int i=1; i<=q; i++) {
        v[(low[i] + high[i] + 1)/2].push_back(i);
    }
    build();
    bool ok = 1;
    for (int i=m; i>=1; i--) {
        for (int j=2*i; j<=n; j+=i) {
            spoji(i, j);
        }
        for (int j=0; j<v[i].size(); j++) {
            int ind = v[i] [j];
            //cout << "bla " << i << "  " << a[ind] << " " << b[ind] << endl;
            if (nadi(a[ind]) == nadi(b[ind])) {
                low[ind] = i;
            } else {
                high[ind] = i-1;
            }
            if (low[ind] != high[ind]) ok = 0;
        }
    }
    return ok;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> q;
    precompute();
    for (int i=1; i<=q; i++) {
        cin >> a[i] >> b[i];
    }
    while (!bs());
    for (int i=1; i<=q; i++) {
        cout << m - low[i] + 1 << endl;
    }
    return 0;
}

Compilation message

pictionary.cpp: In function 'bool bs()':
pictionary.cpp:54:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=0; j<v[i].size(); j++) {
                       ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 3704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 6900 KB Output is correct
2 Correct 112 ms 5920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 8724 KB Output is correct
2 Correct 147 ms 7616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 7192 KB Output is correct
2 Correct 114 ms 6772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 7936 KB Output is correct
2 Correct 116 ms 6620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 9888 KB Output is correct
2 Correct 164 ms 7540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 7896 KB Output is correct
2 Correct 277 ms 11256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 328 ms 12904 KB Output is correct
2 Correct 311 ms 11760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 388 ms 14964 KB Output is correct
2 Correct 365 ms 13332 KB Output is correct