You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.9 KiB
C++
79 lines
1.9 KiB
C++
#include <cstdio>
|
|
#include <cctype>
|
|
#include <algorithm>
|
|
#include <vector>
|
|
#include <cstring>
|
|
using namespace std;
|
|
void getint(int &x)
|
|
{
|
|
int ch = x = 0;
|
|
while (!isdigit(ch = getchar()))
|
|
;
|
|
for (; isdigit(ch); ch = getchar())
|
|
x = x * 10 + ch - '0';
|
|
}
|
|
const int N = 1e5 + 10;
|
|
int T, n, k, c;
|
|
int adj[N], nxt[N << 1], to[N << 1], ecnt, sz[N], cnt[N], fa[N];
|
|
bool b[N];
|
|
vector<int> grp[N];
|
|
inline void addEdge(int f, int t)
|
|
{
|
|
ecnt++;
|
|
nxt[ecnt] = adj[f];
|
|
adj[f] = ecnt;
|
|
to[ecnt] = t;
|
|
}
|
|
void dfs(int x, int p)
|
|
{
|
|
sz[x] = 1;
|
|
for (int e = adj[x]; e; e = nxt[e])
|
|
if (to[e] != p)
|
|
dfs(to[x], fa[to[x]] = x), sz[x] += sz[to[e]];
|
|
cnt[sz[x]]++;
|
|
if (sz[x] % k == 0)
|
|
grp[sz[x]].push_back(x);
|
|
}
|
|
void prnt(int x, int p)
|
|
{
|
|
if (b[x]) return;
|
|
b[x] = true, ++c;
|
|
printf("%d", x);
|
|
putchar(" \n"[c % k == 0]);
|
|
for (int e = adj[x]; e; e = nxt[e])
|
|
if (to[e] != p)
|
|
prnt(to[e], e);
|
|
}
|
|
int main()
|
|
{
|
|
getint(T);
|
|
while (T--)
|
|
{
|
|
memset(adj, 0, sizeof(adj));
|
|
memset(nxt, 0, sizeof(nxt));
|
|
memset(to, 0, sizeof(to));
|
|
memset(sz, 0, sizeof(sz));
|
|
memset(cnt, 0, sizeof(cnt));
|
|
memset(fa, 0, sizeof(fa));
|
|
memset(b, 0, sizeof(b));
|
|
for (int i = 0; i <= n; i++) grp[i].clear();
|
|
ecnt = c = 0;
|
|
getint(n), getint(k);
|
|
for (int i = 1, a, b; i < n; i++)
|
|
getint(a), getint(b), addEdge(a, b), addEdge(b, a);
|
|
dfs(1, 0);
|
|
int gcnt = 0;
|
|
for (int i = k; i <= n; i += k)
|
|
gcnt += cnt[i];
|
|
if (gcnt * k != n)
|
|
puts("NO");
|
|
else
|
|
{
|
|
puts("YES");
|
|
for (int i = k; i <= n; i += k)
|
|
for (int t = 0; t < grp[k].size(); t++)
|
|
prnt(grp[k][t], fa[grp[k][t]]);
|
|
}
|
|
}
|
|
return 0;
|
|
} |