QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#394695#6745. Delete the TreeLo#WA 1ms3704kbC++141.3kb2024-04-20 17:54:332024-04-20 17:54:33

Judging History

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

  • [2024-04-20 17:54:33]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3704kb
  • [2024-04-20 17:54:33]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=505;
vector<int> e[N],w[N];
int n,x,y,del[N],dp[N][2];
void dfs(int u,int fa)
{
	dp[u][1]=1;
	for (auto v:e[u])
	{
		if (v==fa) continue;
		dfs(v,u);
		dp[u][0]+=max(dp[v][0],dp[v][1]);
		dp[u][1]+=dp[v][0];
	}
	if (e[u].size()>2) dp[u][1]=0; 
}
vector<int> b;
void solve(int u,int fa,int flg)
{
	if (flg)
	{
		del[u]=1;
		b.push_back(u);
		for (auto x:e[u])
			for (auto y:e[u])
			{
				if (x==y) continue;
				w[x].push_back(y);
//				cout<<u<<":("<<x<<","<<y<<")\n";
			}
		for (auto v:e[u])
			if (v!=fa) solve(v,u,0);
		return;
	}
	for (auto v:e[u])
	{
		if (v==fa) continue;
		if (dp[v][1]>dp[v][0]) solve(v,u,1);
		else
		{
			w[u].push_back(v);
			w[v].push_back(u);
			solve(v,u,0);
		}
	}
}
int main()
{
	cin>>n;
	for (int i=1;i<n;++i)
	{
		cin>>x>>y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	vector<vector<int>> ans;
	while (n)
	{
		int t=1;
		while (del[t]) t++;
		dfs(t,0);
		if (dp[t][1]>dp[t][0]) solve(t,0,1);
		else solve(t,0,0);
		memset(dp,0,sizeof(dp));
		int m=0;
		for (int i=1;i<=n;++i)
		{
			e[i]=w[i];
			w[i].clear();
			m+=(!del[i]);
		}
		n=m;
		ans.push_back(b);
		b.clear();
	}
	cout<<ans.size()<<"\n";
	for (auto v:ans)
	{
		cout<<v.size();
		for (auto t:v) cout<<" "<<t;
		cout<<"\n";
	}
}

詳細信息

Test #1:

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

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

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:

2
499 1 183 32 334 254 331 348 54 430 275 410 360 468 140 179 93 327 128 365 122 43 46 399 398 269 130 227 412 61 295 98 30 197 397 95 192 266 48 310 283 127 123 7 154 317 302 158 65 218 306 191 309 210 20 190 204 484 182 429 362 99 92 347 39 488 58 115 228 8 346 111 386 498 408 259 289 333 256 352 ...

result:

ok 

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 3704kb

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:

2
498 1 254 337 248 189 330 498 193 89 125 93 327 417 278 312 310 463 478 185 143 253 272 277 91 404 53 68 64 424 78 458 72 5 177 156 218 48 433 137 256 245 329 333 146 131 391 94 237 413 406 65 431 441 233 377 168 118 242 298 259 285 35 63 336 344 260 483 494 465 428 386 313 186 109 414 79 191 440 ...

result:

wrong answer