• 基于邻接表的新顶点的增加


    基于邻接表的新顶点的增加

    发布时间: 2018年11月26日 10:19   时间限制: 1000ms   内存限制: 128M

    给定一个无向图,在此无向图中增加一个新顶点。

    多组数据,每组m+2行。第一行有两个数字n和m,代表有n个顶点和m条边。顶点编号为1到n。第二行到第m+1行每行有两个数字h和k,代表边依附的两个顶点。第m+2行有一个数字f,代表新插入的顶点编号。当n和m都等于0时,输入结束

    每组数据输出n+1行。为增加顶点后的邻接表。每两个数字之间用空格隔开。

    3 2
    1 2
    2 3
    4
    2 1
    1 2
    4
    0 0
    1 2
    2 3 1
    3 2
    4
    1 2
    2 1
    4

     1 #include<iostream>
     2 using namespace std;
     3 #define maxn 100
     4 
     5 typedef struct node
     6 {
     7     int data;
     8     struct node *next;
     9 }Node;
    10 
    11 int main()
    12 {
    13     int n, m;
    14     int x, y, f;
    15     Node *p;
    16     Node *V[maxn];
    17     while (1)
    18     {
    19         cin >> n >> m;
    20         if (n == 0 && m == 0)
    21             break;
    22         for (int i = 0; i<maxn; i++)
    23         {
    24             V[i] = new Node;
    25             V[i]->data = i;
    26             V[i]->next = NULL;
    27         }
    28         while (m--)
    29         {
    30             cin >> x >> y;//标准前插三部曲
    31             p = new Node;//建节点
    32             p->data = y;//赋初值
    33             p->next = V[x]->next;//连后继
    34             V[x]->next = p;//挂前驱
    35             p = new Node;//on the contrary
    36             p->data = x;
    37             p->next = V[y]->next;
    38             V[y]->next = p;
    39         }
    40         cin>>f;//new node coming 
    41         for (int i = 1; i <= n; i++)
    42         {
    43             cout << V[i]->data;
    44             p = V[i]->next;
    45             while (p)
    46             {
    47                 cout<<" "<<p->data;
    48                 p = p->next;
    49             }
    50             cout << endl;
    51         }
    52         cout << f << endl;
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    PointToPointNetDevice doesn't support TapBridgeHelper
    NS3系列—10———NS3 NodeContainer
    NS3系列—9———NS3 IP首部校验和
    NS3系列—8———NS3编译运行
    【习题 7-6 UVA
    【Good Bye 2017 C】 New Year and Curling
    【Good Bye 2017 B】 New Year and Buggy Bot
    【Good Bye 2017 A】New Year and Counting Cards
    【Educational Codeforces Round 35 D】Inversion Counting
    【Educational Codeforces Round 35 C】Two Cakes
  • 原文地址:https://www.cnblogs.com/wind-chaser/p/10066458.html
Copyright © 2020-2023  润新知