1. 程式人生 > 其它 >【牛客小白月賽42】F火鳳燎原

【牛客小白月賽42】F火鳳燎原

「蒲公英」是樹上的一個連通子集,有且僅有一個「中心點」,另外:
設「中心點」在原樹中的度為 k≥3,則必須有k-1個結點在「蒲公英」中是一個單點,剩下一條鏈;那條鏈的本身長度(不包含u)必須不少於2。
兩個「蒲公英」本質不同,當且僅當中心點 u 不同或對應的鏈包含的結點不同。(至少存在一個結點 v 屬於一個「蒲公英」而不屬於另一個「蒲公英」)
給定一棵無根樹,求其中「蒲公英」的個數。

。。。。
這。。。讀清題目就能ak了啊。。。
和u相連的k個點都要選,而且只能有一個鏈,以u為中心的答案當然就是n-1-du。。。

#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 2e6 + 10;
int d[maxn];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T, n, x;
    cin >> T;
    while (T--) {
        cin >> n;
        memset(d, 0, sizeof(int) * (n + 1));
        for (int i = 1; i <= 2 * (n - 1); i++) {
            cin >> x;
            d[x]++;
        }
         
        long long ans = 0;
        for (int i = 1; i <= n; i++)
            if (d[i] >= 3) ans += n - d[i] - 1;
        cout << ans << endl;
    }
    return 0;
}