#include<bits/stdc++.h> usingnamespace std; #define int long long constint N = 200010;
structNode { int color; vector<int> child; unordered_map<int, int> color_cnt; };
Node node[N]; int ans = 0;
// 启发式合并函数 voidmerge(unordered_map<int, int>& a, unordered_map<int, int>& b) { if (a.size() < b.size()) swap(a, b); for (auto& [color, cnt] : b) { a[color] += cnt; } b.clear(); }
// DFS 遍历树 voiddfs(int u) { for (int v : node[u].child) { dfs(v); merge(node[u].color_cnt, node[v].color_cnt); } node[u].color_cnt[node[u].color]++;
bool flag = true; int cnt = -1; for (auto& [color, num] : node[u].color_cnt) { if (cnt == -1) cnt = num; elseif (cnt != num) { flag = false; break; } } if (flag) ans++; }
signedmain() { int n; cin >> n; for (int i = 1; i <= n; i++) { int color, parent; cin >> color >> parent; node[i].color = color; if (parent != 0) node[parent].child.push_back(i); }
#include<bits/stdc++.h> usingnamespace std; #define int long long int n, m; vector<int> a(50), last(50); int ans = 50; voiddfs(int sum, int i, int cnt) { if(cnt >= ans) return; if(sum == m) ans = min(ans, cnt); if(i > n || sum >= m || sum + last[i] < m) return;
dfs(sum + a[i], i + 1, cnt);//取一整个 dfs(sum + a[i] / 2, i + 1, cnt + 1);//取一半 dfs(sum, i + 1, cnt);//不取 } signedmain() { cin >> n >> m; m <<= 1;//为了消除小数点的影响 for(int i = 1; i <= n; i++) cin >> a[i], a[i] <<= 1; sort(a.begin() + 1, a.begin() + n + 1, greater<int>()); for(int i = n; i >= 1; i--) last[i] = last[i + 1] + a[i];
#include<bits/stdc++.h> #define int long long usingnamespace std; constint N = 15; int n, m; char g[N][N]; int ans[N][N], st[N][N]; boolcheck(int x, int y) { if(g[x][y] == '_') returntrue; int cnt = 0; for(int i = x - 1; i <= x + 1; i++) for(int j = y - 1; j <= y + 1; j++) cnt += ans[i][j]; if(cnt == g[x][y] - '0') returntrue; elsereturnfalse; } voiddfs(int x, int y) { ans[x][y] = 0; int ok = 1; if(x > 1 && y > 1) ok &= check(x - 1, y - 1); if(x > 1 && y == m) ok &= check(x - 1, y); if(x == n && y > 1) ok &= check(x, y - 1); if(x == n && y == m) ok &= check(x, y); if(ok) { if(x == n && y == m) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) cout << ans[i][j]; cout << endl; } exit(0); } if(y == m) dfs(x + 1, 1); elsedfs(x, y + 1); } ans[x][y] = 1; ok = 1; if(x > 1 && y > 1) ok &= check(x - 1, y - 1); if(x > 1 && y == m) ok &= check(x - 1, y); if(x == n && y > 1) ok &= check(x, y - 1); if(x == n && y == m) ok &= check(x, y); if(ok) { if(x == n && y == m) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) cout << ans[i][j]; cout << endl; } exit(0); } if(y == m) dfs(x + 1, 1); elsedfs(x, y + 1); } } signedmain() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> g[i][j]; dfs(1, 1); return0; }