---
id: 2372
lang: en
translation_of: index.md
source_sha256: dfe4e098ef7fdfc4cc5ca146e2098857bbeec95af5e1dc1ae495cd24fab6700f
title: "P3349: [ZJOI2016] Little Stars — Solution"
description: "The used-label-set tree DP is too slow, so drop that dimension, fix a label set for the DP, and weight by (-1)^(n-|S|) to count exactly zero overlaps."
tags: ["OI","Inclusion–Exclusion","Tree DP","Subset Inclusion–Exclusion","Bitmask"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "可以的出 (sic) read as 可以得出, “can obtain”."
  - "点集 rendered as “vertex set”."
---

## Main Idea

How do I have no clue at all? (<span class="slang" data-zh="wtcl" data-gloss="Pinyin initials of 我太菜了 (wǒ tài cài le), “I am too vegetable”. Self-deprecating slang: I am so weak, so bad at this.">I'm so weak</span>) But <span class="slang" data-zh="有一说一" data-gloss="yǒu yī shuō yī, “if there is one, say one”. Internet phrase for speaking frankly: to be fair, credit where it is due.">to be fair</span>, the problem really is pretty good.

One approach close to the intended solution is to set up a DP: let $dp[u][i][stat]$ be the state where we are currently at node $u$, it has label $i$, and the set of labels already used is $stat$, and then do a tree DP. It is not hard to work out that the complexity of this thing is $\Theta(n^3 3^n)$.

This definitely won't pass, so consider some <span class="slang" data-zh="玄学" data-gloss="xuánxué, “metaphysics”. Something that seems to run on luck or mysterious rules rather than logic.">black-magic</span> optimization. If only we could get rid of the third constraint during the transitions. Let's consider throwing away this third dimension. Once it's thrown away, one problem we must face is invalid states. We can consider <span class="slang" data-zh="硬点" data-gloss="yìngdiǎn, “forcibly point out”. OI slang for forcibly fixing a choice or condition in advance, like 钦定.">forcibly fixing</span> a vertex set, so that throughout the whole DP only labels inside that set can be chosen. We find that the answer then means the number of ways with at least $i$ points chosen overlapping, so we can happily slap on a $(-1)^{n - |S|}$ coefficient and get the number of ways with exactly $0$ overlapping choices.

<!--more-->

## Code

```cpp
// P3349.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 20;

typedef long long ll;

int head[MAX_N], current, mp[MAX_N][MAX_N], col[MAX_N], siz, n, m;
ll dp[MAX_N][MAX_N];

struct edge
{
    int to, nxt;
} edges[MAX_N << 1];

void addpath(int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    head[src] = current++;
}

void dfs(int u, int fa)
{
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            dfs(edges[i].to, u);
    for (int i = 1; i <= siz; i++)
    {
        int cc = col[i];
        dp[u][cc] = 1;
        for (int j = head[u]; j != -1; j = edges[j].nxt)
            if (edges[j].to != fa)
            {
                ll sum = 0;
                for (int k = 1; k <= siz; k++)
                    sum += dp[edges[j].to][col[k]] * mp[cc][col[k]];
                dp[u][cc] *= sum;
            }
    }
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v; i <= m; i++)
        scanf("%d%d", &u, &v), mp[u][v] = mp[v][u] = 1;
    for (int i = 1, u, v; i <= n - 1; i++)
        scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
    ll ans = 0;
    for (int stat = 0; stat < (1 << n); stat++)
    {
        siz = 0;
        for (int i = 0; i < n; i++)
            if (stat & (1 << i))
                col[++siz] = i + 1;
        dfs(1, 0);
        ll pans = 0;
        for (int i = 1; i <= siz; i++)
            pans += dp[1][col[i]];
        ans += (((n - __builtin_popcount(stat)) & 1) ? -1LL : 1LL) * pans;
    }
    printf("%lld\n", ans);
    return 0;
}
```
