Submission #754605

# Submission time Handle Problem Language Result Execution time Memory
754605 2023-06-08T06:54:33 Z MilosMilutinovic Pictionary (COCI18_pictionary) C++14
140 / 140
95 ms 12548 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<pair<int, int>> qs[N];
int n, m, q, fa[N], ans[N];
int gfa(int x) { return fa[x] == x ? x : fa[x] = gfa(fa[x]); }
void unite(int x, int y, int w) {
    x = gfa(x);
    y = gfa(y);
    if (x == y) {
        return;
    }
    if (qs[x].size() < qs[y].size()) {
        swap(x, y);
    }
    for (auto& p : qs[y]) {
        if (gfa(p.first) == x) {
            ans[p.second] = w;
        } else {
            qs[x].push_back(p);
        }
    }
    fa[y] = x;
}
int main() {
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i++) {
        fa[i] = i;
    }
    for (int i = 1; i <= q; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        qs[a].emplace_back(b, i);
        qs[b].emplace_back(a, i);
    }
    for (int d = m; d >= 1; d--) {
        vector<int> vec;
        for (int i = d; i <= n; i += d) {
            vec.push_back(i);
        }
        for (int i = 0; i + 1 < vec.size(); i++) {
            unite(vec[i], vec[i + 1], m - d + 1);
        }
    }
    for (int i = 1; i <= q; i++) {
        printf("%d\n", ans[i]);
    }
    return 0;
}

Compilation message

pictionary.cpp: In function 'int main()':
pictionary.cpp:41:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         for (int i = 0; i + 1 < vec.size(); i++) {
      |                         ~~~~~~^~~~~~~~~~~~
pictionary.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     scanf("%d%d%d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
pictionary.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7984 KB Output is correct
2 Correct 22 ms 6860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 9980 KB Output is correct
2 Correct 28 ms 8444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 6744 KB Output is correct
2 Correct 29 ms 6724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 7140 KB Output is correct
2 Correct 32 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 9216 KB Output is correct
2 Correct 42 ms 7048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 8464 KB Output is correct
2 Correct 63 ms 10460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 11456 KB Output is correct
2 Correct 87 ms 11120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 12548 KB Output is correct
2 Correct 81 ms 12152 KB Output is correct