A - Can't Wait for Holiday
Description
Solution
模拟水题。
B - ROT N
Description
Solution
模拟水题。
C - Buy an Integer
Description
Solution
二分水题,控制一下上界。
1 #include <algorithm> 2 #include <cctype> 3 #include <cmath> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <iostream> 8 #include <map> 9 #include <numeric> 10 #include <queue> 11 #include <set> 12 #include <stack> 13 #if __cplusplus >= 201103L 14 #include <unordered_map> 15 #include <unordered_set> 16 #endif 17 #include <vector> 18 #define lson rt << 1, l, mid 19 #define rson rt << 1 | 1, mid + 1, r 20 #define LONG_LONG_MAX 9223372036854775807LL 21 #define pblank putchar(' ') 22 #define ll LL 23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) 24 using namespace std; 25 typedef long long ll; 26 typedef long double ld; 27 typedef unsigned long long ull; 28 typedef pair<int, int> P; 29 ll n, m, k, a, b; 30 const int maxn = 1e5 + 10; 31 template <class T> 32 inline T read() 33 { 34 int f = 1; 35 T ret = 0; 36 char ch = getchar(); 37 while (!isdigit(ch)) 38 { 39 if (ch == '-') 40 f = -1; 41 ch = getchar(); 42 } 43 while (isdigit(ch)) 44 { 45 ret = (ret << 1) + (ret << 3) + ch - '0'; 46 ch = getchar(); 47 } 48 ret *= f; 49 return ret; 50 } 51 template <class T> 52 inline void write(T n) 53 { 54 if (n < 0) 55 { 56 putchar('-'); 57 n = -n; 58 } 59 if (n >= 10) 60 { 61 write(n / 10); 62 } 63 putchar(n % 10 + '0'); 64 } 65 template <class T> 66 inline void writeln(const T &n) 67 { 68 write(n); 69 puts(""); 70 } 71 template <typename T> 72 void _write(const T &t) 73 { 74 write(t); 75 } 76 template <typename T, typename... Args> 77 void _write(const T &t, Args... args) 78 { 79 write(t), pblank; 80 _write(args...); 81 } 82 template <typename T, typename... Args> 83 inline void write_line(const T &t, const Args &... data) 84 { 85 _write(t, data...); 86 puts(""); 87 } 88 inline ll d(ll x) 89 { 90 ll res = 0; 91 while (x) 92 { 93 ++res; 94 x /= 10; 95 } 96 return res; 97 } 98 inline int judge(ll x) 99 { 100 return a * x + d(x) * b <= n; 101 } 102 int main(int argc, char const *argv[]) 103 { 104 #ifndef ONLINE_JUDGE 105 // freopen("in.txt", "r", stdin); 106 // freopen("out.txt", "w", stdout); 107 #endif 108 109 fastIO; 110 cin >> a >> b >> n; 111 ll l = 1, r = 1e9, res = 0; 112 while (l <= r) 113 { 114 ll mid = l + r >> 1; 115 if (judge(mid)) 116 { 117 res = mid; 118 l = mid + 1; 119 } 120 else 121 r = mid - 1; 122 } 123 cout << res; 124 return 0; 125 }
D - Coloring Edges on Tree
Description
给出一个N节点的树,要求一种染色方案使得颜色最少且满足相邻边的颜色不同。
Solution
dfs找出儿子最多的一个节点作为根节点。
从根节点出发开始贪心染色,注意颜色和父节点颜色不同。
1 #include <algorithm> 2 #include <cctype> 3 #include <cmath> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <ctime> 8 #include <iostream> 9 #include <map> 10 #include <numeric> 11 #include <queue> 12 #include <set> 13 #include <stack> 14 #if __cplusplus >= 201103L 15 #include <unordered_map> 16 #include <unordered_set> 17 #endif 18 #include <vector> 19 #define lson rt << 1, l, mid 20 #define rson rt << 1 | 1, mid + 1, r 21 #define LONG_LONG_MAX 9223372036854775807LL 22 #define pblank putchar(' ') 23 #define ll LL 24 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) 25 using namespace std; 26 typedef long long ll; 27 typedef long double ld; 28 typedef unsigned long long ull; 29 typedef pair<int, int> P; 30 int n, m, k; 31 const int maxn = 1e5 + 10; 32 template <class T> 33 inline T read() 34 { 35 int f = 1; 36 T ret = 0; 37 char ch = getchar(); 38 while (!isdigit(ch)) 39 { 40 if (ch == '-') 41 f = -1; 42 ch = getchar(); 43 } 44 while (isdigit(ch)) 45 { 46 ret = (ret << 1) + (ret << 3) + ch - '0'; 47 ch = getchar(); 48 } 49 ret *= f; 50 return ret; 51 } 52 template <class T> 53 inline void write(T n) 54 { 55 if (n < 0) 56 { 57 putchar('-'); 58 n = -n; 59 } 60 if (n >= 10) 61 { 62 write(n / 10); 63 } 64 putchar(n % 10 + '0'); 65 } 66 template <class T> 67 inline void writeln(const T &n) 68 { 69 write(n); 70 puts(""); 71 } 72 template <typename T> 73 void _write(const T &t) 74 { 75 write(t); 76 } 77 template <typename T, typename... Args> 78 void _write(const T &t, Args... args) 79 { 80 write(t), pblank; 81 _write(args...); 82 } 83 template <typename T, typename... Args> 84 inline void write_line(const T &t, const Args &... data) 85 { 86 _write(t, data...); 87 puts(""); 88 } 89 struct Node 90 { 91 int idx, to, nxt; 92 } edg[maxn << 1]; 93 int head[maxn], son[maxn]; 94 int tot, rt, maxx = 0; 95 void add(int x, int y, int idx) 96 { 97 edg[tot].idx = idx; 98 edg[tot].to = y; 99 edg[tot].nxt = head[x]; 100 head[x] = tot++; 101 } 102 int col[maxn]; 103 void dfs(int u, int pre) 104 { 105 for (int i = head[u]; ~i; i = edg[i].nxt) 106 { 107 int v = edg[i].to; 108 if (v == pre) 109 continue; 110 dfs(v, u); 111 ++son[u]; 112 } 113 if (son[u] + (u != 1) > maxx) 114 { 115 maxx = son[u] + (u != 1); 116 rt = u; 117 } 118 } 119 void dfs2(int u, int fa, int pre) 120 { 121 int x = 1; 122 if (x == pre) 123 ++x; 124 if (x > maxx) 125 x = 1; 126 for (int i = head[u]; ~i; i = edg[i].nxt) 127 { 128 int v = edg[i].to; 129 int num = edg[i].idx; 130 if (col[num] || v == fa) 131 continue; 132 col[num] = x; 133 ++x; 134 if (x > maxx) 135 x = 1; 136 if (x == pre) 137 ++x; 138 if (x > maxx) 139 x = 1; 140 } 141 for (int i = head[u]; ~i; i = edg[i].nxt) 142 { 143 int v = edg[i].to; 144 int num = edg[i].idx; 145 if (v != fa) 146 dfs2(v, u, col[num]); 147 } 148 } 149 int main(int argc, char const *argv[]) 150 { 151 #ifndef ONLINE_JUDGE 152 // freopen("in.txt", "r", stdin); 153 // freopen("out.txt", "w", stdout); 154 #endif 155 srand(time(nullptr)); 156 n = read<int>(); 157 memset(head, -1, sizeof(int) * (n + 1)); 158 for (int i = 0; i < n - 1; i++) 159 { 160 int x = read<int>(), y = read<int>(); 161 add(x, y, i); 162 add(y, x, i); 163 } 164 dfs(1, 0); 165 writeln(maxx); 166 dfs2(rt, 0, 0); 167 for (int i = 0; i < n - 1; i++) 168 writeln(col[i]); 169 return 0; 170 }