帮我修改下面这个代码:#include <iostream> #include <queue> using namespace std; struct Node { int number; int father; int floor; bool bl = false; }; class dls { private: int n; Node* node; int** map; public: dls(int n):n(n){} void Map()//建立邻接矩阵的下三角并初始化 { map = new int* [n]; for (int i = 0; i < n; i++) map[i] = new int[i + 1]; for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) map[i][j] = 0; } void createGraph()//对邻接矩阵进行赋值 { cout << "请输入村庄的" << n - 1 << "条道路:" << endl; node = new Node[n]; for (int i = 0; i < n; i++) node[i].number = i; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; if (x >= y) map[x][y] = 1; else map[y][x] = 1; } } void BFSTree()//利用BFS建立树 { queue<int>qu; qu.push(0); node[0].father = 0; node[0].floor = 0; node[0].bl = true; while (!qu.empty()) { int x = qu.front(); qu.pop(); for (int i = 0; i < n; i++) { if (map[x][i] == 1 || map[i][x] == 1 && node[i].bl = false) { node[i].bl = true; node[i].father = x; node[i].floor = node[x].floor + 1; qu.push(i); } } } } int findFather(int m,int n)//寻找父亲结点 { int my_m = m; int my_n = n; int gap; if (node[m].floor > node[n].floor) { gap = node[m].floor - node[m].floor; for (int i = 0; i < gap; i++) my_m = node[m].father; } else { gap = node[n].floor - node[m].floor; for (int i = 0; i < gap; i++) my_n = node[n].father; } while (my_m != my_n) { my_m = node[m].father; my_n = node[n].father; } return m; } }; int main() { int T; int N; int M; cout << "请输入需要测试的组数:"; cin >> T; while (T--) { cout << "请输入村庄个数:"; cin >> N; dls ddd(N); ddd.Map(); ddd.createGraph(); ddd.BFSTree(); cout << "请输入需要测试的问题数:"; cin >> M; for (int i = 1; i <= M; i++) { int a, b, c; cout << "请依次输入abc的编号: "; cin >> a >> b >> c; int ab = ddd.findFather(a, b); int ac = ddd.findFather(a, c); int bc = ddd.findFather(b, c); if (ac == c && bc == c && ab == c) cout << "Yes" << endl; else if (ac == c && bc != c) cout << "Yes" << endl; else if (bc == c && ac != c) cout << "Yes" << endl; else cout << "No" << endl; } } return 0; }
时间: 2024-01-23 20:02:14 浏览: 21
代码中存在一些语法错误和逻辑问题,以下是修改后的代码:
```
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int number;
int father;
int floor;
bool bl = false;
};
class dls {
private:
int n;
Node* node;
int** map;
public:
dls(int n) :n(n) {
node = new Node[n];
}
void Map() {
// 建立邻接矩阵的下三角并初始化
map = new int* [n];
for (int i = 0; i < n; i++) {
map[i] = new int[i + 1];
for (int j = 0; j <= i; j++) {
map[i][j] = 0;
}
}
}
void createGraph() {
// 对邻接矩阵进行赋值
cout << "请输入村庄的" << n - 1 << "条道路:" << endl;
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
if (x >= y) {
map[x][y] = 1;
}
else {
map[y][x] = 1;
}
}
}
void BFSTree() {
// 利用 BFS 建立树
queue<int>qu;
qu.push(0);
node[0].father = 0;
node[0].floor = 0;
node[0].bl = true;
while (!qu.empty()) {
int x = qu.front();
qu.pop();
for (int i = 0; i < n; i++) {
if ((map[x][i] == 1 || map[i][x] == 1) && node[i].bl == false) {
node[i].bl = true;
node[i].father = x;
node[i].floor = node[x].floor + 1;
qu.push(i);
}
}
}
}
int findFather(int m, int n) {
// 寻找父亲结点
int my_m = m;
int my_n = n;
int gap;
if (node[m].floor > node[n].floor) {
gap = node[m].floor - node[n].floor;
for (int i = 0; i < gap; i++) {
my_m = node[my_m].father;
}
}
else {
gap = node[n].floor - node[m].floor;
for (int i = 0; i < gap; i++) {
my_n = node[my_n].father;
}
}
while (my_m != my_n) {
my_m = node[my_m].father;
my_n = node[my_n].father;
}
return my_m;
}
};
int main() {
int T;
int N;
int M;
cout << "请输入需要测试的组数:";
cin >> T;
while (T--) {
cout << "请输入村庄个数:";
cin >> N;
dls ddd(N);
ddd.Map();
ddd.createGraph();
ddd.BFSTree();
cout << "请输入需要测试的问题数:";
cin >> M;
for (int i = 1; i <= M; i++) {
int a, b, c;
cout << "请依次输入abc的编号: ";
cin >> a >> b >> c;
int ab = ddd.findFather(a, b);
int ac = ddd.findFather(a, c);
int bc = ddd.findFather(b, c);
if (ac == c && bc == c && ab == c) {
cout << "Yes" << endl;
}
else if (ac == c && bc != c) {
cout << "Yes" << endl;
}
else if (bc == c && ac != c) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
}
return 0;
}
```
修改包括:
1. 在类的构造函数中分配动态内存;
2. 修正建立邻接矩阵和 BFS 建树的代码逻辑;
3. 在 if 语句中增加了一对括号;
4. 在 findFather 函数中修改了变量的赋值语句。