QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#127083#6634. Central Subset45645A#RE 1ms11848kbC++142.1kb2023-07-19 12:52:432023-07-19 12:53:59

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-19 12:53:59]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:11848kb
  • [2023-07-19 12:52:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
inline int read()
{
	char ch;
	while((ch=getchar())<'0'||ch>'9');
	int res=ch-'0';
	while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
	return res;
}	
const int N=200100;
struct Edge{
	int nxt,to;
}e[N<<1];
int h[N],e_num;
void add(int from,int to)
{
	e[++e_num].nxt=h[from];
	e[e_num].to=to;
	h[from]=e_num;
}
int n,K;
struct BCJ{
	int f[N];
	void pre()
	{
		for(int i=1;i<=n;i++) 
			f[i]=i;
	}
	int find(int x){ return f[x]==x?x:(f[x]=find(f[x])); }
	bool mrge(int x,int y)
	{
		x=find(x),y=find(y);
		if(x==y) return 0;
		f[x]=y;
		return 1;
	}
}B;
int vis[N],nxt[2][N],cnt[2],now;
int rt,mx,dp[2][N],son[N];
void dfs1(int u,int fa)
{
	dp[0][u]=1;son[u]=u;
	for(int i=h[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(vis[v]||v==fa) continue;
		dfs1(v,u);
		int d=dp[0][v]+1;
		if(d>dp[0][u]) dp[1][u]=dp[0][u],dp[0][u]=d,son[u]=v;
		else if(d>dp[1][u]) dp[1][u]=d;
	}
}
void dfs2(int u,int fa)
{
	if(dp[0][u]>mx) rt=u,mx=dp[0][u];
	for(int i=h[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(vis[v]||v==fa) continue;
		int d=dp[son[u]==v][u]+1;
		if(d>dp[0][v]) dp[1][v]=dp[0][v],dp[0][v]=d,son[v]=u;
		else if(d>dp[1][v]) dp[1][v]=d;
		dfs2(v,u);
	}
}
void dfs_vis(int u,int dep)
{
	if(dep>K) return void(nxt[now^1][++cnt[now^1]]=u);
	vis[u]=1;
	for(int i=h[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(vis[v]) continue;
		dfs_vis(v,dep+1);
	}
}
int res[N],ans;
int main()
{
	int T=read();
	while(T--)
	{
		n=read();
		int m=read();
		B.pre();
		e_num=0;
		ans=0;
		for(int i=1;i<=n;i++) vis[i]=h[i]=0;
		for(int i=1;i<=m;i++)
		{
			int u=read(),v=read();
			if(B.mrge(u,v)) add(u,v),add(v,u);
		}
		K=sqrt(n);
		if(K*K!=n) K++;
		nxt[now=0][cnt[0]=1]=1;
		while(cnt[now])
		{
			cnt[now^1]=0;
			for(int i=1;i<=cnt[now];i++)
			{
				dfs1(nxt[now][i],0);
				mx=0;	
				dfs2(nxt[now][i],0);
				res[++ans]=rt;
				dfs_vis(rt,0);
			}
			now^=1;	
		}
		assert(ans<=K);
		printf("%d\n",ans);
		for(int i=1;i<=ans;i++) printf("%d ",res[i]);
		printf("\n");
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

result:

ok correct (2 test cases)

Test #2:

score: -100
Dangerous Syscalls

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:


result: