QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#425206#7177. Many Many Cyclesreal_sigma_team#WA 1ms3544kbC++233.1kb2024-05-30 01:35:092024-05-30 01:35:09

Judging History

你现在查看的是最新测评结果

  • [2024-05-30 01:35:09]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3544kb
  • [2024-05-30 01:35:09]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

struct rooted_tree {
    struct sparce_table {
        vector<vector<pair<int, int>>> mn;

        sparce_table() {}

        sparce_table(vector<pair<int, int>> &a) {
            build(a);
        }

        void build(vector<pair<int, int>> &a) {
            int n = a.size();
            int logn = __lg(n) + 1;
            mn.assign(logn, vector<pair<int, int>>(n));
            for (int i = 0; i < n; ++i)
                mn[0][i] = a[i];
            for (int l = 0; l < logn - 1; l++)
                for (int i = 0; i + (2 << l) <= n; i++)
                    mn[l + 1][i] = min(mn[l][i], mn[l][i + (1 << l)]);
        }

        pair<int, int> get(int l, int r) {
            if (l > r)
                swap(l, r);
            ++r;
            int t = __lg(r - l);
            return min(mn[t][l], mn[t][r - (1 << t)]);
        }
    };

    vector<int> pos;
    vector<int> dist;
    sparce_table spt;

    rooted_tree(vector<vector<pair<int, int>>> &g) {
        int n = g.size();
        pos.assign(n, -1);
        dist.resize(n);
        vector<pair<int, int>> tr;
        dfs(0, g, tr, 0, 0);
        spt.build(tr);
    }

    void dfs(int v, vector<vector<pair<int, int>>> &gr, vector<pair<int, int>> &tr, int depth, int d) {
        pos[v] = tr.size();
        dist[v] = d;
        tr.push_back({depth, v});
        for (auto [u, _]: gr[v])
            if (pos[u] == -1) {
                dfs(u, gr, tr, depth + 1, d + _);
                tr.push_back({depth, v});
            }
    }

    int get(int v, int u) {
        return dist[v] + dist[u] - 2 * dist[spt.get(pos[v], pos[u]).second];
    }
};

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> gr(n);
    vector<array<int, 3>> e;
    for (int i = 0, u, v, c; i < m; ++i) {
        cin >> u >> v >> c;
        --u, --v;
        gr[u].push_back({v, c});
        gr[v].push_back({u, c});
        e.push_back({u, v, c});
        e.push_back({v, u, c});
    }
    vector<vector<pair<int, int>>> tree(n);
    vector<bool> used(n, false);
    auto dfs = [&](auto self, int v) -> void {
        used[v] = true;
        for (auto [u, c] : gr[v]) {
            if (!used[u]) {
                self(self, u);
                tree[v].push_back({u, c});
                tree[u].push_back({v, c});
            }
        }
    };
    dfs(dfs, 0);
    rooted_tree tr(tree);
    int d = 0;
    for (int i = 0; i < 2 * m; ++i) {
        if (tr.dist[e[i][0]] + e[i][2] == tr.dist[e[i][1]] || tr.dist[e[i][1]] + e[i][2] == tr.dist[e[i][0]])
            continue;
        d = __gcd(d, tr.get(e[i][0], e[i][1]) + e[i][2]);
        for (int j = 0; j < 2 * m; ++j) {
            if (tr.dist[e[j][0]] + e[j][2] == tr.dist[e[j][1]] || tr.dist[e[j][1]] + e[j][2] == tr.dist[e[j][0]])
                continue;
            if (e[i][0] == e[j][0])
                d = __gcd(d, tr.get(e[i][0], e[j][1]) + e[i][2] + e[j][2]);
        }
    }
    cout << d;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3544kb

input:

4 4
1 2 1
2 3 1
3 4 1
4 1 1

output:

1

result:

wrong answer expected '4', found '1'