QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#412900#6745. Delete the TreeSaviorWA 1ms3760kbC++201.4kb2024-05-16 21:23:582024-05-16 21:23:58

Judging History

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

  • [2024-05-16 21:23:58]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3760kb
  • [2024-05-16 21:23:58]
  • 提交

answer

#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
#define int long long
using ll = long long;
const int inf = 1e17+7;
const int N=505;
const int mod =1e9+7;
typedef pair<int,int>P;
int mn=inf,rt=0,n=0;
int tot=0;
int sz[N];
vector<int>ans[N];
int vis[N];
vector<int>e[N];
void get(int u,int fa){
    sz[u]=1;
    int mx=0;
    for(auto v:e[u]){
        if(v==fa||vis[v]) continue;
        get(v,u);
        mx=max(mx,sz[v]);
        sz[u]+=sz[v];
    }
    mx=max(mx,n-sz[u]);
    if(mx<mn) mn=mx,rt=u;
    return;
}
void dfs(int u,int fa,int w){
    if(vis[u]) return;
    ans[w].push_back(u);
    tot=max(tot,w);    
    vis[u]=1;
    for(auto v:e[u]){
        if(v==fa||vis[v]) continue;
        mn=inf,n=sz[v];
        get(v,u);        
        dfs(rt,0,w+1);
    }
}

void solve(){
    int n;cin>>n;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    get(1,0);
    dfs(rt,0,0);
    //cout<<e[1].size()<<endl;
    cout<<tot+1<<endl;
    for(int i=0;i<=tot;i++){
        cout<<ans[i].size()<<' ';
        for(auto&it:ans[i])
            cout<<it<<' ';
        cout<<endl;
    }
    return;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t=1;//cin>>t;
    while(t--) solve();
    return 0;
}




詳細信息

Test #1:

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

input:

5
1 2
1 3
1 4
4 5

output:

4
1 2 
1 1 
2 3 5 
1 4 

result:

ok 

Test #2:

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

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
1 183 
1 443 
498 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 ...

result:

wrong answer