CF1375G. Tree Modification(贪心,黑白染色)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                CF1375G. Tree Modification(贪心,黑白染色)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                CF1375G. Tree Modification
Solution
假設我們取定了根,那么只可能從深度大的點接到深度小的點,我們每次取一個高度為2的子樹接到該子樹的父親,這樣取一定不劣,操作次數相當于是偶數深度點(根深度為0)的個數減一。
注意到所有奇數深度的點為根的答案都相同,偶數同理。
 因此整合之后,相當于是奇數深度和偶數深度點的個數的較小值減一。
時間復雜度O(n)O(n)O(n)。
Code
#include <bits/stdc++.h>using namespace std;template<typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template<typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; }#define MP(A,B) make_pair(A,B) #define PB(A) push_back(A) #define SIZE(A) ((int)A.size()) #define LEN(A) ((int)A.length()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define fi first #define se secondtypedef long long ll; typedef unsigned long long ull; typedef long double lod; typedef pair<int, int> PR; typedef vector<int> VI; const lod eps = 1e-9; const lod pi = acos(-1); const int oo = 1 << 30; const ll loo = 1ll << 60; const int mods = 1e9 + 7; const int inv2 = (mods + 1) >> 1; const int MAXN = 500005; const int INF = 0x3f3f3f3f; //1061109567 /*--------------------------------------------------------------------*/namespace FastIO{constexpr int SIZE = (1 << 21) + 1;int num = 0, f;char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf, *oT = obuf + SIZE - 1, c;#define gc() (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS ++)) : *iS ++)inline void flush() {fwrite(obuf, 1, oS - obuf, stdout);oS = obuf;}inline void putc(char c) {*oS ++ = c;if (oS == oT) flush();}inline void getc(char &c) {for (c = gc(); c != '<' && c != '>' && c != EOF; c = gc());}inline void reads(char *st) {char c;int n = 0;getc(st[++ n]);for (c = gc(); c == '<' || c == '>' ; c = gc()) st[++ n] = c;st[n + 1] = '\0';}template<class I>inline void read(I &x) {for (f = 1, c = gc(); c < '0' || c > '9' ; c = gc()) if (c == '-') f = -1;for (x = 0; c >= '0' && c <= '9' ; c = gc()) x = (x << 3) + (x << 1) + (c & 15);x *= f;}template<class I>inline void print(I x) {if (x < 0) putc('-'), x = -x;if (!x) putc('0');while (x) que[++ num] = x % 10 + 48, x /= 10;while (num) putc(que[num --]);}struct Flusher_{~Flusher_(){flush();}} io_Flusher_; } using FastIO :: read; using FastIO :: putc; using FastIO :: reads; using FastIO :: print;vector<int> e[MAXN]; int dep[MAXN], num[2]; void dfs(int x, int father) {dep[x] = dep[father] + 1;++ num[dep[x] & 1];for (auto v : e[x]) {if (v == father) continue;dfs(v, x);} } signed main() { #ifndef ONLINE_JUDGEfreopen("a.in", "r", stdin); #endifint n;read(n);for (int i = 1, u, v; i < n ; ++ i) read(u), read(v), e[u].PB(v), e[v].PB(u); dfs(1, 0);print(min(num[0], num[1]) - 1);return 0; }總結
以上是生活随笔為你收集整理的CF1375G. Tree Modification(贪心,黑白染色)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: CF1365G Secure Passw
- 下一篇: 幽门螺杆菌四联疗法
