Codeforces Round #379 (Div. 2) E. Anton and Tree

题意:给你一棵树, 每个点要么是黑色要么是白色, 有一种操作是将同一个颜色的连通块变成相反的颜色,问你最少变换几次, 整颗树变成一种颜色。

思路: 缩点, 加求树的直径, 答案为树的直径除二向上取整。

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define mk make_pair
 4 using namespace std;
 5
 6 const int N = 5e5 + 7;
 7 const int inf = 0x3f3f3f3f;
 8 const int INF = 0x3f3f3f3f3f3f3f3f;
 9
10 int n, a[N], cnt[N], tot, vis[N], depth[N], id, ans;
11 vector<int> edge1[N], edge2[N];
12
13 void dfs(int u, int pre, int op, int f) {
14     vis[u] = f;
15     for(int v : edge1[u]) {
16         if(v == pre || a[v] != op)
17             continue;
18         dfs(v, u, op, f);
19     }
20 }
21
22 void dfs2(int u, int pre, int deep) {
23     depth[u] = deep;
24     if(depth[u] > depth[id])
25         id = u;
26     for(int v : edge2[u]) {
27         if(v == pre) continue;
28         dfs2(v, u, deep + 1);
29     }
30 }
31
32 void dfs3(int u, int pre, int deep) {
33     ans = max(ans, deep);
34     for(int v : edge2[u]) {
35         if(v == pre) continue;
36         dfs3(v, u, deep + 1);
37     }
38 }
39 int main() {
40     scanf("%d", &n);
41     for(int i = 1; i <= n; i++) {
42         scanf("%d", &a[i]);
43     }
44
45     for(int i = 1; i < n; i++) {
46         int u, v; scanf("%d%d", &u, &v);
47         edge1[u].push_back(v);
48         edge1[v].push_back(u);
49     }
50
51     for(int i = 1; i <= n; i++) {
52         if(vis[i]) continue;
53         dfs(i, 0, a[i], ++tot);
54     }
55
56     for(int u = 1; u <= n; u++) {
57         for(int v : edge1[u]) {
58             if(u > v || vis[u] == vis[v])
59                 continue;
60             edge2[vis[u]].push_back(vis[v]);
61             edge2[vis[v]].push_back(vis[u]);
62         }
63     }
64
65     dfs2(1, 0, 0);
66     dfs3(id, 0, 0);
67     if(ans & 1) ans++;
68     printf("%d\n", ans / 2);
69     return 0;
70 }
71 /*
72 */

原文地址:https://www.cnblogs.com/CJLHY/p/8873877.html

时间: 2024-11-04 12:30:04

Codeforces Round #379 (Div. 2) E. Anton and Tree的相关文章

Codeforces Round #379 (Div. 2) C. Anton and Making Potions(二分)

Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare npotions. Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two typ

Codeforces Round #379 (Div. 2) D. Anton and Chess(模拟)

Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check

Codeforces Round #253 (Div. 2) A. Anton and Letters

题目很简单,只需要注意带空格的输入用getline即可 #include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> using namespace std; int main(){ string str; getline(cin,str); set<char> a; for(int i= 1 ; i < s

Codeforces Round #379 (Div. 2) Analyses By Team:Red &amp; Black

A.Anton and Danik Problems: 给你长度为N的,只含'A','D'的序列,统计并输出何者出现的较多,相同为"Friendship" Analysis: lucky_ji: 水题,模拟统计A和D的数目比较大小输出结果即可 Tags: Implementation B.Anton and Digits Problems: 给定k2个2,k3个3,k5个5及k6个6,可以组成若干个32或256,求所有方案中Sigma的最大值 Analysis: lucky_ji: 同

Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心

B. Anton and Lines The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessar

Codeforces Round #379 (Div. 2) 总结分享

前言 初入acm的新手,打算在cf混.这几天没有比赛,就做了个最新的Virtual participation.虽然说div2比较简单,但还是被虐得体无完肤...Orz.两个小时,共6道题.最后只AC了AB两道,花了20分钟,剩下的100分钟也不知道哪去了(逃 A.B两道水题没什么说的.那我们从C题开始吧: C. Anton and Making Potions 题意: 制作n瓶药水,初始时每制作一瓶花费x秒,有两类法术,第一类(包含m个法术)使制作每瓶药的时间由x变为a[i] (a[i] <

Codeforces Round #379 (Div. 2) 解题报告

题目地址 本次CF是在今天早上深夜进行,上午有课就没有直接参加.今天早上上课坐到后排参加了virtual participation.这次CF前面的题目都非常的水,不到10分钟就轻松过了前两题,比较郁闷的是之后一直卡在C,开始是脑残的没有用二分TLE,后来又是因为一个常数打错而一直WA--于是模拟赛就只过了2道题(太弱了orz).时间到了后很快发现了脑残错误,终于A了C题.下午上完课回到宿舍看D题才发现D题水的不行,很快就A了.不过E和F就比较超出我现在知识范围了,暂时就先放下.第一次参加vir

Codeforces Round #288 (Div. 2) B. Anton and currency you all know

B. Anton and currency you all know time limit per test 0.5 seconds memory limit per test 256 megabytes input standard input output standard output Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that

Codeforces Round #379(div 2)

A.B:=w= C: 题意: 你需要制作n瓶药水,每一瓶药水需要x秒. 你现在有m种A魔法,花费b[i],使得每一瓶药水的花费代价降为a[i],只能用一次. 有K种B魔法,花费d[i],使得瞬间制作好c[i]瓶药水,只能用一次. 你最多花费s的魔法值 问你最快完成要多少秒. 分析:题目所给的B魔法都是递增的,容易想到枚举用哪个A魔法,二分对应的B魔法,因为在MP充足的情况下,减少的药水越多对ans越有利 D: 题意:一个无限大的平面,给你一个King的位置,再给你其他棋子的位置,问你这个King