import java.util.Scanner;
public class Main {
public static void main(String[] arg) {
Scanner scanner = new Scanner(System.in);
// todo
}
}
NC208301. 「SCOI2005」王室联邦
描述
输入描述
第一行包含两个数。接下来 N-1 行,每行描述一条边,包含两个数,即这条边连接的两个城市的编号。
输出描述
如果无法满足国王的要求,输出。否则第一行输出数
,表示你给出的划分方案中省的个数,编号为
。第二行输出
个数,第
个数表示编号为
的城市属于的省的编号,第三行输出
个数,表示这
个省的省会的城市编号,如果有多种方案,你可以输出任意一种。
示例1
输入:
8 2 1 2 2 3 1 8 8 7 8 6 4 6 6 5
输出:
3 2 1 1 3 3 3 3 2 2 1 8
C++(clang++ 11.0.1) 解法, 执行用时: 3ms, 内存消耗: 496K, 提交时间: 2022-10-17 15:34:05
#include<bits/stdc++.h>using namespace std;const int N=1005;int n,b;int cap[N],bel[N],cnt=0,stk[N],top=0;vector<int>G[N];inline void dfs(int x,int f){int now=top;for(auto to:G[x]){if(to==f) continue;dfs(to,x);if(top-now>=b){cap[++cnt]=x;while(top!=now) bel[stk[top--]]=cnt;}}stk[++top]=x;}signed main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>b;for(int i=1,u,v;i<n;++i){cin>>u>>v;G[u].emplace_back(v);G[v].emplace_back(u);}dfs(1,0);while(top) bel[stk[top--]]=cnt;cout<<cnt<<"\n";for(int i=1;i<=n;++i) cout<<bel[i]<<" \n"[i==n];for(int i=1;i<=cnt;++i) cout<<cap[i]<<" \n"[i==cnt];return 0;}