2025.8.20总结
T0:
赛时做出,总体没什么问题,是之前讲过的题。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, s, t, ans;
vector<int> g[N];
bool vis[N], locked[N];
void dfs(int u) {
if (locked[u]) return;
if (vis[u]) return;
vis[u] = true;
for (auto v : g[u]) dfs(v);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
scanf("%d%d", &s, &t);
dfs(s);
if (!vis[t]) {
puts("-1");
return 0;
}
for (int i = 1; i <= n; i++) {
if (i == s || i == t) continue;
locked[i] = true;
fill(vis + 1, vis + n + 1, 0);
dfs(s);
if (!vis[t]) ans++;
locked[i] = false;
}
printf("%d\n", ans);
}T1:
赛时做出,二分加模拟,,没有什么问题。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, a[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int l = 0, r = 1e9;
auto check = [&](long long x) -> bool {
for (int i = 1; i <= n; i++) {
x += a[i];
if (x <= 0) return false;
}
return true;
};
while (l + 1 < r) {
int mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid;
}
printf("%d\n", r);
}T2:
赛时未做出,赛时想到正解,但是把自己思路否掉了,就没有做出来。
结论就是
代码:
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
scanf("%d", &n);
printf("%d\n", __lg(n) + 1);
return 0;
}T3:
赛时做出,就是DP。
状态: 表示传了 次球,传到第 个人手中的方案数。
转移:
和 表示 左右两边的人。
答案: 。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 40;
int n, m;
long long f[N][N];
int main() {
scanf("%d%d", &n, &m);
f[1][0] = 1;
for (int j = 1; j <= m; j++) for (int i = 1; i <= n; i++) {
int l, r;
if (i == 1) l = n;
else l = i - 1;
if (i == n) r = 1;
else r = i + 1;
f[i][j] = f[l][j - 1] + f[r][j - 1];
}
printf("%lld\n", f[1][m]);
}T4:
赛时骗分,一分不得,补出了,思路就是先找岛屿,在从岛屿出发往外面走,如果走出去了,就说明不是子岛屿,可以计入答案。
代码:
#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
using db = double;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define frep(i, b, a) for (int i = b; i >= a; i--)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define s second
#define f first
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 62LL) - 1;
const int mod = 998244353;
using namespace std;
const int N = 60;
const int d4[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
const int d8[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}};
int t, n, m, ans, idx;
bool vis[N][N], vis1[N][N];
char s[N][N];
void dfs(int x, int y) {
vis[x][y] = idx;
rep(i, 0, 3) {
int xx = x + d4[i][0], yy = y + d4[i][1];
if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
if (vis[xx][yy] || s[xx][yy] == '0') continue;
dfs(xx, yy);
}
}
bool dfs1(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m) return true;
vis1[x][y] = true;
rep(i, 0, 7) {
int xx = x + d8[i][0], yy = y + d8[i][1];
if (s[xx][yy] == '1' && vis[xx][yy] != idx) continue;
if (vis1[xx][yy]) continue;
if (dfs1(xx, yy)) return true;
}
return false;
}
void solve() {
scanf("%d%d", &n, &m);
rep(i, 1, n) scanf("%s", s[i] + 1);
idx = 0, ans = 0;
rep(i, 1, n) fill(vis[i] + 1, vis[i] + m + 1, 0);
rep(i, 1, n) rep(j, 1, m) {
if (vis[i][j] || s[i][j] == '0') continue;
idx++;
dfs(i, j);
rep(i, 1, n) fill(vis1[i] + 1, vis1[i] + m + 1, 0);
if (dfs1(i, j)) ans++;
}
printf("%d\n", ans);
}
int main() {
scanf("%d", &t);
while (t--) solve();
}总结:
这次模拟赛还可以,没有丢分。