QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#153281 | #6745. Delete the Tree | yangjl | WA | 1ms | 3472kb | C++20 | 2.3kb | 2023-08-29 20:07:22 | 2023-08-29 20:07:23 |
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
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3436kb
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
Wrong Answer
time: 1ms
memory: 3472kb
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:
102 399 1 2 3 6 7 8 10 11 12 13 16 17 18 19 20 21 22 23 24 25 26 28 30 31 32 33 34 35 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 57 58 59 60 61 64 65 67 68 69 72 73 74 75 76 77 78 79 80 82 83 84 85 86 87 88 89 90 91 92 93 94 95 98 99 100 101 102 103 104 105 108 109 110 111 112 114 115 116 11...
result:
wrong answer Integer 102 violates the range [0, 10]