zoukankan      html  css  js  c++  java
  • 邻接表示例程序

    输入:n,m代表顶点数目和边数。然后m行,代表每个边的起点和终点。0 0 表示结束。

    输出:第一行为n个正整数,表示每个点的出度;第二行为n个正整数,表示每个点的入度。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cctype>
     6 #include <stack>
     7 #include <queue>
     8 #include <map>
     9 #include <set>
    10 #include <vector>
    11 #include <cmath>
    12 #include <algorithm>
    13 #define lson l, m, rt<<1
    14 #define rson m+1, r, rt<<1|1
    15 using namespace std;
    16 typedef long long int LL;
    17 const int MAXN =  0x3f3f3f3f;
    18 const int  MIN =  -0x3f3f3f3f;
    19 const double eps = 1e-9;
    20 const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1},
    21   {1,1},{1,-1},{-1,-1}};
    22 const int MAX = 100;
    23 struct ArcNode{  // 边节点
    24   int adjvex; ArcNode *nextarc;
    25 };
    26 struct VNode{  // 顶点
    27   int data;
    28   ArcNode *head1; // 出边表表头指针
    29   ArcNode *head2;  // 入边表表头指针
    30 };
    31 struct LGraph{ // 临界表结构体
    32   VNode vertexs[MAX];  // 顶点数组
    33   int vexnum, arcnum;
    34 };
    35 LGraph lg;
    36 void CreateLG()
    37 {
    38   int i = 0; ArcNode *pi; int v1, v2;
    39   //scanf("%d%d", &lg.vexnum, &lg.arcnum);
    40   for (i = 0; i < lg.vexnum; ++i) lg.vertexs[i].head1 = lg.vertexs[i].head2 = NULL;
    41   for (i = 0; i < lg.arcnum; ++i){
    42     scanf("%d%d", &v1, &v2); v1--; v2--;
    43     pi = new ArcNode; pi->adjvex = v2; pi->nextarc = lg.vertexs[v1].head1;
    44     lg.vertexs[v1].head1 = pi;
    45     pi = new ArcNode; pi->adjvex = v1; pi->nextarc = lg.vertexs[v2].head2;
    46     lg.vertexs[v2].head2 = pi;
    47   }
    48 }
    49 void DeleteLG()
    50 {
    51   ArcNode *pi; int i;
    52   for (i = 0; i < lg.vexnum; ++i){
    53     pi = lg.vertexs[i].head1;
    54     while (pi){
    55       lg.vertexs[i].head1 = pi->nextarc;
    56       delete pi;
    57       pi = lg.vertexs[i].head1;
    58     }
    59     pi = lg.vertexs[i].head2;
    60     while (pi){
    61       lg.vertexs[i].head2 = pi->nextarc;
    62       delete pi;
    63       pi = lg.vertexs[i].head2;
    64     }
    65   }
    66 }
    67 
    68 int main(void){
    69 #ifndef ONLINE_JUDGE
    70   freopen("neighlist.in", "r", stdin);
    71 #endif
    72   int i, id, od; ArcNode *pi;
    73   while (1){
    74     int n, m; scanf("%d%d", &n, &m);
    75     if (n+m == 0) break;
    76     lg.vexnum = n; lg.arcnum = m; CreateLG();
    77     for (i = 0; i < lg.vexnum; ++i){
    78       od = 0;
    79       pi = lg.vertexs[i].head1;
    80       while (pi){
    81         od++; pi = pi->nextarc;
    82       }
    83       if (i == 0) printf("%d", od);
    84       else printf(" %d", od);
    85     }
    86     printf("\n");
    87     for (i = 0; i < lg.vexnum; ++i){
    88       id = 0; pi = lg.vertexs[i].head2;
    89       while (pi){
    90         id++; pi = pi->nextarc;
    91       }
    92       if (i == 0) printf("%d", id);
    93       else printf(" %d", id);
    94     }
    95     printf("\n");
    96   }
    97 
    98   return 0;
    99 }

      也不觉得难写了……

  • 相关阅读:
    c#-全局键盘钩子
    C#-自动获取IP
    C#-自动获取IP
    C#-获取CPUID
    C#-获取CPUID
    手动添加导入表修改EXE功能
    安装全局消息钩子实现dll窗体程序注入
    手动添加导入表修改EXE功能
    虚拟桌面模拟查找点击自绘控件
    虚拟桌面模拟查找点击自绘控件
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/3048165.html
Copyright © 2011-2022 走看看