QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#446737#7177. Many Many CyclesCelestialCoderWA 7ms24756kbC++201.8kb2024-06-17 15:34:522024-06-17 15:34:53

Judging History

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

  • [2024-06-17 15:34:53]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:24756kb
  • [2024-06-17 15:34:52]
  • 提交

answer

#include <bits/stdc++.h>
#ifdef SHARAELONG
#include "../../cpp-header/debug.hpp"
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int MAX_N = 5e3 + 1;

vector<pii> adj[MAX_N];
vector<int> chd[MAX_N];
bool visited[MAX_N];
int d[MAX_N];
ll h[MAX_N];
int lca[MAX_N][MAX_N];
vector<pii> back_edges;
ll ans = 0;

void dfs(int x) {
    visited[x] = true;
    chd[x].push_back(x);
    for (auto[y, c]: adj[x]) {
        if (!visited[y]) {
            d[y] = d[x]+1;
            h[y] = h[x]+c;
            dfs(y);
            for (int z: chd[x]) {
                for (int w: chd[y]) lca[z][w] = x;
            }
            for (int z: chd[y]) chd[x].push_back(z);
        } else if (d[x] < d[y]) {
            ans = gcd(ans, h[y]-h[x]+c);
            back_edges.push_back({ x, y });
        }
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i=0; i<m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        adj[a].push_back({ b, c });
        adj[b].push_back({ a, c });
    }

    for (int i=1; i<=n; ++i) {
        if (!visited[i]) {
            back_edges.clear();
            dfs(i);
            for (int i=0; i<back_edges.size(); ++i) {
                for (int j=i+1; j<back_edges.size(); ++j) {
                    auto[x1, y1] = back_edges[i];
                    auto[x2, y2] = back_edges[j];
                    int l = lca[y1][y2];
                    int x = (d[x1] < d[x2] ? x2 : x1);
                    ll inter = h[l] - h[x];
                    if (d[x] <= d[l]) ans = gcd(inter*2, ans);
                }
            }
        }
    }
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3628kb

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: 1ms
memory: 3740kb

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: 1ms
memory: 5788kb

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: 0
Accepted
time: 1ms
memory: 5968kb

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:

3

result:

ok answer is '3'

Test #6:

score: 0
Accepted
time: 1ms
memory: 6016kb

input:

100 130
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:

7

result:

ok answer is '7'

Test #7:

score: 0
Accepted
time: 1ms
memory: 5904kb

input:

100 200
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:

4

result:

ok answer is '4'

Test #8:

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

input:

100 190
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:

2

result:

ok answer is '2'

Test #9:

score: 0
Accepted
time: 5ms
memory: 22956kb

input:

1000 1500
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
...

output:

3

result:

ok answer is '3'

Test #10:

score: 0
Accepted
time: 2ms
memory: 24756kb

input:

1000 1500
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
...

output:

1

result:

ok answer is '1'

Test #11:

score: 0
Accepted
time: 3ms
memory: 24688kb

input:

1000 1600
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
...

output:

1

result:

ok answer is '1'

Test #12:

score: 0
Accepted
time: 1ms
memory: 5952kb

input:

100 190
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:

4

result:

ok answer is '4'

Test #13:

score: 0
Accepted
time: 7ms
memory: 24696kb

input:

1000 3000
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
...

output:

2

result:

ok answer is '2'

Test #14:

score: 0
Accepted
time: 4ms
memory: 24556kb

input:

1000 1400
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
...

output:

1

result:

ok answer is '1'

Test #15:

score: -100
Wrong Answer
time: 2ms
memory: 22916kb

input:

1000 1500
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
...

output:

8

result:

wrong answer expected '2', found '8'