zoukankan      html  css  js  c++  java
  • Balancing Act 树的重心

    Balancing Act

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <cassert>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = acos(-1.0);
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    inline ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    void exgcd(ll A, ll B, ll& x, ll& y)
    {
        if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
    }
    inline int qpow(int x, ll n) {
        int r = 1;
        while (n > 0) {
            if (n & 1) r = 1ll * r * x % mod;
            n >>= 1; x = 1ll * x * x % mod;
        }
        return r;
    }
    inline int inv(int x) {
        return qpow(x, mod - 2);
    }
    ll lcm(ll a, ll b)
    {
        return a * b / gcd(a, b);
    }
    /**********************************************************/
    const int N = 2e4 + 5;
    int sz[N],wt[N];
    int n, rt;
    void getcentroid(int x, int fa,vector<vector<int> >& g)
    {
        sz[x] = 1;
        wt[x] = 0;
        for (int i = 0; i < g[x].size(); i++)
        {
            int to = g[x][i];
            if (to != fa)
            {
                getcentroid(to, x, g);
                sz[x] += sz[to];
                wt[x] = max(wt[x], sz[to]);
            }
        }
        wt[x] = max(wt[x], n - sz[x]);
        if (rt == 0 || wt[x] < wt[rt])//rt是重心编号
            rt = x;
    }
    
    int main() {
        //ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
        int T;
        cin >> T;
        while (T--)
        {
            memset(wt, 0, sizeof(wt));
            memset(sz, 0, sizeof(sz));
            cin >> n;
            rt = 0;
            vector<vector<int> > g(n + 1);
            rep(i, 1, n - 1)
            {
                int u, v;
                scanf("%d%d", &u ,& v);
                g[u].push_back(v);
                g[v].push_back(u);
            }
            getcentroid(1, -1, g);
            printf("%d %d
    ", rt, wt[rt]);
        }
    }
  • 相关阅读:
    sqlserver编程基本语法
    每日一记--技术小细节
    每日一记--jsp
    每日一记--session/servletContext
    每日一记--cookie
    每日一记--HashTable/HashMap/ConcurrentHashMap
    每日一记--Ajax(下)
    每日一记--Axjx
    每日一记--索引/过滤器
    每日一记--酱油日
  • 原文地址:https://www.cnblogs.com/dealer/p/13418669.html
Copyright © 2011-2022 走看看