「模拟测试」20171023

T1 Fibonacci

询问一个数能否被分成两个 Fibonacci 数的乘积。

题解

数很小,$O(n ^ 2)$ 预处理,然后二分回答询问就好了。
预处理要超过 $40$,别问我怎么知道的

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Copyright (c) 2017, xehoth
* All rights reserved.
* 「SuperOJ 2001」Fibonacci 23-10-2017
*
* @author xehoth
*/
#include <bits/stdc++.h>
namespace {
const int MAXN = 100;
long long f[MAXN + 1];
std::vector<long long> vc;
inline void solve() {
f[0] = 0, f[1] = 1;
for (register int i = 2; i < 47; i++) f[i] = f[i - 1] + f[i - 2];
for (register int i = 0; i < 47; i++)
for (register int j = 0; j < 47; j++) vc.push_back(f[i] * f[j]);
std::sort(vc.begin(), vc.end()),
vc.erase(std::unique(vc.begin(), vc.end()), vc.end());
register int T;
std::cin >> T;
for (register long long x; T--;)
std::cin >> x,
std::cout << (std::binary_search(vc.begin(), vc.end(), x) ? "Yes\n"
: "No\n");
}
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(NULL), std::cout.tie(NULL);
solve();
return 0;
}

T2 一样远

给出一棵树,多次询问到 $A, B$ 距离相等的点的个数。

题解

分类讨论题:

  1. $u = v$ 答案为 $n$。
  2. $dep[u] = dep[v]$,去掉 $u, v$ 所在的子树的所有节点。
  3. $\text{dis}(u, v)$ 为奇数,答案为 $0$。
  4. 否则找中点,去掉对应子树大小。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright (c) 2017, xehoth
* All rights reserved.
* 「SuperOJ 2002」一样远 23-10-2017
* 倍增
* @author xehoth
*/
#include <bits/stdc++.h>
namespace IO {
inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
return s == t ? -1 : *s++;
}
template <typename T>
inline bool read(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return false;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig ? x = -x : 0;
return true;
}
inline void read(char &c) {
while (c = read(), isspace(c) && c != -1)
;
}
inline int read(char *buf) {
register int s = 0;
register char c;
while (c = read(), isspace(c) && c != -1)
;
if (c == -1) {
*buf = 0;
return -1;
}
do
buf[s++] = c;
while (c = read(), !isspace(c) && c != -1);
buf[s] = 0;
return s;
}
const int OUT_LEN = 1000000;
char obuf[OUT_LEN], *oh = obuf;
inline void print(char c) {
oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
*oh++ = c;
}
template <typename T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
x < 0 ? (print('-'), x = -x) : 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
}
}
inline void print(const char *s) {
for (; *s; s++) print(*s);
}
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }
struct InputOutputStream {
template <typename T>
inline InputOutputStream &operator>>(T &x) {
read(x);
return *this;
}
template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}
~InputOutputStream() { flush(); }
} io;
}
namespace {
using IO::io;
const int MAXN = 100000;
const int MAX_LOG = 18;
std::vector<int> edge[MAXN + 1];
typedef std::vector<int>::iterator Iterator;
inline void addEdge(const int u, const int v) {
edge[u].push_back(v), edge[v].push_back(u);
}
int in[MAXN + 1], out[MAXN + 1], fa[MAX_LOG][MAXN + 1], sz[MAXN + 1];
int n, m, dep[MAXN + 1], idx;
bool vis[MAXN + 1];
inline bool isAncestor(const int u, const int v) {
return in[u] <= in[v] && out[u] >= out[v];
}
void dfs(const int u) {
vis[u] = true, dep[u] = dep[fa[0][u]] + 1, sz[u] = 1, in[u] = idx++;
for (register int i = 1; i < MAX_LOG; i++)
fa[i][u] = fa[i - 1][fa[i - 1][u]];
for (register Iterator p = edge[u].begin(); p != edge[u].end(); p++)
if (!vis[*p]) fa[0][*p] = u, dfs(*p), sz[u] += sz[*p];
out[u] = idx++;
}
inline int bitUp(register int u, register int v) {
for (register int i = MAX_LOG - 1; i >= 0; i--)
!isAncestor(fa[i][u], v) ? u = fa[i][u] : 0;
return u;
}
inline int lca(const int u, const int v) {
return isAncestor(u, v) ? u : (isAncestor(v, u) ? v : fa[0][bitUp(u, v)]);
}
inline void query(int u, int v) {
if (u == v) {
io << n << '\n';
return;
}
register int l = lca(u, v);
if (dep[u] - dep[l] == dep[v] - dep[l]) {
io << n - sz[bitUp(u, l)] - sz[bitUp(v, l)] << '\n';
return;
}
dep[u] < dep[v] ? std::swap(u, v) : (void)0;
register int dis = dep[u] + dep[v] - 2 * dep[l];
if (dis & 1) {
io << "0\n";
return;
}
dis /= 2;
register int mid = u;
for (register int i = MAX_LOG - 1; i >= 0; i--)
dep[u] - dep[fa[i][mid]] < dis ? mid = fa[i][mid] : 0;
io << sz[fa[0][mid]] - sz[mid] << '\n';
}
inline void solve() {
io >> n, fa[0][1] = 1;
for (register int i = 1, u, v; i < n; i++) io >> u >> v, addEdge(u, v);
dfs(1), io >> m;
for (register int u, v; m--;) io >> u >> v, query(u, v);
}
}
int main() {
solve();
return 0;
}

T3 拆网线

给定一棵树,求最少保留多少条边,使得每个点至少连接另一个点。

题解

显然一条边连接两个点这种情况是最优的,我们从叶子节点开始贪心,用树状数组维护 $size$ 来维护是否可删,然后把这样的点对的个数记录下来,剩余的边就一条一条加进去就好了。

时间复杂度 $O(Tn \log n)$。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Copyright (c) 2017, xehoth
* All rights reserved.
* 「SuperOJ 2003」拆网线 23-10-2017
* 贪心 + 树状数组
* @author xehoth
*/
#include <bits/stdc++.h>
namespace IO {
inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
return s == t ? -1 : *s++;
}
template <typename T>
inline bool read(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return false;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig ? x = -x : 0;
return true;
}
inline void read(char &c) {
while (c = read(), isspace(c) && c != -1)
;
}
inline int read(char *buf) {
register int s = 0;
register char c;
while (c = read(), isspace(c) && c != -1)
;
if (c == -1) {
*buf = 0;
return -1;
}
do
buf[s++] = c;
while (c = read(), !isspace(c) && c != -1);
buf[s] = 0;
return s;
}
const int OUT_LEN = 1000000;
char obuf[OUT_LEN], *oh = obuf;
inline void print(char c) {
oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
*oh++ = c;
}
template <typename T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
x < 0 ? (print('-'), x = -x) : 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
}
}
inline void print(const char *s) {
for (; *s; s++) print(*s);
}
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }
struct InputOutputStream {
template <typename T>
inline InputOutputStream &operator>>(T &x) {
read(x);
return *this;
}
template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}
~InputOutputStream() { flush(); }
} io;
}
namespace {
using IO::io;
const int MAXN = 100000;
int fa[MAXN + 1], deg[MAXN + 1], tot, edges, n, k, sz[MAXN + 1];
int in[MAXN + 1], out[MAXN + 1], idx;
int d[MAXN + 1];
bool vis[MAXN + 1];
inline void modify(register int k, register int v) {
for (; k <= n; k += k & -k) d[k] += v;
}
inline int query(register int k) {
register int ret = 0;
for (; k; k ^= k & -k) ret += d[k];
return ret;
}
inline int query(int l, int r) { return query(r) - query(l - 1); }
std::vector<int> edge[MAXN + 1];
typedef std::vector<int>::iterator Iterator;
inline void addEdge(const int u, const int v) {
edge[u].push_back(v), edge[v].push_back(u);
}
void dfs(const int u) {
sz[u] = 1, in[u] = ++idx;
for (register Iterator p = edge[u].begin(); p != edge[u].end(); p++) {
if (*p != fa[u]) {
dfs(*p), sz[u] += sz[*p];
}
}
out[u] = idx;
}
void decPairDegOne(const int u) {
for (register Iterator p = edge[u].begin(); p != edge[u].end(); p++) {
if (*p != fa[u]) {
decPairDegOne(*p);
}
}
if (!vis[u] && !vis[fa[u]] && deg[u] == 1 &&
query(in[fa[u]], out[fa[u]]) >= 2) {
deg[u]--, deg[fa[u]]--;
vis[u] = vis[fa[u]] = true;
if (fa[fa[u]]) deg[fa[fa[u]]]--;
modify(in[fa[u]], -1), modify(in[u], -1);
tot += 2, edges++;
}
}
inline void solveCase() {
io >> n >> k;
for (register int i = 0; i <= n; i++) edge[i].clear();
idx = 0;
memset(deg, 0, sizeof(int) * (n + 1));
memset(d, 0, sizeof(int) * (n + 1));
tot = edges = 0;
memset(vis, 0, sizeof(bool) * (n + 1));
for (register int i = 1, u; i < n; i++)
io >> u, fa[i + 1] = u, deg[u]++, deg[i + 1]++;
for (register int i = 2; i <= n; i++) addEdge(i, fa[i]);
dfs(1);
for (register int i = 1; i <= n; i++) modify(in[i], 1);
decPairDegOne(1);
if (k % 2 == 0 && tot >= k) {
io << k / 2 << '\n';
return;
}
if (k % 2 == 1 && tot >= k - 1) {
io << k / 2 + 1 << '\n';
return;
}
io << k - tot + edges << '\n';
}
inline void solve() {
register int T;
io >> T;
while (T--) solveCase();
}
}
int main() {
solve();
return 0;
}
分享到