zoukankan      html  css  js  c++  java
  • Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径

    E. Anton and Tree
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph.

    There are n vertices in the tree, each of them is painted black or white. Anton doesn't like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white).

    To change the colors Anton can use only operations of one type. We denote it as paint(v), where v is some vertex of the tree. This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v and u). For example, consider the tree

    and apply operation paint(3) to get the following:

    Anton is interested in the minimum number of operation he needs to perform in order to make the colors of all vertices equal.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.

    The second line contains n integers colori (0 ≤ colori ≤ 1) — colors of the vertices. colori = 0 means that the i-th vertex is initially painted white, while colori = 1 means it's initially painted black.

    Then follow n - 1 line, each of them contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — indices of vertices connected by the corresponding edge. It's guaranteed that all pairs (ui, vi) are distinct, i.e. there are no multiple edges.

    Output

    Print one integer — the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white.

    Examples
    Input
    11
    0 0 0 1 1 0 1 0 0 1 1
    1 2
    1 3
    2 4
    2 5
    5 6
    5 7
    3 8
    3 9
    3 10
    9 11
    Output
    2
    Input
    4
    0 0 0 0
    1 2
    2 3
    3 4
    Output
    0
    Note

    In the first sample, the tree is the same as on the picture. If we first apply operation paint(3) and then apply paint(6), the tree will become completely black, so the answer is 2.

    In the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0.

    题意:你每次去一个点,可以将其同色的联通块一起变色,求最后变为一色的最少次数;

    思路:将同色的看作一个点,每次去树的直径的中点即是最小次数;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    const int N=1e6+10,M=1e6+10,inf=1e9+10;
    const ll INF=1e18+10,mod=2147493647;
    int clo[N];
    struct is
    {
        int v,next;
    }edge[N];
    int head[N],edg;
    int node1,ans;
    void init()
    {
        edg=0;
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v)
    {
        edg++;
        edge[edg].v=v;
        edge[edg].next=head[u];
        head[u]=edg;
    }
    void dfs(int u,int fa,int deep)
    {
        if(deep>ans)
        {
            ans=deep;
            node1=u;
        }
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(v==fa)continue;
            if(clo[v]!=clo[u])
            dfs(v,u,deep+1);
            else
            dfs(v,u,deep);
        }
    }
    int main()
    {
        init();
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&clo[i]);
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        dfs(1,-1,0);
        dfs(node1,-1,0);
        cout<<(ans+1)/2<<endl;
        return 0;
    }
  • 相关阅读:
    科目一考试顺口溜 假一吊二撤三醉五逃终身酒犯罪
    Intellij IDEA 关闭和开启自动更新提示
    Spring注解之@Autowired:装配构造函数和属性
    Spring注解之@Autowired:Setter 方法上使用@Autowired注解
    Spring注解之@Autowired组件装配
    Spring注解之@Autowired:注入Arrays, Collections, and Maps
    SQL语句between and边界问题
    Spring中几个最常见的注解
    编辑距离(C++)
    回溯法解N皇后问题
  • 原文地址:https://www.cnblogs.com/jhz033/p/6082790.html
Copyright © 2011-2022 走看看