QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187713#6634. Central SubsetqzhwlzyWA 15ms7904kbC++141.2kb2023-09-24 21:04:072023-09-24 21:04:07

Judging History

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

  • [2023-09-24 21:04:07]
  • 评测
  • 测评结果:WA
  • 用时:15ms
  • 内存:7904kb
  • [2023-09-24 21:04:07]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cmath>
#define maxn 200005
using namespace std;
int T,n,m,u,v,dep[maxn],maxpos,s[maxn],top,ans[maxn],num=0; bool flag=0;
struct node{int to,nex;}a[maxn*2]; int head[maxn],cnt=0;
void add(int from,int to){a[++cnt].to=to; a[cnt].nex=head[from]; head[from]=cnt;}
int f[maxn]; int getfa(int x){return f[x]==x?x:f[x]=getfa(f[x]);}
void dfs1(int p,int fa){
	dep[p]=dep[fa]+1; maxpos=(dep[p]>dep[maxpos]?p:maxpos);
	for(int i=head[p];i;i=a[i].nex) if(a[i].to!=fa) dfs1(a[i].to,p);
}
void dfs2(int p,int fa){
	s[++top]=p; if(p==maxpos){flag=1; return;}
	for(int i=head[p];i;i=a[i].nex){if(a[i].to!=fa) dfs2(a[i].to,p); if(flag) return; top--;}
}
int main(){
	scanf("%d",&T); while(T--){
		scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) f[i]=i,head[i]=0; cnt=num=maxpos=flag=top=0;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&u,&v); int r1=getfa(u),r2=getfa(v); if(r1==r2) continue;
			f[r2]=r1; add(u,v); add(v,u);
		} dfs1(1,0); dfs2(1,0);
		int thres=int(sqrt(n)); if(thres*thres!=n) thres++; while(top>0){
			for(int i=1;i<=thres;i++){top--; if(top==0) top++;} ans[++num]=s[top]; if(top==1) break;
		} printf("%d\n",num); for(int i=1;i<=num;i++) printf("%d ",ans[i]); printf("\n");
	} return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
4 3
1 2
2 3
3 4
6 7
1 2
2 3
3 1
1 4
4 5
5 6
6 4

output:

2
2 1 
1
1 

result:

ok correct (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 15ms
memory: 7904kb

input:

10000
15 14
13 12
5 4
9 8
11 12
15 14
10 9
14 13
2 3
2 1
6 5
10 11
3 4
7 6
8 7
6 5
2 1
2 4
4 6
2 3
3 5
10 9
8 3
9 4
5 6
5 10
3 2
5 4
2 7
1 2
4 3
2 1
2 1
2 1
2 1
9 8
9 8
5 4
1 2
6 5
3 4
3 2
7 8
7 6
2 1
1 2
14 13
3 10
5 6
2 9
11 4
2 3
2 1
8 7
13 6
5 4
5 12
6 7
4 3
7 14
16 15
2 3
2 1
6 10
6 9
6 4
9 11
...

output:

2
7 1 
1
1 
1
3 
1
1 
1
1 
2
2 1 
1
1 
1
1 
0

1
1 
2
8 1 
1
1 
1
1 
0

1
1 
2
8 1 
1
1 
1
1 
1
1 
1
1 
1
1 
1
8 
1
1 
1
1 
1
1 
2
9 1 
1
1 
1
1 
1
1 
1
1 
3
9 2 1 
1
1 
1
1 
1
6 
1
1 
1
1 
1
1 
1
1 
1
1 
1
1 
2
5 1 
1
1 
1
7 
1
1 
1
1 
1
1 
2
2 1 
1
1 
1
1 
1
1 
2
5 1 
0

1
6 
1
1 
1
1 
2
6 1 
1
1 ...

result:

wrong answer Condition failed: "getMaxBfsDist(n, subset) <= csqrtn" (test case 1)