constint N = 110; int a[N], ans; int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; unordered_set<int> st;
boolcheck(int date){ if (st.count(date)) returnfalse; // 判断是否重复 int day = date%100; int month = date/100%100; if (day < 1 || month > 12 || month < 0) returnfalse; if (day <= months[month]) returntrue; returnfalse; }
// idx表示遍历到数组的下标,len表示当前日期长度,date表示当前日期 voiddfs(int idx, int len, int date){ if (idx == 100) return; if (len == 8) { if (check(date)) ++ans; st.insert(date); return; }
constint N = 52; char g[N][N]; int t, n, m; // 实际范围为 [1, n], 遍历范围为[0, n+1] int d8[8][2] = {{1, 1}, {1, -1}, {1, 0}, {0, 1}, {0, -1}, {-1, 1}, {-1, -1}, {-1, 0}}; // 八联通 int d4[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // 四联通
boolcheck1(int x, int y){ if (x < 0 || x > n+1 || y < 0 || y > m+1) returnfalse; // 越界 if (g[x][y] == '0') returntrue; returnfalse; }
voiddfs1(int x, int y){ g[x][y] = '2'; // 不是陆地,也不是海水 for (int i = 0; i < 8; ++i) { int nx = x + d8[i][0], ny = y + d8[i][1]; if (check1(nx, ny)) dfs1(nx, ny); } }
boolcheck2(int x, int y){ if (x < 0 || x > n+1 || y < 0 || y > m+1) returnfalse; // 越界 if (g[x][y] == '1') returntrue; returnfalse; }
voiddfs2(int x, int y){ g[x][y] = '2'; // 不是陆地,也不是海水 for (int i = 0; i < 4; ++i) { int nx = x + d4[i][0], ny = y + d4[i][1]; if (check2(nx, ny)) dfs2(nx, ny); } }
voidsolve(){ // 初始化 memset(g, '0', sizeof(g)); int res = 0;
cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> g[i][j]; } }
dfs1(0, 0); // 海水染色
// 将环内的海水变为普通陆地 for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (g[i][j] == '0') g[i][j] = '1'; } }
// 岛屿个数统计 for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (check2(i, j)) { res++; dfs2(i, j); } } }
int n, m; int h[N], e[M], w[M], ne[M], idx; // 存树 int depth[N], fa[N][18]; // depth记录节点深度,fa记录向上跳的节点 int q[N]; // 队列,用于宽度优先搜索 ll dist[N]; // 记录节点与根节点的距离 int query[N]; // 记录浏览顺序 ll sum = 0; // 不跳过的总花费
voidadd(int a, int b, int c){ e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++; }
while (hh <= tt) { int t = q[hh++]; for (int i = h[t]; ~i; i = ne[i]) { int j = e[i]; if (depth[j] > depth[t] + 1) { dist[j] = dist[t] + w[i]; depth[j] = depth[t] + 1; q[++tt] = j;
fa[j][0] = t; for (int k = 1; k <= 17; ++k) { fa[j][k] = fa[fa[j][k-1]][k-1]; } } } } }
intlca(int a, int b){ // 跳到同一层 if (depth[a] < depth[b]) swap(a, b); for (int k = 17; k >= 0; --k) if (depth[fa[a][k]] >= depth[b]) a = fa[a][k]; if (a == b) return a;
// 一起往上跳 for (int k = 17; k >= 0; --k) if (fa[a][k] != fa[b][k]) { a = fa[a][k]; b = fa[b][k]; } return fa[a][0]; }
ll cost(int a, int b){ int anc = lca(query[a], query[b]); return dist[query[a]] + dist[query[b]] - 2*dist[anc]; }
intmain(){ cin >> n >> m; memset(h, -1, sizeof(h)); int a, b, c; for (int i = 0; i < n-1; ++i) { cin >> a >> b >> c; add(a, b, c), add(b, a, c); }
bfs(1); // 随便选个编号为1的节点做根节点
// 输入浏览顺序 for (int i = 1; i <= m; ++i) cin >> query[i]; // 计算总花费 for (int i = 2; i <= m; ++i) sum += cost(i, i-1); // 跳过第一个 cout << sum - cost(1, 2) << " "; // 跳过第i个 for (int i = 2; i < m; ++i) cout << sum - cost(i, i+1) - cost(i, i-1) + cost(i-1, i+1) << " "; // 跳过最后一个 cout << sum - cost(m, m-1);
int n, m; int h[N], e[M], ne[M], idx; // 存图 int depth[N], fa[N][16]; // depth为深度,fa为父节点 int q[N]; // 队列 map<pii, int> id; // 记录边的编号 int d[N]; // 差分数组 int ans = -1; // 最终结果编号,没有答案返回-1
voidadd(int a, int b){ e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
while (hh <= tt) { int t = q[hh++]; for (int i = h[t]; ~i; i = ne[i]) { int j = e[i]; if (depth[j] > depth[t] + 1) { depth[j] = depth[t] + 1; q[++tt] = j;
fa[j][0] = t; for (int k = 1; k <= 15; ++k) fa[j][k] = fa[fa[j][k-1]][k-1]; } } } }
intlca(int a, int b){ // 跳到同一层 if (depth[a] < depth[b]) swap(a, b); for (int k = 15; k >= 0; --k) if (depth[fa[a][k]] >= depth[b]) a = fa[a][k]; if (a == b) return a;
// 一起往上跳 for (int k = 15; k >= 0; --k) if (fa[a][k] != fa[b][k]) { a = fa[a][k]; b = fa[b][k]; } return fa[a][0]; }
intget_sum(int u){ int sum = d[u]; for (int i = h[u]; ~i; i = ne[i]) { int j = e[i]; if (depth[j] == depth[u] + 1) { int res = get_sum(j); sum += res; } } if (sum == m && ans < id[{fa[u][0], u}]) ans = id[{fa[u][0], u}]; return sum; }
intmain(){ cin >> n >> m; memset(h, -1, sizeof(h)); for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; add(a, b), add(b, a); // 无向边 id[{a, b}] = i; id[{b, a}] = i; // 记录边的编号 }
int root = 1; // 选择节点编号为1的节点作为根节点
bfs(root);
for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; d[a] ++, d[b] ++; d[lca(a, b)] -= 2; }