Skip to content

Commit

Permalink
Upload Chapter11
Browse files Browse the repository at this point in the history
  • Loading branch information
BenedictYoung authored Apr 21, 2020
1 parent 9c06601 commit 4ccaa98
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
24 changes: 12 additions & 12 deletions Chapter11/11.1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ using namespace std;

const int MAXN = 1000 + 10;

int father[MAXN]; //父亲结点
int height[MAXN]; //结点高度
int father[MAXN]; //父亲结点
int height[MAXN]; //结点高度

void Initial(int n) { //初始化
void Initial(int n) { //初始化
for (int i = 0; i <= n; i++) {
father[i] = i; //每个结点的父亲为自己
height[i] = 0; //每个结点的高度为零
father[i] = i; //每个结点的父亲为自己
height[i] = 0; //每个结点的高度为零
}
}

int Find(int x) { //查找根结点
if (x != father[x]) { //路径压缩
int Find(int x) { //查找根结点
if (x != father[x]) { //路径压缩
father[x] = Find(father[x]);
}
return father[x];
}

void Union(int x, int y) { //合并集合
void Union(int x, int y) { //合并集合
x = Find(x);
y = Find(y);
if (x != y) { //矮树作为高树的子树
if (x != y) { //矮树作为高树的子树
if (height[x] < height[y]) {
father[x] = y;
} else if (height[y] < height[x]) {
Expand All @@ -52,16 +52,16 @@ int main() {
break;
}
scanf("%d", &m);
Initial(n); //初始化
Initial(n); //初始化
while (m--) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
Union(x, y); //合并集合
Union(x, y); //合并集合
}
int answer = -1;
for (int i = 1; i <= n; i++) {
if (Find(i) == i) { //集合数目
if (Find(i) == i) { //集合数目
answer++;
}
}
Expand Down
14 changes: 7 additions & 7 deletions Chapter11/11.10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ const int MAXN = 1000 + 10;
const int INF = INT_MAX;

struct Edge {
int to; //终点
int length; //长度
int to; //终点
int length; //长度
Edge(int t, int l): to(t), length(l) {}
};

vector<Edge> graph[MAXN];
int earliest[MAXN]; //最早完成时间
int latest[MAXN]; //最晚完成时间
int inDegree[MAXN]; //入度
int earliest[MAXN]; //最早完成时间
int latest[MAXN]; //最晚完成时间
int inDegree[MAXN]; //入度

int CriticalPath(int n) {
vector<int> topology; //拓扑序列
vector<int> topology; //拓扑序列
queue<int> node;
for (int i = 0; i < n; ++i) {
if (inDegree[i] == 0) {
node.push(i);
earliest[i] = 1; //初始化为1
earliest[i] = 1; //初始化为1
}
}
int totalTime = 0; //总耗时
Expand Down
26 changes: 13 additions & 13 deletions Chapter11/11.2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ using namespace std;

const int MAXN = 1000 + 10;

int father[MAXN]; //父亲结点
int height[MAXN]; //结点高度
int father[MAXN]; //父亲结点
int height[MAXN]; //结点高度

void Initial(int n) { //初始化
void Initial(int n) { //初始化
for (int i = 0; i <= n; i++) {
father[i] = i; //每个结点的父亲为自己
height[i] = 0; //每个结点的高度为零
father[i] = i; //每个结点的父亲为自己
height[i] = 0; //每个结点的高度为零
}
}

int Find(int x) { //查找根结点
if (x != father[x]) { //路径压缩
int Find(int x) { //查找根结点
if (x != father[x]) { //路径压缩
father[x] = Find(father[x]);
}
return father[x];
}

void Union(int x, int y) { //合并集合
void Union(int x, int y) { //合并集合
x = Find(x);
y = Find(y);
if (x != y) { //矮树作为高树的子树
if (x != y) { //矮树作为高树的子树
if (height[x] < height[y]) {
father[x] = y;
} else if (height[y] < height[x]) {
Expand All @@ -51,16 +51,16 @@ int main() {
if (n == 0 && m == 0) {
break;
}
Initial(n); //初始化
Initial(n); //初始化
while (m--) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
Union(x, y); //合并集合
Union(x, y); //合并集合
}
int component = 0; //连通分量
int component = 0; //连通分量
for (int i = 1; i <= n; i++) {
if (Find(i) == i) { //集合数目
if (Find(i) == i) { //集合数目
component++;
}
}
Expand Down
24 changes: 12 additions & 12 deletions Chapter11/11.3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ using namespace std;

const int MAXN = 1e4 + 10;

int father[MAXN]; //父亲结点
int height[MAXN]; //结点高度
int inDegree[MAXN]; //入度
bool visit[MAXN]; //标记
int father[MAXN]; //父亲结点
int height[MAXN]; //结点高度
int inDegree[MAXN]; //入度
bool visit[MAXN]; //标记

void Initial() { //初始化
void Initial() { //初始化
for (int i = 0; i < MAXN; i++) {
father[i] = i;
height[i] = 0;
Expand All @@ -26,14 +26,14 @@ void Initial() { //初始化
}
}

int Find(int x) { //查找根结点
int Find(int x) { //查找根结点
if (x != father[x]) {
father[x] = Find(father[x]);
}
return father[x];
}

void Union(int x, int y) { //合并集合
void Union(int x, int y) { //合并集合
x = Find(x);
y = Find(y);
if (x != y) {
Expand All @@ -51,8 +51,8 @@ void Union(int x, int y) { //合并集合

bool IsTree() {
bool flag = true;
int component = 0; //连通分量数目
int root = 0; //根结点数目
int component = 0; //连通分量数目
int root = 0; //根结点数目
for (int i = 0; i < MAXN; ++i) {
if (!visit[i]) {
continue;
Expand All @@ -62,14 +62,14 @@ bool IsTree() {
}
if (inDegree[i] == 0) {
root++;
} else if (inDegree[i] > 1) { //入度不满足要求
} else if (inDegree[i] > 1) { //入度不满足要求
flag = false;
}
}
if (component != 1 || root != 1) { //不符合树定义
if (component != 1 || root != 1) { //不符合树定义
flag = false;
}
if(component == 0 && root == 0) { //空集也是树
if(component == 0 && root == 0) { //空集也是树
flag = true;
}
return flag;
Expand Down

0 comments on commit 4ccaa98

Please sign in to comment.