QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#316682#7177. Many Many CyclesMinhhoWA 34ms5484kbC++172.6kb2024-01-28 01:58:242024-01-28 01:58:24

Judging History

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

  • [2024-01-28 01:58:24]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:5484kb
  • [2024-01-28 01:58:24]
  • 提交

answer

#define taskname "C"
#include <bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define ff first
#define ss second

using namespace std;
const int maxn = 1e4 + 10;
vector<ii> adj[maxn];
int pa[maxn][20], dep[maxn], dis[maxn], p[maxn], n, m;
array<int, 4> e[maxn];

inline int fp(int u) {return !p[u] ? u : p[u] = fp(p[u]);}

inline bool uni(int u, int v)
{
    if ((u = fp(u)) == (v = fp(v))) return 0;
    return p[v] = u;
}

void DFS(int u = 1, int par = 0)
{
    for (auto [v, c]: adj[u]) if (v != par)
    {
        dep[v] = dep[u] + 1;
        dis[v] = dis[u] + c;
        pa[v][0] = u;
        DFS(v, u);
    }
}

inline int lca(int u, int v)
{
    if (dep[u] > dep[v]) swap(u, v);
    int dif = dep[v] - dep[u];
    for (int i=19; i>=0; i--) if (dif >> i & 1) v = pa[v][i];
    if (v == u) return u;
    for (int i=19; i>=0; i--) if (pa[v][i] != pa[u][i]) v = pa[v][i], u = pa[u][i];
    return pa[u][0];
}

inline int cal(int u, int v) {return dis[u] + dis[v] - 2 * dis[lca(u, v)];}

inline int sol(int m, int u, int v, int add)
{
    if (dep[u] > dep[v]) swap(u, v);
    int l = lca(u, v);
    if (l == u && lca(m, v) == m) return 0;
    else if ((lca(m, v) == m || lca(m, u) == m) && lca(m, l) != m) return 0;
    else if (m == l) return 0;
    return add + cal(u, v);
}

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cin>>n>>m;
    for (int i=1; i<=m; i++) cin>>e[i][1]>>e[i][2]>>e[i][0], e[i][3] = i;
    shuffle(e+1, e+m+1, rng);
    vector<int> ve;
    for (int i=1; i<=m; i++) if (uni(e[i][1], e[i][2]))
    {
        adj[e[i][1]].emplace_back(e[i][2], e[i][0]);
        adj[e[i][2]].emplace_back(e[i][1], e[i][0]);
    } else ve.emplace_back(i);

    DFS();
    for (int i=1; i<20; i++)
        for (int j=1; j<=n; j++)
            pa[j][i] = pa[pa[j][i-1]][i-1];

    int ans = 0;
    for (int i=0; i<ve.size()-1; i++)
        for (int j=i+1; j<ve.size(); j++)
        {
            auto [c1, u1, v1, id1] = e[ve[i]];
            auto [c2, u2, v2, id2] = e[ve[j]];
            int add = c1 + c2;
            if (u1 == u2) ans = __gcd(ans, sol(u1, v1, v2, add));
            else if (u1 == v2) ans = __gcd(ans, sol(u1, u2, v1, add));
            else if (v1 == u2) ans = __gcd(ans, sol(v1, u1, v2, add));
            else if (v1 == v2) ans = __gcd(ans, sol(v1, u1, u2, add));
        }
    for (int i: ve)
    {
        auto [c, u, v, id] = e[i];
        ans = __gcd(ans, c + cal(u, v));
    }
    cout<<ans;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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

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

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

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

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

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

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

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: 2ms
memory: 4228kb

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

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: 13ms
memory: 4220kb

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

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: 0
Accepted
time: 2ms
memory: 4108kb

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:

2

result:

ok answer is '2'

Test #16:

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

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:

ok answer is '8'

Test #17:

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

input:

2000 2500
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

12

result:

ok answer is '12'

Test #18:

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

input:

2000 2500
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

2

result:

ok answer is '2'

Test #19:

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

input:

2000 2500
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

13

result:

ok answer is '13'

Test #20:

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

input:

2000 2500
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

1

result:

ok answer is '1'

Test #21:

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

input:

2000 3000
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

3

result:

ok answer is '3'

Test #22:

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

input:

2000 3000
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

1

result:

ok answer is '1'

Test #23:

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

input:

2000 2500
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

1

result:

ok answer is '1'

Test #24:

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

input:

2000 3500
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

2

result:

ok answer is '2'

Test #25:

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

input:

2000 4000
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

4

result:

ok answer is '4'

Test #26:

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

input:

2000 2300
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

1

result:

ok answer is '1'

Test #27:

score: 0
Accepted
time: 6ms
memory: 4744kb

input:

3000 5000
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

1

result:

ok answer is '1'

Test #28:

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

input:

3000 3700
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

13

result:

ok answer is '13'

Test #29:

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

input:

3000 3700
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

1

result:

ok answer is '1'

Test #30:

score: 0
Accepted
time: 12ms
memory: 5468kb

input:

5000 8000
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

3

result:

ok answer is '3'

Test #31:

score: 0
Accepted
time: 12ms
memory: 5260kb

input:

5000 8000
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

1

result:

ok answer is '1'

Test #32:

score: 0
Accepted
time: 8ms
memory: 5240kb

input:

5000 7200
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

5

result:

ok answer is '5'

Test #33:

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

input:

5000 7200
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

1

result:

ok answer is '1'

Test #34:

score: 0
Accepted
time: 6ms
memory: 5448kb

input:

5000 8000
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

6

result:

ok answer is '6'

Test #35:

score: 0
Accepted
time: 12ms
memory: 5324kb

input:

5000 8000
1 2 22794613
1 3 23670271
1 4 36254735
1 5 7068116
2 6 20774480
6 7 3707773
5 8 6740416
5 9 4815222
5 10 15296810
7 11 24351908
3 12 26824656
7 13 2326259
6 14 32470002
11 15 35443314
3 16 37448596
9 17 18691706
17 18 8500660
15 19 40337666
12 20 15876730
2 21 33971565
11 22 3767381
17 23 ...

output:

2

result:

ok answer is '2'

Test #36:

score: 0
Accepted
time: 34ms
memory: 5464kb

input:

5000 10000
1 2 4216811
1 3 4386257
1 4 6720587
1 5 1328886
2 6 3846518
6 7 694803
5 8 1271800
5 9 889810
5 10 2840518
7 11 4515600
3 12 4968300
7 13 446045
6 14 6013208
11 15 6568096
3 16 6933598
9 17 3459860
17 18 1591452
15 19 7479694
12 20 2940576
2 21 6277391
11 22 714171
17 23 95771
2 24 205804...

output:

4

result:

ok answer is '4'

Test #37:

score: -100
Wrong Answer
time: 34ms
memory: 5456kb

input:

5000 10000
1 2 4216811
1 3 4386257
1 4 6720587
1 5 1328886
2 6 3846518
6 7 694803
5 8 1271800
5 9 889810
5 10 2840518
7 11 4515600
3 12 4968300
7 13 446045
6 14 6013208
11 15 6568096
3 16 6933598
9 17 3459860
17 18 1591452
15 19 7479694
12 20 2940576
2 21 6277391
11 22 714171
17 23 95771
2 24 205804...

output:

4

result:

wrong answer expected '2', found '4'