Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
gbhb committed Jun 2, 2021
0 parents commit eafb8e6
Show file tree
Hide file tree
Showing 50 changed files with 1,667 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"configurations": [
{
"name": "MinGW",
"intelliSenseMode": "gcc-x64",
"compilerPath": "C:/Program Files/LLVM/bin/gcc.exe",
"includePath": [
"${workspaceFolder}"
],
"defines": [],
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
30 changes: 30 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": "0.2.0",
"configurations": [



{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "neverOpen",
"MIMode": "gdb",
"miDebuggerPath": "gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Compile"
}
]
}
31 changes: 31 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"files.defaultLanguage": "cpp",
"editor.formatOnType": true,
"editor.snippetSuggestions": "top",
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc --target=x86_64-w64-mingw -std=c11 && $dir$fileNameWithoutExt",
"cpp": "cd $dir && gcc++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc --target=x86_64-w64-mingw -std=c++17 && $dir$fileNameWithoutExt"
},
"code-runner.saveFileBeforeRun": true,
"code-runner.preserveFocus": true,
"code-runner.clearPreviousOutput": false,
"C_Cpp.clang_format_sortIncludes": true,
"C_Cpp.intelliSenseEngine": "Default",
"C_Cpp.errorSquiggles": "Disabled",
"C_Cpp.autocomplete": "Disabled",
"clang.cflags": [
"--target=x86_64-w64-mingw",
"-std=c11",
"-Wall"
],
"clang.cxxflags": [
"--target=x86_64-w64-mingw",
"-std=c++17",
"-Wall"
],
"clang.completion.enable": true,
"files.associations": {
"stdlib.h": "c"
}
}
32 changes: 32 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"command": "clang++",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-g",
"-Wall",
"-static-libgcc",
"-fcolor-diagnostics",
"--target=x86_64-w64-mingw",
"-std=c++17"
],
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
// "problemMatcher":"$gcc"
}
]
}
Binary file added 107502558.docx
Binary file not shown.
Binary file added 107502558.pdf
Binary file not shown.
Binary file added 107502558.zip
Binary file not shown.
105 changes: 105 additions & 0 deletions 107502558_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include<stdio.h>
#include<stdlib.h>
int IsTree(int nPoint);
int IsInSet(int*n,int nPoint,int x); /* Return member ID in array */
int g[10000][10000];
void Trace(int nPoint,int root);
int visit = 0; /*Tree Traversal*/
int main(){
int a,b;/* input */
int numberSet[10000],nPoint;/*numberSet and Set size */
int indexA,indexB; /*member ID of numberSet*/
int icase,i,j,multiEdge;

for(icase =1; ;icase++){
multiEdge = 0;/*Prevent From muti-edge*/
scanf("%d%d",&a,&b);
if(a==-1&&b==-1)return 0;
if(!a&&!b){
printf("Case %d is a tree.\n",icase);
continue;
}

/* Initialize */
for(i=0;i<10000;i++)
for(j=0;j<10000;j++)
g[i][j]=0;

/* EnSet(a,b) */
if(a==b){
numberSet[0]=a;
nPoint = 1;
g[0][0] = 1;
}
else{
numberSet[0]=a;
numberSet[1]=b;
nPoint = 2;
g[1][0] = 1;
}
while(1){
scanf("%d%d",&a,&b);
if(!a&&!b)break;/*End Of Case */

indexA = IsInSet(numberSet,nPoint,a);
if(indexA==-1){/* EnSet(a,b) */
numberSet[nPoint++]= a;
indexA = nPoint-1;
}

indexB = IsInSet(numberSet,nPoint,b);
if(indexB==-1){/* EnSet(a,b) */
numberSet[nPoint++]= b;
indexB = nPoint-1 ;
}


if(g[indexB][indexA]==0)
g[indexB][indexA] = 1;
else {multiEdge = 1;}
}

if(multiEdge)printf("Case %d is not a tree.\n",icase);
else if(IsTree(nPoint))printf("Case %d is a tree.\n",icase);
else printf("Case %d is not a tree.\n",icase);

}
return 0;
}
/* g[a][b] is a <- b and a b is sequential number of array */
int IsInSet(int*numberSet,int nPoint,int x){
int i;
for(i=0;i<nPoint;i++)
if(numberSet[i]==x)return i;
return -1;
}
int IsTree(int nPoint){
int i,j;
int checkSum,nRoot,root;
for(i=0,nRoot=0;i<nPoint;i++){
if(g[i][i]==1)return 0;/* connect self */
for(j=0,checkSum=0;j<nPoint;j++){
checkSum+=g[i][j]; /* count how many b connect a */
if(checkSum>1) {return 0;}
}

if(!checkSum) {nRoot++;root = i; }/* Record who is Root because no one will point at root */
}
if(nRoot!=1){return 0;} /* have only one */
/* Check Cycle and path :Using DFS*/
visit = 1;/*From root */
Trace(nPoint,root);

if(visit==nPoint) return 1; /*Is visit the points same with total points */
else return 0;

}
void Trace(int nPoint,int start){
int i;
for(i=0;i<nPoint;i++)
if(g[i][start]){
visit++;
Trace(nPoint,i);
}
return ;
}
Binary file added 107502558_1.exe
Binary file not shown.
Binary file added 107502558_1.o
Binary file not shown.
117 changes: 117 additions & 0 deletions 107502558_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct edge
{
int next;
int cost;
} edge;

struct min_heap
{
edge stack[1024];
int tail;
};

int compare(const edge lhs, const edge rhs)
{
if (lhs.cost == rhs.cost)
return lhs.next - rhs.next;
return lhs.cost - rhs.cost;
}

void swap(edge *lhs, edge *rhs) //lhs = left hand side
{
edge temp = *lhs;
*lhs = *rhs;
*rhs = temp;
}

int choose_left_right(const struct min_heap *h, const unsigned int father)
{
int left = father * 2 + 1, right = left + 1;
if (right > h->tail) // if father have one or no child
return left > h->tail ? 0 : left;
return compare(h->stack[left], h->stack[right]) <= 0 ? left : right;
}

void pop(struct min_heap *h)
{
if (h->tail < 0) //tail ==-1 is empty heap
return;
edge *t = h->stack;
swap(&t[0], &t[h->tail--]); // h-> tail ; tail--
for (int next, index = 0; (next = choose_left_right(h, index)) && compare(t[next], t[index]) < 0; index = next) // rebulid min heap ,first compare left amd right child then compare father and smaller child
swap(&t[index], &t[next]);
}

edge get_min(const struct min_heap *h)
{
return (h->tail >= 0) ? h->stack[0] : (edge){0, 0}; // p
}

void push(struct min_heap *h, edge lenght)
{
if (h->tail >= 1023)
return;
edge *t = h->stack;
t[++(h->tail)] = lenght;
for (int index = h->tail; (index) && compare(t[index], t[(index - 1) / 2]) < 0; index = (index - 1) / 2)
swap(&t[index], &t[(index - 1) / 2]);
}

void filter_edges(struct min_heap *h, int edges[], int n) //edges[] = ptr of array
{
for (int i = 0; i < n; i++)
if (edges[i]) // edges[i] = vertex[ ][i]
push(h, (edge){i, edges[i]}); //i = next ,edges[i] = cost
}

int main(int argc, char *argv[])
{
while (1)
{
int n;
scanf("%d", &n);
if (n == -1)
{
break;
}
struct min_heap h = {.stack = {}, .tail = -1}; // initialize the heap { {},-1 }

int vertex[26][26] = {0}, connected[26] = {[0] = 1};

int total_cost = 0, have_connected_num = 1;

for (int i = 0, temp; i < n; i++) //read the matrix
for (int j = 0; j < n; j++)
{
scanf("%d", &temp);
vertex[i][j] = temp;
}

filter_edges(&h, vertex[0], n); //filter 0 cost and add first row

for (; h.tail >= 0; pop(&h))
{
int next_vertex = get_min(&h).next;
if (!connected[next_vertex])
{
filter_edges(&h, vertex[next_vertex], n);
total_cost += get_min(&h).cost;
connected[next_vertex] = 1;
have_connected_num++;
}
}
if (have_connected_num != n)
{
printf("NO connected\n");
}
else
{
printf("Minimum cost:%d\n", total_cost);
}
}

return 0;
}
Binary file added 107502558_2.exe
Binary file not shown.
Binary file added 107502558_2.o
Binary file not shown.
Loading

0 comments on commit eafb8e6

Please sign in to comment.