关于树:直径,重心

树的重心:

找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心

    #include   
    #include   
    #include   
      
    using namespace std;  
    const int N = 20005;  
    const int INF = 1<<30;  
      
    int head[N];  
    int son[N];  
    bool vis[N];  
    int cnt,n;  
    int ans,size;  
      
    struct Edge  
    {  
        int to;  
        int next;  
    };  
      
    Edge edge[2*N];  
      
    void Init()  
    {  
        cnt = 0;  
        size = INF;  
        memset(vis,0,sizeof(vis));  
        memset(head,-1,sizeof(head));  
    }  
      
    void add(int u,int v)  
    {  
        edge[cnt].to = v;  
        edge[cnt].next = head[u];  
        head[u] = cnt++;  
    }  
      
    void dfs(int cur)  
    {  
        vis[cur] = 1;  
        son[cur] = 0;  
        int tmp = 0;  
        for(int i=head[cur];~i;i=edge[i].next)  
        {  
            int u = edge[i].to;  
            if(!vis[u])  
            {  
                dfs(u);  
                son[cur] += son[u] + 1;  
                tmp = max(tmp,son[u] + 1);  
            }  
        }  
        tmp = max(tmp,n-son[cur]-1);  
        if(tmp < size || tmp == size && cur < ans)  
        {  
            ans = cur;  
            size = tmp;  
        }  
    }  
      
    int main()  
    {  
        int T;  
        scanf("%d",&T);  
        while(T--)  
        {  
            Init();  
            scanf("%d",&n);  
            for(int i=1;i<=n-1;i++)  
            {  
                int u,v;  
                scanf("%d%d",&u,&v);  
                add(u,v);  
                add(v,u);  
            }  
            dfs(1);  
            printf("%d %d\n",ans,size);  
        }  
        return 0;  
    }  

树的直径:

int num[maxn];  
int res, point;  
void dfs(int u, int fa)  
{  
    for(int i = head[u]; ~i; i = edge[i].next) {  
        int v = edge[i].to, w = edge[i].w;  
        if(v == fa) continue;  
        num[v] = num[u] + w;  
        if(res < num[v]) {  
            res = num[v];  
            point = v;  
        }  
        dfs(v, u);  
    }  
}  
struct Path{  
    int to, id;  
}path[maxn];  
  
void dfs1(int u, int fa)  
{  
    for(int i = head[u]; ~i; i = edge[i].next) {  
        int v = edge[i].to, w = edge[i].w;  
        if(v == fa) continue;  
        num[v] = num[u] + w;  
        path[v].to = u; path[v].id = i;  
        if(res < num[v]) {  
            res = num[v];  
            point = v;  
        }  
        dfs1(v, u);  
    }  
}  



你可能感兴趣的:(模板)