In the following tree diagrams, - and | denote edges of the tree, * denotes an unoccupied vertex, and ? denotes an occupied vertex.
For the answer is always Now let's consider the case
Say that a state is in "normalized" if the cows occupy vertices . Clearly any state can be normalized via a series of moves. Call two normalized states "friends" if one is reachable from the other. The number of friends for each state must be the same. The answer will equal over the number of friends of any normalized state with cows has.
For any two cows and in the normalized state, we wish to know whether we can swap and while leaving all other cows in the same vertices. The goal is to move until the tree contains the following as a subgraph:
* | | x--*--y
Then you can swap and and undo all the moves made up to this point to re-normalize the state.
Consider the following method that moves and into the above state if possible, or otherwise states that it is impossible to swap and .
x--*--*--*--z--y ?--*--*--*--z--y | -> | | | ? x
x--*--*--z--y | | ?
* z | | | -> | *--x--z--y x--*--*--y
Let's further examine the case where it is impossible to swap and . Call a vertex "intermediate" if it has degree 2. Move such that is adjacent to , and suppose that the shortest path with non-intermediate endpoints that contains edge at the time of termination is ; call this the extension of .
Let be the size of the subtree of when the tree is rooted at and define similarly. We can show that if and can't be swapped, then In fact, there will be
vertices that always remain on the path (and their relative order on the path can never change). Plus, cows that are in the subtree of rooted at excluding can never reach the subtree of rooted at excluding , and vice versa. For example, consider the following extension:
. . | | | | a--.--b | | | | . . | | .
In the following situation, , so none of can swap with any of .
1 4 | | | | *--*--* | | | | 2 5 | | 3In the following situation, , so cannot leave the path.
1 4 | | | | 6--*--* | | | | 2 5 | | 3
Call every edge on each such extension "saturated," and the cows that are constrained to some extension "stuck."
Now consider each connected component that remains after removing all saturated edges. Suppose that connected component contains exactly unsaturated edges and is adjacent to exactly saturated edges. To compute the number of non-stuck cows within , we should start with and then subtract some quantity for each adjacent saturated edge.
For the extension of each adjacent saturated edge, let be the vertex outside the component and be the vertex inside the component. Then number of vertices removed from the component by this edge is equal to
For any friend, the stuck cows must remain in the same relative positions. However, the unstuck cows in each component mentioned above can permute themselves arbitrarily. So the number of friends of each state is and the answer will be
We can compute this expression for all in decreasing order. For each path that transitions from saturated to unsaturated when is decremented, we can update and for the resulting combined component with the "Disjoint Set Union" data structure. Furthermore, the sum of the number of components over all is equal to
#include <bits/stdc++.h> using namespace std; #define f first #define s second typedef long long ll; const int MOD = 1e9+7; const int MX = 1e5+5; void setIO(string s) { ios_base::sync_with_stdio(0); cin.tie(0); freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } struct mi { int v; explicit operator int() const { return v; } mi() { v = 0; } mi(ll _v):v(_v%MOD) { v += (v<0)*MOD; } }; mi& operator+=(mi& a, mi b) { if ((a.v += b.v) >= MOD) a.v -= MOD; return a; } mi& operator-=(mi& a, mi b) { if ((a.v -= b.v) < 0) a.v += MOD; return a; } mi operator+(mi a, mi b) { return a += b; } mi operator-(mi a, mi b) { return a -= b; } mi operator*(mi a, mi b) { return mi((ll)a.v*b.v); } mi& operator*=(mi& a, mi b) { return a = a*b; } vector<int> invs, fac, ifac; void genFac(int SZ) { invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); invs[1] = fac[0] = ifac[0] = 1; for (int i = 2; i < SZ; ++i) invs[i] = MOD-(ll)MOD/i*invs[MOD%i]%MOD; for (int i = 1; i < SZ; ++i) { fac[i] = (ll)fac[i-1]*i%MOD; ifac[i] = (ll)ifac[i-1]*invs[i]%MOD; } } ll comb(int a, int b) { if (a < b || b < 0) return 0; return (ll)fac[a]*ifac[b]%MOD*ifac[a-b]%MOD; } int N, par[MX]; vector<int> adj[MX]; mi ans[MX]; pair<int,int> cur[MX]; vector<pair<int,pair<int,int>>> ed; set<int> con; struct DSU { vector<int> e; void init(int n) { e = vector<int>(n,-1); } int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } bool unite(int len, int x, int y) { // union-by-rank x = get(x), y = get(y); assert(x != y); if (e[x] > e[y]) swap(x,y); e[x] += e[y]; e[y] = x; assert(con.count(y)); con.erase(y); cur[x].f += cur[y].f-2; cur[x].s += cur[y].s+len; return 1; } }; DSU D; void dfs(int x) { for (int t: adj[x]) if (t != par[x]) { par[t] = x; dfs(t); } } void dfs(int x, int lst, int d) { if (adj[x].size() != 2) { if (lst) ed.push_back({d,{x,lst}}); d = 0; lst = x; } for (int y: adj[x]) if (y != par[x]) { par[y] = x; dfs(y,lst,d+1); } } int main() { setIO("circus"); cin >> N; genFac(N+1); for (int i = 0; i < N-1; ++i) { int a,b; cin >> a >> b; adj[a].push_back(b), adj[b].push_back(a); } int root = 1; while (adj[root].size() == 2) root ++; dfs(root,0,0); sort(begin(ed),end(ed)); for (int i = 1; i <= N; ++i) if (adj[i].size() != 2) { cur[i] = {adj[i].size(),0}; con.insert(i); } ans[N] = fac[N]; int ind = 0; D.init(N+1); for (int k = N-1; k > 0; --k) { while (ind < ed.size() && N-1-ed[ind].f > k) { D.unite(ed[ind].f,ed[ind].s.f,ed[ind].s.s); ind ++; } mi ret = fac[k]; for (int t: con) ret *= ifac[(N-1-k)*(cur[t].f-1)+cur[t].s]; ans[k] = ret; } for (int i = 1; i <= N; ++i) cout << ans[i].v << "\n"; }