Submission #238659

# Submission time Handle Problem Language Result Execution time Memory
238659 2020-06-12T09:22:37 Z Vimmer Pictionary (COCI18_pictionary) C++14
56 / 140
1500 ms 2864 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 100001
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9

using namespace std;
//using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 2> a2;

//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


int pr[N], rk[N];

int fnd(int x) {if (pr[x] != x) pr[x] = fnd(pr[x]); return pr[x];}

void link(int a, int b)
{
    if (rk[b] > rk[a]) swap(a, b);

    pr[b] = a;

    rk[a] += rk[b];
}

int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);

    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, m, q;

    cin >> n >> m >> q;

    for (int i = 1; i <= n; i++) {rk[i] = 1; pr[i] = i;}

    vector <pair <int, int> > g(q);

    for (int i = 0; i < q; i++) cin >> g[i].F >> g[i].S;

    int ans[q], kr = 0;

    memset(ans, 0, sizeof(ans));

    for (int x = m; x >= 2 && kr < q; x--)
    {
        if (m / x == 1)
            for (int j = 1; j <= n / x; j++)
                for (int u = j + 1; u <= n / x; u++)
                {
                    int a = fnd(j * x), b = fnd(u * x);

                    if (a != b) link(a, b);
                }
        else
            for (int j = 2; j <= n / x; j++)
            {
                int a = fnd(j * x), b = fnd(x);

                if (a != b) link(a, b);
            }

        for (int i = 0; i < q; i++)
          if (ans[i] == 0 && fnd(g[i].F) == fnd(g[i].S)) {kr++; ans[i] = m - x + 1;}
    }

    for (int i = 0; i < q; i++) if (ans[i] == 0) ans[i] = m;

    for (int i = 0; i < q; i++) cout << ans[i] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 383 ms 1656 KB Output is correct
2 Correct 186 ms 1656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 575 ms 2296 KB Output is correct
2 Correct 350 ms 1912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1573 ms 1152 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1588 ms 1280 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1592 ms 1656 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 385 ms 2296 KB Output is correct
2 Execution timed out 1577 ms 2864 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 1585 ms 2168 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1578 ms 2432 KB Time limit exceeded
2 Halted 0 ms 0 KB -