请尝试查找代码,因为我无法找到正确的结果 [英] Please try to find the code since I couldnt find the proper result

查看:56
本文介绍了请尝试查找代码,因为我无法找到正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图形是抽象的顶点集合,由

非负整数和边集合标记。如果我们只讨论

顶点i和j之间的边缘而不是它的方向,则称为

无向图的图形。



例如,以下是图表:





In这个问题,您将获得无向图中的边。

边是一对非负整数(i,j),它表示vetex我通过边缘连接到vetex j





之后,您将获得一个顶点数n。您必须输出

由边缘连接的顶点列表,按边缘输入的

顺序。



输入



您获得以下内容:



1.第一行包含一个整数,E,介于1和1000之间



2.接下来是E行,其中每行包含一对

数字i和j,其中i和j都是非负整数< =

34,000。没有边缘会被列出多次。



3.最后一行包含一个非负整数n <= 34,000。 n是

保证是第(2)部分E行中列出的顶点。



输出



你必须输出n有边的节点列表,在输入边的

顺序中,每个顶点一行。



我尝试了什么:



A graph is abstractly a collection of vertices which are labelled by
non-negative integers, and a collection of edges. A graph called an
undirected graph if we talk of merely the presence of an edge between
vertices i and j, rather than its direction.

For example, the following is a graph:


In this problem, you are given the edges in an undirected graph. An
edge is a pair of non-negative integers (i, j) which indicates that
the vetex i is connected to vetex j by an edge.

Afterwards, you will be given a vertex number n. You have to output
the list of vertices which are connected n by an edge, in the order in
which the edges were input.

Input

You are given the following.

1. The first line contains an integer, E, between 1 and 1000

2. This is followed by E lines, where each containing a pair of
numbers i and j where i and j are both non-negative integers <=
34,000. No edge will be listed more than once.

3. The last line contains a non-negative integer n <= 34,000. n is
assured to be a vertex listed in one of the E lines in part (2).

Output

You have to output the list of nodes to which n has an edge, in the
order in which the edges were input, one line for each vertex.

What I have tried:

#include <stdio.h> 
#include <stdlib.h> 
   
struct AdjListNode 
{ 
    int dest; 
    struct AdjListNode* next; 
}; 
  
struct AdjList 
{ 
    struct AdjListNode *head;  
}; 
   
struct Graph 
{ 
    int V; 
    struct AdjList* array; 
}; 
  
struct AdjListNode* newAdjListNode(int dest) 
{ 
    struct AdjListNode* newNode = 
     (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); 
    newNode->dest = dest; 
    newNode->next = NULL; 
    return newNode; 
} 
  
// A utility function that creates a graph of V vertices 
struct Graph* createGraph(int V) 
{ 
    struct Graph* graph =  
        (struct Graph*) malloc(sizeof(struct Graph)); 
    graph->V = V; 
  
    graph->array =  
      (struct AdjList*) malloc(V * sizeof(struct AdjList)); 
  
    int i; 
    for (i = 0; i < V; ++i) 
        graph->array[i].head = NULL; 
  
    return graph; 
} 
  
void addEdge(struct Graph* graph, int src, int dest) 
{ 
   
    struct AdjListNode* newNode = newAdjListNode(dest); 
    newNode->next = graph->array[src].head; 
    graph->array[src].head = newNode; 
  
    newNode = newAdjListNode(src); 
    newNode->next = graph->array[dest].head; 
    graph->array[dest].head = newNode; 
} 
  
void printGraph(struct Graph* graph) 
{ 
    int v; 
    for (v = 0; v < graph->V; ++v) 
    { 
        struct AdjListNode* pCrawl = graph->array[v].head; 
        printf("\n Adjacency list of vertex %d\n head ", v); 
        while (pCrawl) 
        { 
            printf("-> %d", pCrawl->dest); 
            pCrawl = pCrawl->next; 
        } 
        printf("\n"); 
    } 
} 
   
int main() 
{ 
   
    int V = 5; 
    struct Graph* graph = createGraph(V); 
    addEdge(graph, 0, 1); 
    addEdge(graph, 0, 4); 
    addEdge(graph, 1, 2); 
    addEdge(graph, 1, 3); 
    addEdge(graph, 1, 4); 
    addEdge(graph, 2, 3); 
    addEdge(graph, 3, 4); 
  
    return 0; 
}

推荐答案

由于你没有问过问题,我没有答案。但是,我确实有代码推荐。您的三个数据结构序列是多余的。一个人可以做到。主要是因为AdjList结构完全没用。拥有一个成员的结构有什么意义?该成员的实例是相同且更明显的。另外两个有一个整数和一个指向同一类型项的指针。既然他们是同一个为什么有两个呢?这只剩下一个看起来像这样的结构:
Since you haven't asked a question, I don't have an answer. However, I do have a code recommendation. Your sequence of three data structures are redundant. One could do job. Mostly because the AdjList structure is completely useless. What is the point of having a structure with one member? An instance of that member is the same and more obvious. The other two have an integer and a pointer to an item of the same type. Since they are the same why have two of them? That leaves just one structure that could look something like this:
struct ListNode 
{ 
    int integer; 
    struct ListNode * pnext; 
};

整数在一个中称为dest,在另一个中称为V.当它们基本相同时,实际上不需要有两个结构和两组代码来访问它们。在C ++中,您可以使其成为模板类或结构,并包含您想要的任何类型的数据。

Integer is called dest in one and V in the other. There is really no need to have two structures and two sets of code to access them when they are essentially the same. In C++ you could make that a template class or structure and contain any type of data you want.


这篇关于请尝试查找代码,因为我无法找到正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆