QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#386029#7177. Many Many CyclesxlaoWA 0ms3980kbC++141.6kb2024-04-11 11:12:412024-04-11 11:12:42

Judging History

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

  • [2024-04-11 11:12:42]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3980kb
  • [2024-04-11 11:12:41]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define ll long long
const int N=5005, M=1e4+5;

int n,m,bj[M];
struct Edge {int x,y,z;} e[M];

struct edge {int nex,to,v;} a[M<<1];
int h[N],tot=1;
void add(int x,int y,int z)
{
	a[++tot]=(edge){h[x],y,z}, h[x]=tot;
	a[++tot]=(edge){h[y],x,z}, h[y]=tot;
}

int sq[N<<1],in[N],ti; ll dep[N];
void dfs0(int u)
{
	sq[in[u]=++ti]=u;
	for(int i=h[u];i;i=a[i].nex) if(!in[a[i].to])
	{
		bj[i/2]=1;
		int v=a[i].to;
		dep[v]=dep[u]+a[i].v;
		dfs0(v); sq[++ti]=u;
	}
}
#define _min(x,y) (dep[x]<dep[y]?x:y)
int st[N<<1][15],lg[N<<1];
void build_ST()
{
	lg[0]=-1; for(int i=1;i<=ti;++i) st[i][0]=sq[i], lg[i]=lg[i>>1]+1;
	for(int i=1;(1<<i)<=ti;++i)
	for(int l=1;l+(1<<i)-1<=ti;++l)
		st[l][i] = _min(st[l][i-1], st[l+(1<<(i-1))][i-1]);
}
int LCA(int x,int y)
{
	x=in[x], y=in[y];
	if(x>y) swap(x,y);
	int k=lg[y-x+1];
	return _min(st[x][k], st[y-(1<<k)+1][k]);
}

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1,x,y,z;i<=m;++i)
		scanf("%d %d %d",&x,&y,&z), add(x,y,z), e[i]=(Edge){x,y,z};
	dfs0(1);
	build_ST();
	ll ans=0;
	for(int i=1;i<=m;++i) if(!bj[i])
	{
		int u=e[i].x, v=e[i].y, w=e[i].z;
		if(dep[u]<dep[v]) swap(u,v);
		if(!ans) ans = dep[u]-dep[v]+w;
		else ans = __gcd(ans, dep[u]-dep[v]+w);
		for(int j=i+1;j<=m;++j) if(!bj[j])
		{
			int x=e[j].x, y=e[j].y, z=e[j].z;
			if(dep[x]<dep[y]) swap(x,y);
			
			if(LCA(u,y)==y)
			{
				int dw=LCA(u,x), up=LCA(v,y);
				ans = __gcd(ans, dep[u]-dep[v]+w + dep[x]-dep[y]+z - (dep[dw]-dep[up])*2);
			}
		}
	}
	printf("%lld\n",ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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:

wrong answer expected '1', found '-1'