ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#212744 | #3829. C | wangliwen123456 | Compile Error | / | / | C++ | 1.9kb | 2024-10-20 10:25:52 | 2024-10-20 12:40:51 |
answer
#include <iostream>
#include <vector>
using namespace std;
class Tree {
public:
int n;
vector<vector<int>> adj;
Tree(int n) : n(n), adj(n + 1) {}
void addEdge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
int dfs(int u, int parent, vector<int>& subtreeSize) {
int size = 1;
for (int v : adj[u]) {
if (v!= parent) {
size += dfs(v, u, subtreeSize);
}
}
subtreeSize[u] = size;
return size;
}
int findCentroid() {
vector<int> subtreeSize(n + 1);
dfs(1, -1, subtreeSize);
int centroid = -1;
int minSize = n;
function<void(int, int)> find = [&](int u, int parent) {
bool isCentroid = true;
int maxSubtree = 0;
for (int v : adj[u]) {
if (v!= parent) {
if (subtreeSize[v] > n / 2) {
isCentroid = false;
}
maxSubtree = max(maxSubtree, subtreeSize[v]);
}
}
if (n - subtreeSize[u] > n / 2) {
isCentroid = false;
}
if (isCentroid && maxSubtree < minSize) {
centroid = u;
minSize = maxSubtree;
}
for (int v : adj[u]) {
if (v!= parent) {
find(v, u);
}
}
};
find(1, -1);
return centroid;
}
};
int solve(Tree& tree) {
int centroid = tree.findCentroid();
return tree.n - 1;
}
int main() {
int n;
cin >> n;
Tree tree(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
tree.addEdge(u, v);
}
cout << solve(tree) << endl;
return 0;
}
详细
answer.code:9:22: error: '>>' should be '> >' within a nested template argument list vector<vector<int>> adj;\x0d ^ answer.code: In member function 'int Tree::dfs(int, int, std::vector<int>&)': answer.code:20:22: error: range-based 'for' loops are not allowed in C++98 mode for (int v : adj[u]) {\x0d ^ answer.code: In member function 'int Tree::findCentroid()': answer.code:35:9: error: 'function' was not declared in this scope function<...