补提报告...
图着色问题 (25分)
这题需要注意的点是,题目中说无向图的颜色书为k个,那么这个图的颜色多了少了都是不对的。
#include <bits/stdc++.h> using namespace std; const int N = 550; int n, m, k, t, v[N], e[N][N]; bool vis[N]; int main() { cin >> n >> m >> k; while(m --){ int x, y; cin >> x >> y; e[x][y] = 1; e[y][x] = 1; } cin >> t; while(t --){ memset(vis, 0, sizeof(vis)); int cnt = 0; for(int i = 1;i <= n; ++ i){ int x; cin >> x; v[i] = x; if(!vis[x]) vis[x] = true, cnt ++; } int f = 0; for(int i = 1;i <= n; ++ i){ for(int j = 1;j <= n; ++ j){ if(e[i][j] && v[i] == v[j]) f = 1; } } if(cnt != k) f = 1; if(!f) puts("Yes"); else puts("No"); } }
愿天下有情人都是失散多年的兄妹 (25分)
需要注意的是,他的父母也要分出来男女。(毕竟可以离婚嘛...)
#include <bits/stdc++.h> using namespace std; const int N = 100010; struct node{ char f; int p1, p2; }a[N]; int n, m; vector<int> s1, s2; void dfs(vector<int>& s, int x, int y, int deep){ if(deep > 4) return ; if(x == -1 && y == -1) return ; if(x != -1) s.push_back(x); if(y != -1) s.push_back(y); int x1, y1, x2, y2; if(x == -1){ x1 = -1; y1 = -1; } else x1 = a[x].p1, y1 = a[x].p2; if(y == -1){ x2 = -1; y2 = -1; } else x2 = a[y].p1, y2 = a[y].p2; dfs(s, x1, y1, deep + 1); dfs(s, x2, y2, deep + 1); } bool check(int x, int y){ s1.clear(), s2.clear(); dfs(s1, a[x].p1, a[x].p2, 1); dfs(s2, a[y].p1, a[y].p2, 1); for(int i = 0;i < s1.size(); ++ i){ for(int j = 0;j < s2.size(); ++ j){ if(s1[i] == s2[j]) return false; } } return true; } int main() { cin >> n; for(int i = 1;i <= n; ++ i){ int x, id, p1, p2; char ch; cin >> id >>ch >> p1 >> p2; a[id].f = ch; a[id].p1 = p1; a[id].p2 = p2; a[p1].f = 'M'; a[p2].f = 'F'; if(!a[p1].p1) a[p1].p1 = -1; if(!a[p1].p2) a[p1].p2 = -1; if(!a[p2].p1) a[p2].p1 = -1; if(!a[p2].p2) a[p2].p2 = -1; } cin >> m; while(m --){ int x, y; cin >> x >> y; if(a[x].f == a[y].f){ puts("Never Mind"); continue; } check(x, y) == true ? puts("Yes") : puts("No"); } }
肿瘤诊断 (30分)
三个方向的bfs
#include <bits/stdc++.h> using namespace std; struct node{ int x, y, z; }; int m, n, l, t, a[80][1450][150]; int tx[10] = {0, 0, -1, 1, 0, 0}; int ty[10] = {1, -1, 0, 0, 0, 0}; int tz[10] = {0, 0, 0, 0, 1, -1}; bool vis[80][1450][150]; int bfs(int x, int y, int z) { int cnt = 0; vis[x][y][z] = true; queue<node> q; q.push({x, y, z}); cnt ++; while(q.size()) { node now = q.front(); q.pop(); for(int i = 0;i < 6; ++ i) { int nx = now.x + tx[i], ny = now.y + ty[i], nz = now.z + tz[i]; if(!vis[nx][ny][nz] && a[nx][ny][nz]) q.push({nx, ny, nz}), vis[nx][ny][nz] = true, cnt ++; } } return cnt; } int main() { cin >> m >> n >> l >> t; for(int i = 1;i <= l; ++ i) for(int j = 1;j <= m; ++ j) for(int k = 1;k <= n; ++ k) cin >> a[i][j][k]; int res = 0; for(int i = 1;i <= l; ++ i) for(int j = 1;j <= m; ++ j) for(int k = 1;k <= n; ++ k){ if(a[i][j][k] && !vis[i][j][k]){ int tmp = bfs(i, j, k); if(tmp >= t) res += tmp; } } cout << res << endl; }
红色警报 (25分)
每删除一个点判断一次是否比前面的“区域”多
#include <bits/stdc++.h> //代码看师哥的,自己搜不明白QAQ using namespace std; const int N = 550; int n, m, k, a[N][N]; bool vis[N], e[N]; void dfs(int s) { vis[s] = true; for(int i = 0;i < n; ++ i){ if(a[s][i] && !vis[i]) dfs(i); } } int get_sum() { int cnt = 0; memset(vis, 0, sizeof(vis)); for(int i = 0;i < n; ++ i) { if(!vis[i] && !e[i]) dfs(i), cnt ++; } return cnt; } int main() { cin >> n >> m; while(m --) { int x, y; cin >> x >> y; a[x][y] = 1; a[y][x] = 1; } cin >> k; int cnt1 = get_sum(), cnt2, cnt = 0; while(k --) { int x; cin >> x; e[x] = true; for(int i = 0;i < n; ++ i) if(a[x][i]) a[x][i] = a[i][x] = 0; cnt2 = get_sum(); if(cnt1 >= cnt2) printf("City %d is lost.\n", x); else printf("Red Alert: City %d is lost!\n", x); if(++cnt == n) puts("Game Over."); cnt1 = cnt2; } }