[poj1655 Balancing Act]樹的重心

題意:樹的重心定義爲樹上的一個點把這個點刪掉後造成的若干棵子樹中節點數的最大值最小。求樹的重心和那個最小值ios

思路:考慮dfs,當根從fa轉到cur時,那麼fa以及fa除cur之外的其它子樹變成cur的一個兒子,它的大小爲N-sizeof(cur)。其它的遞歸統計便可。spa

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>
using namespace std;
#define X first
#define Y second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define mset(a, x) memset(a, x, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(b))
#define cas() int T, cas = 0; cin >> T; while (T --)
template<typename T>bool umax(T&a, const T&b){return a<b?(a=b,true):false;}
template<typename T>bool umin(T&a, const T&b){return b<a?(a=b,true):false;}
typedef long long ll;
typedef pair<int, int> pii;

#ifndef ONLINE_JUDGE
    #include "local.h"
#endif

const int N = 2e4 + 7;
const int M = N;
const int INF = 0x3f3f3f3f;

namespace Edge {
    int last[N], to[M << 1], next[M << 1], cntE;
    void init() {
        cntE = 0;
        memset(last, -1, sizeof(last));
    }
    void addEdge(int u, int v) {
        to[cntE] = v;
        next[cntE] = last[u];
        last[u] = cntE ++;
    }
}

int n;

namespace Center {
    int root, siz, son[N];
    bool vis[N];
    void init() {
        siz = INF;
        memset(vis, 0, sizeof(vis));
    }
    void getRoot(int cur) {
        vis[cur] = true;
        son[cur] = 0;
        int buf = 0;
        for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
            int to = Edge::to[i];
            if (!vis[to]) {
                getRoot(to);
                son[cur] += son[to] + 1;
                buf = max(buf, son[to] + 1);
            }
        }
        buf = max(buf, n - son[cur] - 1);
        if (buf < siz || buf == siz && cur < siz) {
            siz = buf;
            root = cur;
        }
    }
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    int u, v;
    cas() {
        Edge::init();
        Center::init();
        cin >> n;
        for (int i = 1; i < n; i ++) {
            scanf("%d%d", &u, &v);
            Edge::addEdge(u, v);
            Edge::addEdge(v, u);
        }
        Center::getRoot(1);
        printf("%d %d\n", Center::root, Center::siz);
    }
    return 0;
}
相關文章
相關標籤/搜索