QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#412918#6745. Delete the TreeSaviorWA 0ms3676kbC++201.3kb2024-05-16 21:37:102024-05-16 21:37:11

Judging History

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

  • [2024-05-16 21:37:11]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3676kb
  • [2024-05-16 21:37:10]
  • 提交

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(){
    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<<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: 0
Wrong Answer
time: 0ms
memory: 3676kb

input:

5
1 2
1 3
1 4
4 5

output:

3
1 1 
3 2 3 5 
1 4 

result:

wrong answer