QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187724#6634. Central SubsetrsjWA 31ms14740kbC++141.2kb2023-09-24 21:20:182023-09-24 21:20:18

Judging History

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

  • [2023-09-24 21:20:18]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:14740kb
  • [2023-09-24 21:20:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
struct edge {
	int to;
	edge *nex;
}*head[N];

int f[N]; 
int find(int x) {
	if(f[x]==x) return x;
	return f[x]=find(f[x]);
}
void uni(int x,int y) {
	f[find(x)]=find(y);
}
bool check(int x,int y) {
	return find(x)==find(y);
}
void add(int u,int v) {
	edge *cur=new edge;
	cur->to=v;
	cur->nex=head[u];
	head[u]=cur;
}
int h[N],b;
int maxn;
vector<int> s[N];
void dfs(int u,int fa) {
	h[u]=h[fa]+1;
	s[h[u]].push_back(u);
	maxn=max(maxn,h[u]);
	for(edge *cur=head[u];cur;cur=cur->nex) {
		if(cur->to==fa) continue;
		dfs(cur->to,u);
	}	
}

void get() {
	maxn=0;
	int n,m,i,u,V;
	cin>>n>>m;
	b=ceil(sqrt(n));
	for(i=1;i<=n;i++) head[i]=0,f[i]=i;
	for(i=1;i<=n;i++) s[i].clear();
	for(i=1;i<=m;i++) {
		cin>>u>>V;
		if(check(u,V)) continue;
		uni(u,V),add(u,V),add(V,u);
	} dfs(1,0); vector<int> ans; ans.push_back(1);
	for(i=maxn;i>=b+1;i-=b) {
		for(int x:s[i]) ans.push_back(x);
	} cout<<ans.size()<<endl;
	for(i=0;i<ans.size();i++) cout<<ans[i]<<" \n"[i==ans.size()-1];
}
int main() {
	// freopen("1.in","r",stdin);
	int T; cin>>T;
	while(T--) get();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
1 4
2
1 6

result:

ok correct (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 31ms
memory: 14740kb

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:

4
1 15 11 7
3
1 5 6
3
1 10 6
1
1
1
1
3
1 9 6
1
1
3
1 14 8
6
1 16 7 8 9 10
1
1
4
1 20 15 10
2
1 8
3
1 6 10
3
1 17 16
1
1
4
1 15 11 7
4
1 6 5 7
1
1
2
1 9
1
1
2
1 4
2
1 8
3
1 8 14
2
1 18
1
1
4
1 15 11 7
3
1 6 4
5
1 9 16 5 12
2
1 3
1
1
5
1 21 16 11 6
4
1 9 13 11
3
1 8 14
5
1 16 7 8 9
1
1
2
1 6
3
1 7 6
3...

result:

wrong answer Integer 6 violates the range [1, 4] (test case 9)