QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#153280 | #6745. Delete the Tree | yangjl | RE | 1ms | 3864kb | C++20 | 2.3kb | 2023-08-29 20:05:24 | 2023-08-29 20:05:25 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>
#define endl '\n'
using namespace std;
using ll=long long;
#ifdef YJL
#include<debug.h>
#else
#define debug(...) 1
#define debug_n(...) 1
#endif
int n;
vector<vector<int>> g;
int finalDel[505];
int del[505];
void dfs(int u,int fa) {
g[u].erase(find(g[u].begin(),g[u].end(),fa),g[u].end());
if(g[u].size()==0) {
del[u]=1;
return;
}
for(int v:g[u])
dfs(v,u);
if(g[u].size()==1 && !del[g[u][0]]) {
del[u]=1;
}
}
void solve() {
cin>>n;
g.resize(n);
for(int i=1,u,v; i<n; ++i) {
cin>>u>>v;
--u,--v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<vector<int>> ans;
while(1) {
// debug(g);
int rt=-1;
for(int i=0; i<n; ++i) {
if(finalDel[i])
continue;
if(g[i].size()<=1) {
rt=i;
break;
}
}
fill_n(del,n,0);
dfs(rt,-1);
vector<int> cur;
for(int i=0; i<n; ++i) {
if(finalDel[i])
continue;
if(del[i]) {
cur.push_back(i);
finalDel[i]=1;
}
}
ans.push_back(cur);
vector<vector<int>> newG(n);
int remain=0;
for(int i=0; i<n; ++i) {
if(finalDel[i])
continue;
remain++;
for(int v:g[i]) {
if(!finalDel[v])
newG[i].push_back(v);
else {
if(g[v].size()==0) continue;
newG[i].push_back(g[v][0]);
}
}
}
g.swap(newG);
if(remain<=0)
break;
}
assert(ans.size()<=10);
cout<<ans.size()<<endl;
for(auto& v:ans) {
cout<<v.size();
for(int x:v)
cout<<" "<<x+1;
cout<<endl;
}
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int tt=1;
// cin>>tt;
while(tt--) {
solve();
}
return 0;
}
/*
flie=C; g++ $flie.cpp -o $flie -D TRIPLE_XOR_SUM -std=c++17 -fsanitize=address -fsanitize=undefined ; ./$flie
5
1 2
1 3
1 4
4 5
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3864kb
input:
5 1 2 1 3 1 4 4 5
output:
5 1 1 1 2 1 3 1 5 1 4
result:
ok
Test #2:
score: -100
Dangerous Syscalls
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...