1. 程式人生 > >【CF613D】Kingdom and its Cities

【CF613D】Kingdom and its Cities

stdin || ios 表示 代碼 %d 分類討論 如果 sort

【CF613D】Kingdom and its Cities

題面

洛谷

題解

看到關鍵點當然是建虛樹啦。

\(f[x]\)表示以\(x\)為根的子樹的答案,\(g[x]\)表示以\(x\)為根的子樹內是否有\(x\)聯通的點,\(c=\sum_{v\in son_x} g[v]\)

分類討論一下:

  • 如果一個點在原樹下它和它的父親均為關鍵點,那麽顯然不行。
  • 這個點是關鍵點,\(f[x]+=c\)\(g[x]=1\)
  • 這個點不是關鍵點,若\(c>1\)則斷掉這個點,\(++f[x],g[x]=0\),否則\(g[x]=1\)

這題就做\(v.a.n\)啦。

代碼

#include <iostream> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <cmath> 
#include <algorithm> 
using namespace std; 
inline int gi() { 
    register int data = 0, w = 1; 
    register char ch = 0; 
    while (!isdigit(ch) && ch != '-') ch = getchar(); 
    if (ch == '-') w = -1, ch = getchar(); 
    while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); 
    return w * data; 
} 
const int MAX_N = 1e5 + 5; 
struct Graph { int to, next; } e[MAX_N << 1];
int fir1[MAX_N], fir2[MAX_N], e_cnt; 
void clearGraph() {
    memset(fir1, -1, sizeof(fir1)); 
    memset(fir2, -1, sizeof(fir2)); 
} 
void Add_Edge(int *fir, int u, int v) { 
    e[e_cnt] = (Graph){v, fir[u]}; 
    fir[u] = e_cnt++; 
}
namespace Tree { 
    int fa[MAX_N], dep[MAX_N], size[MAX_N], top[MAX_N], son[MAX_N], dfn[MAX_N], tim; 
    void dfs1(int x) { 
        dfn[x] = ++tim; 
        size[x] = 1, dep[x] = dep[fa[x]] + 1; 
        for (int i = fir1[x]; ~i; i = e[i].next) {
            int v = e[i].to; if (v == fa[x]) continue; 
            fa[v] = x; dfs1(v); size[x] += size[v]; 
            if (size[v] > size[son[x]]) son[x] = v; 
        } 
    } 
    void dfs2(int x, int tp) {
        top[x] = tp; 
        if (son[x]) dfs2(son[x], tp); 
        for (int i = fir1[x]; ~i; i = e[i].next) {
            int v = e[i].to; if (v == fa[x] || v == son[x]) continue; 
            dfs2(v, v); 
        } 
    } 
    int LCA(int x, int y) { 
        while (top[x] != top[y]) { 
            if (dep[top[x]] < dep[top[y]]) swap(x, y); 
            x = fa[top[x]]; 
        } 
        return dep[x] < dep[y] ? x : y; 
    } 
}
using Tree::dfn; using Tree::LCA; 
int N, M, K, a[MAX_N], f[MAX_N];
bool g[MAX_N], key[MAX_N]; 
bool cmp(const int &i, const int &j) { return dfn[i] < dfn[j]; } 
void build() {
    static int stk[MAX_N], top;
    e_cnt = 0; top = 0; 
    sort(&a[1], &a[K + 1], cmp); 
    for (int i = 1; i <= K; i++) if (a[i] != a[i - 1]) stk[++top] = a[i];
    K = top; 
    for (int i = 1; i <= K; i++) a[i] = stk[i]; 
    stk[top = 1] = 1, fir2[1] = -1; 
    for (int i = 1; i <= K; i++) { 
        if (a[i] == 1) continue;
        int lca = LCA(a[i], stk[top]);
        if (lca != stk[top]) { 
            while (dfn[lca] < dfn[stk[top - 1]]) { 
                int u = stk[top], v = stk[top - 1]; 
                Add_Edge(fir2, u, v), Add_Edge(fir2, v, u); 
                --top; 
            } 
            if (dfn[lca] > dfn[stk[top - 1]]) { 
                fir2[lca] = -1; int u = lca, v = stk[top];
                Add_Edge(fir2, u, v), Add_Edge(fir2, v, u);
                stk[top] = lca; 
            } else { 
                int u = lca, v = stk[top--]; 
                Add_Edge(fir2, u, v), Add_Edge(fir2, v, u); 
            } 
        } 
        fir2[a[i]] = -1, stk[++top] = a[i]; 
    } 
    for (int i = 1; i < top; i++) {
        int u = stk[i], v = stk[i + 1]; 
        Add_Edge(fir2, u, v), Add_Edge(fir2, v, u); 
    } 
} 
void Dp(int x, int fa) { 
    f[x] = g[x] = 0; 
    int c = 0; 
    for (int i = fir2[x]; ~i; i = e[i].next) { 
        int v = e[i].to; if (v == fa) continue; 
        Dp(v, x); 
        f[x] += f[v], c += g[v]; 
    } 
    if (key[x]) g[x] = 1, f[x] += c; 
    else if (c > 1) g[x] = 0, ++f[x]; 
    else g[x] = c; 
} 
int main () { 
#ifndef ONLINE_JUDGE 
    freopen("cpp.in", "r", stdin); 
#endif
    clearGraph(); 
    N = gi();
    for (int i = 1; i < N; i++) { 
        int u = gi(), v = gi(); 
        Add_Edge(fir1, u, v), Add_Edge(fir1, v, u); 
    }
    Tree::dfs1(1), Tree::dfs2(1, 1); 
    M = gi(); 
    while (M--) {
        K = gi(); for (int i = 1; i <= K; i++) key[a[i] = gi()] = 1;
        bool flg = 1; 
        for (int i = 1; i <= K && flg; i++) if (key[Tree::fa[a[i]]]) flg = 0; 
        if (!flg) { puts("-1"); goto NXT; } 
        build(); 
        Dp(1, 0); 
        printf("%d\n", f[1]); 
      NXT : 
        for (int i = 1; i <= K; i++) key[a[i]] = 0; 
    } 
    return 0; 
} 

【CF613D】Kingdom and its Cities