Submission #83158

# Submission time Handle Problem Language Result Execution time Memory
83158 2018-11-05T19:07:14 Z fredbr Pictionary (COCI18_pictionary) C++17
42 / 140
1500 ms 1524 KB
#include <bits/stdc++.h>

using namespace std;

using ii = pair<int, int>;


template <int maxn>
struct Dsu {
    int n;
    int pai[maxn], w[maxn];

    int find(int x)
    {
        if (x == pai[x]) return x;
        return pai[x] = find(pai[x]);
    }

    bool find(int a, int b)
    {
        a = find(a), b= find(b);
        return a == b;
    }
    void join(int a, int b)
    {
        a = find(a), b = find(b);
    
        if (w[a] < w[b]) swap(a, b);

        pai[b] = a;
        w[a] += w[b];
    }

    void init(int n_) {
        n  = n_;
        fill(w+1, w+n+1, 1);
        iota(pai+1, pai+n+1, 1);
    }
};

int const maxn = 101010;

Dsu<maxn> dsu;

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);

    int n, m;
    int q;
    cin >> n >> m >> q;

    while (q--) {
        dsu.init(n);

        int a, b;
        cin >> a >> b;

        for (int k = m; k >= 1; k--) {
            for (int i = k*2; i <= n; i += k) {
                if (!dsu.find(k, i)) dsu.join(k, i);
            }
            if (dsu.find(a, b)) {
                cout << m-k+1 << "\n";
                break;
            }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 34 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1082 ms 972 KB Output is correct
2 Correct 868 ms 1064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1512 ms 1064 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1560 ms 1064 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1547 ms 1064 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1577 ms 1176 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1574 ms 1228 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1560 ms 1240 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1571 ms 1524 KB Time limit exceeded
2 Halted 0 ms 0 KB -