QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#211302#7177. Many Many CycleszhouhuanyiWA 1ms4300kbC++142.2kb2023-10-12 14:38:492023-10-12 14:38:49

Judging History

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

  • [2023-10-12 14:38:49]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4300kb
  • [2023-10-12 14:38:49]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#define N 10000
using namespace std;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
long long gcd(long long x,long long y)
{
	if (!y) return x;
	return gcd(y,x%y);
}
struct reads
{
	int num,data;
};
struct rds
{
	int x,y,data;
};
rds tong[N+1];
int n,m,length,lg[N+1],fa[N+1],st[N+1],leng,lengs,ps[N+1],dfn[N+1],rev[N+1],ST[N+1][21];
long long d,depth[N+1];
vector<reads>E[N+1];
void add(int x,int y,int z)
{
	E[x].push_back((reads){y,z}),E[y].push_back((reads){x,z});
	return;
}
void dfs(int x)
{
	dfn[x]=++leng,rev[dfn[x]]=x;
	for (int i=0;i<E[x].size();++i)
	{
		if (x<E[x][i].num) tong[++length]=(rds){x,E[x][i].num,E[x][i].data};
		if (!dfn[E[x][i].num]) fa[E[x][i].num]=x,depth[E[x][i].num]=depth[x]+E[x][i].data,dfs(E[x][i].num);
	}
	return;
}
void dfs2(int x)
{
	st[++lengs]=x,ps[x]=lengs;
	for (int i=0;i<E[x].size();++i)
		if (fa[E[x][i].num]==x)
			dfs2(E[x][i].num),st[++lengs]=x;
	return;
}
int query(int l,int r)
{
	int lw=lg[r-l+1];
	return min(ST[l][lw],ST[r-(1<<lw)+1][lw]);
}
int lca(int x,int y)
{
	x=ps[x],y=ps[y];
	if (x>y) swap(x,y);
	return rev[query(x,y)];
}
int main()
{
	int x,y,z;
	for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
	n=read(),m=read();
	for (int i=1;i<=m;++i) x=read(),y=read(),z=read(),add(x,y,z);
	for (int i=1;i<=n;++i)
		if (!dfn[i])
		{
			leng=lengs=length=0,dfs(i),dfs2(i);
			for (int j=1;j<=lengs;++j) ST[j][0]=dfn[st[j]];
			for (int j=1;j<=lg[lengs];++j)
				for (int k=1;k+(1<<j)-1<=lengs;++k)
					ST[k][j]=min(ST[k][j-1],ST[k+(1<<(j-1))][j-1]);
			for (int j=1;j<=length;++j)
			{
				if (depth[tong[j].x]>depth[tong[j].y]) swap(tong[j].x,tong[j].y);
				if (fa[tong[j].y]!=tong[j].x) d=gcd(d,depth[tong[j].y]-depth[tong[j].x]+tong[j].data);
			}
			for (int j=1;j<=length;++j)
				if (fa[tong[j].y]!=tong[j].x)
					for (int k=j+1;k<=length;++k)
						if (fa[tong[k].y]!=tong[k].x)
							d=gcd(d,max(depth[lca(tong[j].y,tong[k].y)]-max(depth[tong[j].x],depth[tong[k].y]),0ll));
		}
	printf("%lld\n",d);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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

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

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

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

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

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:

wrong answer expected '2', found '4'