• 图的存储


    描述

    本题目考察大家图的存储的基本能力(建议大家将两种图的储存方式都试一下),题目将给大家n个点的信息(字符串,长度<=30),再给大家m条边的信息,最后提出k个问题,每个问题都是寻找每个点的邻接点。

    输入

    第一行:n,m,k
    接下来n行,每行一个字符串,表示每个顶点的信息(字符串长度<=30)
    接下来m行,每行两个数字a b,(0<=a,b <n 且a!=b)表示存在一条第a个点指向第b个点的边;
    接下来k行,每行一个数字z,表示询问第z顶点所有的邻接点信息;

    输出

    一共k行,分别对应k个问题的答案:
    如果询问的点,没有邻接点,那么输出0;
    如果询问的点有多个邻接点,那么按照每个点(注意不是边)的输入顺序,进行输出,每个点的信息用空格隔开。

    样例输入

    5 7 5
    A
    B
    C
    E
    F
    0 1
    1 2
    2 4
    0 3
    4 0
    4 1
    3 2
    0
    2
    3
    4
    1
    

    样例输出

    B E 
    F 
    C 
    A B 
    C 
    

    提示

    20% n<=10 m<=20 k<=n;
    50% n<=500 m<=1000 k<=n;
    70% n<=3000 m<=6000 k<=n;
    100% n<=10000 m<=20000 k<=n;

    A部分代码

    只要根据题意做就ok了,注意提示内容:

    那么按照每个点(注意不是边)的输入顺序,进行输出
    详细参考:CSDN问答----一道关于图的存储的题,为何我的代码一直报错Output Limit Exceeded?] 注意,问答中的代码存在问题,参考使用

    B部分代码

    用邻接表存储,注意提示内容!!!

    完整代码_A

    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    #define MAX_SIZE 10010
    #define MAX_LEN 31
    
    using namespace std;
    
    char vexs[MAX_SIZE][MAX_LEN];
    vector <int> nextvexs[MAX_SIZE];
    
    int main(int argc, char const *argv[])
    {
        int n, m, k, a, b, i, j, count = 0;
    	cin >> n >> m >> k;
    	cin.ignore();
    	for (i = 0; i < n; i++) gets(vexs[i]);
    	while (m--) {
    		cin >> a >> b;
    		nextvexs[a].push_back(b);
    	}
    	while (k--) {
    		cin >> a;
    		if (nextvexs[a].size() == 0) cout << 0;
    		else {
    			sort(nextvexs[a].begin(),nextvexs[a].end());
    			for(vector<int>::iterator it=nextvexs[a].begin(); it!=nextvexs[a].end(); it++){
    				cout<< vexs[*it]<<" ";
    			}
    		}
       		 cout<<endl;
    	}
        system("pause");
        return 0;
    }
    

    完整代码_B

    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    #include <algorithm>
    #define MAX_SIZE 10010
    #define MAX_LEN 31
    
    using namespace std;
    
    typedef struct ANode
    {
       int adjvex;
       struct ANode *nextarc;
    }ArcNode;
    typedef struct Vnode
    {
        char info[MAX_LEN];
        ArcNode *firstarc;
    }VNode;
    typedef struct
    {
        VNode adjlist[MAX_SIZE];
        int n, e;
    }AdjGraph;
    bool com (ArcNode *a, ArcNode *b)
    {
        return a->adjvex < b->adjvex;
    }
    int main(int argc, char const *argv[])
    {
        int n, m, k, a, b, i, j;
        AdjGraph *adjgraph;
        adjgraph = (AdjGraph*) malloc (sizeof(AdjGraph));
        cin >> n >> m >> k;
        cin.ignore();
        adjgraph->n = n;
        adjgraph->e = m;
        for (i = 0; i < n; i++) {
            gets(adjgraph->adjlist[i].info);
            adjgraph->adjlist[i].firstarc = NULL;
        }
        while (m--) {
            cin >> a >> b;
            ArcNode *p;
            p = (ArcNode*) malloc (sizeof(ArcNode));
            p->adjvex = b;
            p->nextarc = adjgraph->adjlist[a].firstarc;
            adjgraph->adjlist[a].firstarc = p;
        }
        while (k--) {
            cin >> a;
            int count = 0;
            ArcNode *nex_[MAX_SIZE];
            ArcNode *nex = adjgraph->adjlist[a].firstarc;
            while (NULL != nex) {
                 nex_[count++] = nex;
                 nex = nex->nextarc;
            }
            if (count == 0) cout << 0;
            else {
                sort(nex_,nex_+count,com);
                for (i = 0; i < count; i++) cout << adjgraph->adjlist[nex_[i]->adjvex].info << " ";
            }
            cout << endl;
        }
        system("pause");
        return 0;
    }
    
  • 相关阅读:
    数据库应用-java+sqlserver(十)StuInfo
    数据库应用-java+sqlserver(九)StudentMain和StudentManager
    数据库应用-java+sqlserver(八)StoreScore
    数据库应用-java+sqlserver(七)SqlManager
    小学期学习总结二
    小学期学习总结一
    四则运算小程序
    第二小组第七周学习心得
    需求分析
    MSF
  • 原文地址:https://www.cnblogs.com/levarz/p/12925330.html
Copyright © 2020-2023  润新知