{"id":60,"date":"2013-12-14T14:37:20","date_gmt":"2013-12-14T14:37:20","guid":{"rendered":""},"modified":"2013-12-14T14:37:20","modified_gmt":"2013-12-14T14:37:20","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=60","title":{"rendered":"Graph"},"content":{"rendered":"<pre class=\"brush:cpp; toolbar: true; auto-links: true;\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;conio.h&gt;\r\n#include &lt;windows.h&gt;\r\n\r\n#define MAX 7\r\n\r\nconst char *name[]= {\"\u5927\u6210\u6865\",\"\u5e7f\u573a\",\"\u56fe\u4e66\u9986\",\"\u4eba\u5de5\u6e56\",\"\u9910\u5385\",\"\u7bee\u7403\u573a\",\"\u8db3\u7403\u573a\"};\r\nconst char *describe[]= {\"\u4e00\u5ea7\u62f1\u6865\",\"\u9f50\u9c81\u5e7f\u573a\",\"\u5f88\u6f02\u4eae\",\"\u6ca1\u4fee\u597d\",\"\u7b2c\u4e09\u98df\u5802\",\"\u8fd8\u662f\u6c34\u6ce5\u5730\",\"\u4e00\u5708400\u7c73\"};\r\n\r\ntypedef struct\r\n{\r\n    int num;\r\n    char name[20];\r\n    char describe[30];\r\n} GNode;\r\n\r\ntypedef struct Node_Index\r\n{\r\n    int index;\r\n    struct Node_Index *next;\r\n} Index;\r\n\r\ntypedef struct\r\n{\r\n    GNode GraphNode;\r\n    Index *next;\r\n} MGraph;\r\n\r\n\/*-------------------------------Queue----------------------------------*\/\r\n#define DATATYPE MGraph\r\n\r\ntypedef struct Queue_Node\r\n{\r\n    DATATYPE data;\r\n    struct Queue_Node *next;\r\n} QNode;\r\n\r\ntypedef struct\r\n{\r\n    QNode *head;\r\n    QNode *end;\r\n    int queuesize;\r\n} Queue;\r\n\r\n\r\nBOOL EnQueue(Queue **Q,DATATYPE Elem)\r\n{\r\n    if(!*Q)\r\n    {\r\n        if((*Q=(Queue *)malloc(sizeof(Queue)))==NULL)\r\n            return FALSE;\r\n        memset(*Q,NULL,sizeof(Queue));\r\n    }\r\n\r\n    if(!(*Q)-&gt;head)\r\n    {\r\n        \/\/\u961f\u5217\u5934\u7ed3\u70b9\u4e3a\u7a7a\r\n        (*Q)-&gt;head=(*Q)-&gt;end=(QNode *)malloc(sizeof(QNode));\r\n        if((*Q)-&gt;head==NULL) return FALSE;\r\n        memset((*Q)-&gt;head,NULL,sizeof(QNode));\r\n        (*Q)-&gt;head-&gt;data=Elem;\r\n        (*Q)-&gt;queuesize++;\r\n        return TRUE;\r\n    }\r\n    (*Q)-&gt;end-&gt;next=(QNode *)malloc(sizeof(QNode));\r\n    if((*Q)-&gt;end-&gt;next==NULL) return FALSE;\r\n    memset((*Q)-&gt;end-&gt;next,NULL,sizeof(QNode));\r\n    (*Q)-&gt;end=(*Q)-&gt;end-&gt;next;\r\n    (*Q)-&gt;end-&gt;data=Elem;\r\n    (*Q)-&gt;queuesize++;\r\n\r\n    return TRUE;\r\n}\r\n\r\nBOOL DeQueue(Queue *Q,DATATYPE *Elem)\r\n{\r\n    if(!Q || !Q-&gt;head) return FALSE;\r\n    QNode *temp=Q-&gt;head;\r\n\r\n    Q-&gt;queuesize--;   \/\/\u961f\u5217\u957f\u5ea6\u51cf\u4e00\r\n    *Elem=Q-&gt;head-&gt;data;\r\n    Q-&gt;head=Q-&gt;head-&gt;next;\r\n    free(temp);\r\n\r\n    return TRUE;\r\n}\r\n\r\nBOOL QueueEmpty(Queue *Q)\r\n{\r\n    return Q-&gt;head==NULL?TRUE:FALSE;\r\n}\r\n\r\n\r\n\/\/-------------------------------------------------------------------------\r\n\r\nvoid importInfo(MGraph *graph)\r\n{\r\n    int i;\r\n\r\n    for(i=0; i&lt;MAX; i++)\r\n    {\r\n        memcpy(graph[i].GraphNode.name,name[i],strlen(name[i]));\r\n        memcpy(graph[i].GraphNode.describe,describe[i],strlen(describe[i]));\r\n        graph[i].GraphNode.num=i;\r\n    }\r\n\r\n    return;\r\n}\r\n\r\nint linkNode(MGraph *graph,int x,int y)\r\n{\r\n    MGraph *G=NULL;\r\n    Index *index=NULL,*temp=NULL;\r\n\r\n    if((index=(Index *)malloc(sizeof(Index)))==NULL)\r\n        return 0;\r\n    memset(index,NULL,sizeof(Index));\r\n    G=graph+x;\r\n    if(G-&gt;next==NULL)\r\n    {\r\n        G-&gt;next=index;\r\n    }\r\n    else\r\n    {\r\n        temp=G-&gt;next;\r\n        while(temp-&gt;next) temp=temp-&gt;next;\r\n        temp-&gt;next=index;\r\n    }\r\n    index-&gt;index=y;\r\n    \/\/===========================================\r\n    if((index=(Index *)malloc(sizeof(Index)))==NULL)\r\n        return 0;\r\n    memset(index,NULL,sizeof(Index));\r\n    G=graph+y;\r\n    if(G-&gt;next==NULL)\r\n    {\r\n        G-&gt;next=index;\r\n    }\r\n    else\r\n    {\r\n        temp=G-&gt;next;\r\n        while(temp-&gt;next) temp=temp-&gt;next;\r\n        temp-&gt;next=index;\r\n    }\r\n    index-&gt;index=x;\r\n\r\n    return 1;\r\n}\r\n\r\nint CreateGraph(MGraph **graph)\r\n{\r\n    *graph=(MGraph *)malloc(sizeof(MGraph)*MAX);\r\n    if(*graph==NULL) return 0;\r\n    memset(*graph,NULL,sizeof(MGraph)*MAX);\r\n    importInfo(*graph);\r\n\r\n    return 1;\r\n}\r\n\r\nvoid visit(MGraph *graph)\r\n{\r\n    printf(\"\u8282\u70b9%d:\u666f\u70b9:%s\\t\\t\u63cf\u8ff0:%s\\n\",graph-&gt;GraphNode.num,graph-&gt;GraphNode.name,graph-&gt;GraphNode.describe);\r\n    return;\r\n}\r\n\r\nint Traversal[MAX];\r\nMGraph *root=NULL;\r\n\r\nint DFS(MGraph *graph)\r\n{\r\n    \/\/\u6df1\u5ea6\u4f18\u5148\u904d\u5386\r\n    int i;\r\n    Index *index=NULL;\r\n\r\n    for(i=0; i&lt;MAX; i++)\r\n        if(graph-&gt;GraphNode.num==Traversal[i])\r\n            return 0;  \/\/\u68c0\u67e5\u8282\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee\u8fc7\r\n    visit(graph);\r\n    for(i=0; i&lt;MAX; i++)\r\n        if(Traversal[i]==-1)\r\n        {\r\n            Traversal[i]=graph-&gt;GraphNode.num;\r\n            break;\r\n        }\r\n    index=graph-&gt;next;\r\n    while(index)\r\n    {\r\n        DFS(&amp;root[index-&gt;index]);\r\n        index=index-&gt;next;\r\n    }\r\n\r\n\r\n    return 1;\r\n}\r\n\r\nint BFS(MGraph *graph)\r\n{\r\n    \/\/\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\r\n    Queue *Q=NULL;\r\n    int i;\r\n    MGraph temp,*ptemp=NULL;\r\n    Index *index=NULL;\r\n\r\n    visit(graph);\r\n    index=graph-&gt;next;\r\n\r\n    while(index)\r\n    {\r\n        EnQueue(&amp;Q,root[index-&gt;index]);\r\n        index=index-&gt;next;\r\n    }\r\n    Traversal[0]=0;\r\n\r\n    while(!QueueEmpty(Q))\r\n    {\r\n        memset(&amp;temp,NULL,sizeof(MGraph));\r\n        DeQueue(Q,&amp;temp);\r\n        index=temp.next;\r\n        while(index)\r\n        {\r\n            for(i=0; i&lt;MAX; i++)\r\n                if(index-&gt;index==Traversal[i])\r\n                    goto skip;\r\n\r\n            EnQueue(&amp;Q,root[index-&gt;index]);\r\nskip:\r\n            index=index-&gt;next;\r\n        }\r\n        visit(&amp;temp);\r\n\r\n        for(i=0; i&lt;MAX; i++)\r\n            if(Traversal[i]==-1)\r\n            {\r\n                Traversal[i]=temp.GraphNode.num;\r\n                break;\r\n            }\r\n    }\r\n\r\n    return 1;\r\n}\r\n\r\nint main()\r\n{\r\n    int i,x,y;\r\n    MGraph *graph=NULL;\r\n\r\n    CreateGraph(&amp;graph);\r\n    root=graph;\r\n    printf(\"\u8bf7\u8f93\u5165\u8981\u8fde\u63a5\u7684\u8282\u70b9:(\u683c\u5f0f:X-Y)\\n\");\r\n    while(1)\r\n    {\r\n        if(scanf(\"%d-%d\",&amp;x,&amp;y)!=2) break;\r\n        linkNode(graph,x,y);\r\n    }\r\n    int j,k;\r\n    Index *index=NULL;\r\n    for(i=0; i&lt;MAX; i++)\r\n    {\r\n        printf(\"%d \",graph[i].GraphNode.num);\r\n        index=graph[i].next;\r\n        while(index)\r\n        {\r\n            printf(\"-&gt;%d\",index-&gt;index);\r\n            index=index-&gt;next;\r\n        }\r\n        puts(\"\");\r\n    }\r\n    printf(\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386:\\n\");\r\n    for(i=0; i&lt;MAX; i++)\r\n        Traversal[i]=-1;\r\n    DFS(graph);\r\n    printf(\"\\n\u5e7f\u5ea6\u4f18\u5148\u904d\u5386:\\n\");\r\n    for(i=0; i&lt;MAX; i++)\r\n        Traversal[i]=-1;\r\n    BFS(graph);\r\n\r\n    return 0;\r\n}<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\n\t#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;conio.h&gt;<br \/>\n#include &lt;windows.h&gt;\n<\/p>\n<p>\n\t#define MAX 7\n<\/p>\n<p>\n\tconst char *name[]= {&#8220;\u5927\u6210\u6865&#8221;,&#8221;\u5e7f\u573a&#8221;,&#8221;\u56fe\u4e66\u9986&#8221;,&#8221;\u4eba\u5de5\u6e56&#8221;,&#8221;\u9910&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[326],"tags":[],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Graph - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=60\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Graph - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;conio.h&gt; #include &lt;windows.h&gt;    #define MAX 7    const char *name[]= {&quot;\u5927\u6210\u6865&quot;,&quot;\u5e7f\u573a&quot;,&quot;\u56fe\u4e66\u9986&quot;,&quot;\u4eba\u5de5\u6e56&quot;,&quot;\u9910...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=60\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2013-12-14T14:37:20+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=60#webpage\",\"url\":\"http:\/\/weizn.net\/?p=60\",\"name\":\"Graph - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"datePublished\":\"2013-12-14T14:37:20+00:00\",\"dateModified\":\"2013-12-14T14:37:20+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=60#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=60\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=60#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Graph\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=60#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=60#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"Graph\",\"datePublished\":\"2013-12-14T14:37:20+00:00\",\"dateModified\":\"2013-12-14T14:37:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=60#webpage\"},\"wordCount\":1,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"articleSection\":[\"\\u6570\\u636e\\u7ed3\\u6784\\u53ca\\u7b97\\u6cd5\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=60#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Graph - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=60","og_locale":"zh_CN","og_type":"article","og_title":"Graph - Wayne&#039;s Blog","og_description":"#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;conio.h&gt; #include &lt;windows.h&gt;    #define MAX 7    const char *name[]= {\"\u5927\u6210\u6865\",\"\u5e7f\u573a\",\"\u56fe\u4e66\u9986\",\"\u4eba\u5de5\u6e56\",\"\u9910...","og_url":"http:\/\/weizn.net\/?p=60","og_site_name":"Wayne&#039;s Blog","article_published_time":"2013-12-14T14:37:20+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=60#webpage","url":"http:\/\/weizn.net\/?p=60","name":"Graph - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"datePublished":"2013-12-14T14:37:20+00:00","dateModified":"2013-12-14T14:37:20+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=60#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=60"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=60#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"Graph"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=60#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=60#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"Graph","datePublished":"2013-12-14T14:37:20+00:00","dateModified":"2013-12-14T14:37:20+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=60#webpage"},"wordCount":1,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"articleSection":["\u6570\u636e\u7ed3\u6784\u53ca\u7b97\u6cd5"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=60#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=60"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}