QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#277057#6745. Delete the Treeucup-team958TL 1ms3912kbC++141.4kb2023-12-06 15:02:262023-12-06 15:02:26

Judging History

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

  • [2023-12-06 15:02:26]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3912kb
  • [2023-12-06 15:02:26]
  • 提交

answer

#include<bits/stdc++.h>
#define il inline
using namespace std;
const int maxn=510;
il int read(){
	int x=0;
	char c=getchar();
	for(;!(c>='0'&&c<='9');c=getchar());
	for(;c>='0'&&c<='9';c=getchar())
		x=(x<<1)+(x<<3)+c-'0';
	return x;
}
vector<int>ans[maxn];
int cnt;
struct edge{
	int v,to;
}e[maxn<<1];
int head[maxn],ecnt;
void addedge(int u,int v){
	e[++ecnt].v=v,e[ecnt].to=head[u],head[u]=ecnt;
}
int n,tag[maxn];
int deg[maxn];
set<int>s[maxn];
int fa[maxn];
void Erase(int x){
	deg[fa[x]]--;
	for(auto y:s[x])
		if(y!=fa[x])
			fa[y]=fa[x],s[fa[x]].insert(y),deg[fa[x]]++;
}
void dfs(int fath,int x){
	fa[x]=fath;
	for(auto y:s[x])
		if(y!=fa[x]) dfs(x,y);
}
int main(){
	n=read();
	for(int i=1;i<n;i++){
		int x=read(),y=read();
		s[x].insert(y);
		s[y].insert(x);
		deg[x]++,deg[y]++;
	}dfs(0,1);
	int tot=0;
	while(tot<n-1){
		++cnt;
		for(int i=2;i<=n;i++)
			if(!tag[i]&&deg[i]==1) tag[i]=1;
		for(int i=2;i<=n;i++)
			if(!tag[i]&&deg[i]==2){
				bool fl=1;
				for(auto x:s[i])
					if(tag[x]) fl=0;
				if(fl) tag[i]=1;
			}
		for(int i=2;i<=n;i++)
			if(tag[i]==1){
				ans[cnt].push_back(i);
				Erase(i),tag[i]=2;
				tot++;
			}
	}
	ans[++cnt].push_back(1);
	printf("%d\n",cnt);
	for(int i=1;i<=cnt;i++){
		printf("%d ",ans[i].size());
		for(int j=0;j<ans[i].size();j++)
			printf("%d ",ans[i][j]);
		printf("\n");
	}
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
1 2
1 3
1 4
4 5

output:

3
3 2 3 5 
1 4 
1 1 

result:

ok 

Test #2:

score: 0
Accepted
time: 1ms
memory: 3912kb

input:

500
183 443
32 443
334 443
254 443
331 443
348 443
54 443
430 443
275 443
410 443
360 443
443 468
140 443
179 443
93 443
327 443
128 443
365 443
122 443
43 443
46 443
399 443
398 443
269 443
130 443
227 443
412 443
61 443
295 443
98 443
30 443
197 443
397 443
95 443
192 443
266 443
48 443
310 443
28...

output:

3
498 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...

result:

ok 

Test #3:

score: -100
Time Limit Exceeded

input:

500
80 180
80 254
1 180
80 337
180 323
80 248
180 205
80 189
180 480
80 330
180 454
80 498
142 180
80 193
180 346
80 89
180 389
80 125
180 232
80 93
180 228
80 327
180 357
80 417
180 362
80 278
180 316
80 312
163 180
80 310
176 180
80 463
180 210
80 478
180 294
80 185
124 180
80 143
180 339
80 253
1...

output:


result: