QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#884299#7177. Many Many CyclesqojdanRE 1ms3968kbC++141.9kb2025-02-05 23:37:472025-02-05 23:37:53

Judging History

This is the latest submission verdict.

  • [2025-02-05 23:37:53]
  • Judged
  • Verdict: RE
  • Time: 1ms
  • Memory: 3968kb
  • [2025-02-05 23:37:47]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
constexpr int N=10004;
mt19937_64 rnd(time(0));
bool a1;
int gcd(int x,int y){
    for(int z;y;x=z){z=y;y=x%y;}
    return x;
}
int n,m,f[N][21],dfn[N],par[N],e1[N],e2[N],e3[N],cdfn;
ll ans,dep[N];
ull col[N];
unordered_map<ull,ll>mp;
vector<pair<int,int> >v[N];
int lca(int x,int y){
    if(dep[x]<dep[y])swap(x,y);
    for(int i=18;i>=0;--i){
        if(dep[f[x][i]]>=dep[y])x=f[x][i];
    }
    if(x==y)return x;
    for(int i=18;i>=0;--i){
        if(f[x][i]!=f[y][i]){
            x=f[x][i];
            y=f[y][i];
        }
    }
    return par[x];
}
void dfs(int x,int p){
    dfn[++cdfn]=x;
    par[x]=f[x][0]=p;
    for(int i=0;f[x][i];++i)f[x][i+1]=f[f[x][i]][i];
    for(unsigned i=0;i<v[x].size();++i){
        if(dfn[v[x][i].fi])continue;
        dep[v[x][i].fi]=dep[x]+v[x][i].se;
        dfs(v[x][i].fi,x);
    }
}
bool a2;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cerr<<"static memory::"<<&a1-&a2<<'\n';
    cin>>n>>m;
    for(int i=1;i<=m;++i){
        int &x=e1[i],&y=e2[i],&z=e3[i];
        cin>>x>>y>>z;
        v[x].emplace_back(y,z);
        v[y].emplace_back(x,z);
    }
    dfs(1,0);
    for(int i=1;i<=m;++i){
        int x=e1[i],y=e2[i],z=e3[i];
        if(par[y]!=x&&par[x]!=y){
            ull r=rnd();
            mp[r]+=z;
            col[x]^=r;
            col[y]^=r;
            ans=gcd(ans,dep[x]+dep[y]-2*dep[lca(x,y)]+z);
        }
    }
    for(int i=n;i>=1;--i){
        col[par[dfn[i]]]^=col[dfn[i]];
    }
    for(int i=2;i<=n;++i){
        mp[col[i]]+=dep[i]-dep[par[i]];
    }
    for(auto it=mp.begin();it!=mp.end();++it){
        ans=gcd(ans,2*it->se);
    }
    cout<<ans<<'\n';
    cerr<<"time::"<<clock()<<'\n';
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3968kb

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

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
Runtime Error

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:


result: