QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#425236#7177. Many Many Cyclesreal_sigma_team#WA 1ms3796kbC++233.7kb2024-05-30 02:22:152024-05-30 02:22:16

Judging History

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

  • [2024-05-30 02:22:16]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3796kb
  • [2024-05-30 02:22:15]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long

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;
        for (int i = 0; i < n; ++i) {
            if (pos[i] == -1)
                dfs(i, 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];
    }

    int lca(int v, int u) {
        return 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});
    }
    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});
            }
        }
    };
    for (int i = 0; i < n; ++i) {
        if (!used[i])
            dfs(dfs, i);
    }
    rooted_tree tr(tree);
    int d = 0;
    for (int i = 0; i < m; ++i) {
        if (tr.dist[e[i][0]] > tr.dist[e[i][1]])
            swap(e[i][0], e[i][1]);
        if (tr.dist[e[i][0]] + e[i][2] == tr.dist[e[i][1]])
            continue;
        d = __gcd(d, tr.get(e[i][0], e[i][1]) + e[i][2]);
        for (int j = 0; j < m; ++j) {
            if (tr.dist[e[j][0]] > tr.dist[e[j][1]])
                swap(e[j][0], e[j][1]);
            if (tr.dist[e[i][0]] > tr.dist[e[j][0]])
                continue;
            if (tr.dist[e[j][0]] + e[j][2] == tr.dist[e[j][1]])
                continue;
            if (e[i][0] == e[j][0] && i != j) {
                d = __gcd(d, tr.get(e[i][1], e[j][1]) + e[i][2] + e[j][2]);
            } else if (i != j) {
                if (tr.dist[e[j][0]] < tr.dist[e[i][1]])
                    d = __gcd(d, tr.get(e[i][0], e[j][0]) + e[i][2] + e[j][2] + tr.get(e[i][1], e[j][1]));
                else
                    continue;
            }
        }
    }
    cout << d;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3500kb

input:

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

output:

4

result:

ok answer is '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

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

output:

4

result:

ok answer is '4'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3796kb

input:

20 50
1 2 8
1 3 1
3 4 5
3 5 9
3 6 5
6 7 6
7 8 8
2 9 2
8 10 3
8 11 7
8 12 5
3 13 4
7 14 3
6 15 7
9 16 6
8 17 7
16 18 9
16 19 3
18 20 10
11 3 2
17 1 1
16 2 2
15 1 1
10 3 2
9 1 2
19 2 1
6 1 2
7 3 1
17 3 2
15 3 2
8 6 2
5 1 2
8 1 2
12 1 1
12 7 1
4 1 2
18 2 1
11 7 1
14 1 1
18 1 1
18 9 1
10 6 1
14 3 2
20 2...

output:

2

result:

ok answer is '2'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

20 50
1 2 18468
1 3 26501
3 4 15725
3 5 29359
3 6 24465
6 7 28146
7 8 16828
2 9 492
8 10 11943
8 11 5437
8 12 14605
3 13 154
7 14 12383
6 15 18717
9 16 19896
8 17 21727
16 18 11539
16 19 19913
18 20 26300
11 3 2
17 1 1
16 2 2
15 1 1
10 3 2
9 1 2
19 2 1
6 1 2
7 3 1
17 3 2
15 3 2
8 6 2
5 1 2
8 1 2
12 ...

output:

1

result:

ok answer is '1'

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3620kb

input:

100 150
1 2 184676335
1 3 191705725
1 4 293606963
1 5 57078146
2 6 168279962
6 7 29961943
5 8 54392392
5 9 39020154
5 10 123837422
7 11 197199896
3 12 217274772
7 13 18709913
6 14 263007036
11 15 287053812
3 16 303347674
9 17 151417712
17 18 68705548
15 19 326652758
12 20 128598724
2 21 275290779
11...

output:

1

result:

wrong answer expected '3', found '1'