QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#638933#7177. Many Many Cycleslichenyu_acWA 0ms3960kbC++141.8kb2024-10-13 17:21:392024-10-13 17:21:39

Judging History

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

  • [2024-10-13 17:21:39]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3960kb
  • [2024-10-13 17:21:39]
  • 提交

answer

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=5e3+10,M=N*2;

int head[N],ver[M],nxt[M],tot=1;
ll edge[M];
void add(int x,int y,ll z){
    ver[++tot]=y,edge[tot]=z,nxt[tot]=head[x],head[x]=tot;
}

tuple<int,int,ll>e[N],sta[N];
int fa[N],top;
int find(int x){
    return fa[x]==x?x:fa[x]=find(fa[x]);
}

int n,m;
ll dis[N],dep[N],f[N][25],ans[N];
bool v[N];
void dfs(int x,int fa){
    v[x]=1;
    f[x][0]=fa;
    for(int i=1;i<20;i++)
        f[x][i]=f[f[x][i-1]][i-1];
    for(int i=head[x];i;i=nxt[i]){
        int y=ver[i];
        if(y==fa)continue;
        dep[y]=dep[x]+1,dis[y]=dis[x]+edge[i];
        dfs(y,x);
    }
}

int LCA(int x,int y){
    if(dep[x]<dep[y])swap(x,y);
    for(int i=19;~i;i--)
        if(dep[f[x][i]]>=dep[y])
            x=f[x][i];
    if(x==y)return x;
    for(int i=19;~i;i--)
        if(f[x][i]!=f[y][i])
            x=f[x][i],y=f[y][i];
    return f[x][0];
}

ll dist(int x,int y){
    return dis[x]+dis[y]-2*dis[LCA(x,y)];
}

ll gcd(ll a,ll b){
    return b?a:gcd(b,a%b);
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        e[i]=make_tuple(x,y,z);
    }
    for(int i=1;i<=n;i++)fa[i]=i;
    for(int i=1;i<=m;i++){
        auto [x,y,z]=e[i];
        if(find(x)==find(y)){
            sta[++top]=e[i];
        }else{
            fa[find(y)]=find(x);
            add(x,y,z),add(y,x,z);
            // printf("%d %d %lld\n",x,y,z);
        }
    }
    for(int i=1;i<=n;i++)
        if(!v[i])dep[i]=1,dfs(i,0);
    for(int i=1;i<=top;i++){
        auto [x,y,z]=sta[i];
        ans[i]=z+dist(x,y);
    }
    ll tmp=ans[1];
    for(int i=2;i<=top;i++)
        tmp=gcd(tmp,ans[i]);
    printf("%lld\n",tmp);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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: -100
Wrong Answer
time: 0ms
memory: 3960kb

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:

28

result:

wrong answer expected '2', found '28'