Anton and Tree
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 andu). 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.
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.
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.
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
2
4
0 0 0 0
1 2
2 3
3 4
0
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.
分析:树上有2种颜色,每次操作可以把连通同色的变色,问树上所有点变成一种颜色所需最小操作;
可以证明缩点后答案就是直径上点数/2;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] #define sys system("pause") #define freopen freopen("in.txt","r",stdin) const int maxn=2e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,k,t,p[maxn],c[maxn],dep[maxn],fa[maxn],ma,po; vi e[maxn],f[maxn]; int find(int x) { return p[x]==x?x:p[x]=find(p[x]); } void dfs(int s,int q) { for(int x:e[s]) { if(x==q)continue; fa[x]=s; if(c[x]==c[s]) { p[find(x)]=find(s); } dfs(x,s); } } void dfs1(int p,int q) { for(int x:f[p]) { if(x==q)continue; dep[x]=dep[p]+1; if(dep[x]>ma)ma=dep[x],po=x; dfs1(x,p); } } int main() { int i,j; scanf("%d",&n); rep(i,1,n)scanf("%d",&c[i]),p[i]=i; rep(i,1,n-1) { scanf("%d%d",&j,&k); e[j].pb(k),e[k].pb(j); } dfs(1,0); rep(i,1,n) { int x=find(i),y=find(fa[i]); if(x==i&&y&&y!=x) { po=i; f[i].pb(y); f[y].pb(i); } } dfs1(po,0); dep[po]=0; dfs1(po,0); printf("%d\n",(ma+1)/2); //system("Pause"); return 0; }