QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#784947 | #7177. Many Many Cycles | hotdogseller | WA | 0ms | 3860kb | C++14 | 3.1kb | 2024-11-26 16:24:38 | 2024-11-26 16:24:38 |
Judging History
answer
#include<bits/stdc++.h>
#define maxn 5005
#define INF 1e18
#define pii pair<int,int>
#define int long long
using namespace std;
inline int read(){
int lre=0,f=1;
char c=getchar();
while(!isdigit(c)){
if(c=='-'){
f=-1;
}
c=getchar();
}
while(isdigit(c)){
lre=(lre<<3)+(lre<<1)+(c-'0');
c=getchar();
}
return lre*f;
}
struct edge{
int to;
int val;
int next;
}e[2*maxn];
int e_cnt=1,head[maxn];
void add(int u,int v,int w){
e[e_cnt].to=v;
e[e_cnt].val=w;
e[e_cnt].next=head[u];
head[u]=e_cnt++;
}
int gcd(int a,int b){
return (b==0)?a:gcd(b,a%b);
}
int n,m,res=0;
vector<pair<pii,int> > E;
int root[maxn];
int f_f(int x){
if(root[x]!=x){
root[x]=f_f(root[x]);
}
return root[x];
}
int dfs_cnt=1;
int f[maxn];
int mapping[maxn],q_map[maxn],depth[maxn],cnt[maxn];
int sz[maxn],son[maxn],rt[maxn];
void dfs1(int x,int fa){
sz[x]=1;cnt[x]=cnt[fa]+1;
f[x]=fa;
for(int i=head[x];i;i=e[i].next){
int v=e[i].to,w=e[i].val;
if(v==f[x])continue;
depth[v]=depth[x]+w;
dfs1(v,x);
sz[x]+=sz[v];
if(sz[v]>sz[son[x]])son[x]=v;
}
// cout<<"depth["<<x<<"]="<<depth[x]<<endl;
}
void dfs2(int x,int RT){
rt[x]=RT;mapping[dfs_cnt]=x;
q_map[x]=dfs_cnt++;
if(son[x])dfs2(son[x],RT);
for(int i=head[x];i;i=e[i].next){
int v=e[i].to;
if(v==son[x]||v==f[x])continue;
dfs2(v,v);
}
}
int lca(int u,int v){
while(rt[u]!=rt[v]){
if(cnt[rt[u]]<cnt[rt[v]])swap(u,v);
u=f[rt[u]];
}
if(cnt[u]>cnt[v])return v;
return u;
}
int dist(int u,int v){
return depth[u]+depth[v]-2*depth[lca(u,v)];
}
bool on(int u,int v,int x){
return lca(lca(u,v),x)==lca(u,v)&&(lca(x,u)==x||lca(x,v)==x);
}//x是不是在u,v上
signed main(){
// freopen("1.in","r",stdin);
n=read();m=read();
for(int i=1;i<=n;i++)root[i]=i;
for(int i=1;i<=m;i++){
int a=read(),b=read(),c=read();
if(f_f(a)!=f_f(b)){
root[f_f(a)]=f_f(b);
add(a,b,c);add(b,a,c);
}else{
// cout<<"push!"<<endl;
E.push_back(make_pair(make_pair(a,b),c));
}
}
for(int i=1;i<=n;i++){
if(!q_map[i]){
dfs1(i,0);dfs2(i,i);
}
}
for(pair<pair<int,int>,int> p:E){
res=gcd(res,dist(p.first.first,p.first.second)+p.second);
}
// cout<<"E.size()="<<E.size()<<endl;
// cout<<"res="<<res<<endl;
for(int i=0;i<E.size();++i){
for(int j=i+1;j<E.size();++j){
int a=E[i].first.first,b=E[i].first.second,c=E[j].first.first,d=E[j].first.second;
int l1=dist(a,b)+E[i].second,l2=dist(c,d)+E[j].second;
int f1=lca(a,b),f2=lca(c,d);
int e,w;
if(on(a,b,f2)){
e=f2;
if(lca(e,a)==a){
w=dist(a,e);
}else{
w=dist(b,e);
}
}else if(on(c,d,f1)){
e=f1;
if(lca(e,c)==c){
w=dist(c,e);
}else{
w=dist(d,e);
}
}
// cout<<"l1="<<l1<<" l2="<<l2<<" w="<<w<<endl;
res=gcd(res,l1);res=gcd(res,l2);
res=gcd(res,2*w);
}
}
printf("%lld\n",res);
return 0;
}
/*
5 10
2 4 1
4 5 7
2 3 3
3 1 7
5 1 10
3 5 9
1 2 10
2 4 6
4 2 5
4 2 8
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3860kb
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: 3776kb
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: 3860kb
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: 3840kb
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: 0ms
memory: 3856kb
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'