晚间测试6
作者:互联网
T1 : 山洞
- dp的十分显然,考试在最后五分钟给成了滚动数组,然后就没有清空。。。
- 100分只需要矩阵优化即可。
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int k = 0, f = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) k = k * 10 + ch - '0';
return k * f;
}
#define int long long
int f[5][1005];
const int mod = 1e9 + 7;
signed main() {
freopen("cave.in", "r", stdin);
freopen("cave.out", "w", stdout);
int n = read(), m = read();
if (m > 1000) return !puts("0");
int add = (m / n + 1) * n;
f[0][0] = 1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < n; j++) {
int last = (i - 1) & 1, now = i & 1;
f[now][j] = (f[last][(1ll * j - i + 1ll * n * m) % n] % mod + f[last][(j + i) % n]) % mod;
}
}
cout << f[m & 1][0] % mod << endl;
}
T2 : beauty
- 统计每条边儿子方向上的关键点数量,设为 \(cnt_i\),那么另一个方向上的关键点数量为 \(2k - cnt_i\),所以 \(ans = \sum_{i = 1}^{n - 1}min(cnt[i], 2 * k - cnt[i])\),显然答案不会大于这个值,因为跨过每一条边的路径数不会超过这个答案,构造出一个方案是可行的,找出树的重心然后把 \(i\) 和 \(i + k\) 匹配。
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int k = 0, f = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) k = k * 10 + ch - '0';
return k * f;
}
const int mod = 1e9 + 7;
const int maxn = 100000 * 4 + 100;
struct node { int to, next; } e[maxn];
int important[maxn], head[maxn], ecnt;
void add(int u, int v) { e[++ecnt] = (node){v, head[u]}; head[u] = ecnt; }
int size[maxn], dp[maxn], vis[maxn];
int n, k, a;
void dfs(int u, int f) {
if (important[u]) size[u] = 1;
// size[u] = 1;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (v == f) continue;
dfs(v, u);
size[u] += size[v];
}
}
int main() {
freopen("beauty.in", "r", stdin);
freopen("beauty.out", "w", stdout);
n = read(), k = read() * 2, a = read();
for (int i = 1; i <= k; i++) { int x = read(); important[x] = 1; }
for (int i = 1; i < n; i++) {
int u = read(), v = read();
add(u, v), add(v, u);
}
dfs(1, 0);
int ans = 0;
for (int i = 1; i <= n; i++) ans += min(size[i], k - size[i]);
cout << ans << endl;
}
标签:ch,int,read,maxn,测试,晚间,size,getchar 来源: https://www.cnblogs.com/hellohhy/p/13832256.html