BJFUOJ-278

基于邻接表的新边的增加

#include<iostream>
using namespace std;
#define maxsize 100
typedef struct ArcNode{
	int val;
	ArcNode* next;
	ArcNode():val(0),next(nullptr){}
	ArcNode(int x):val(x),next(nullptr){}
}ArcNode;

typedef struct VexNode {
	int data;
	ArcNode* first;
	VexNode():data(0),first(nullptr){}
}VexNode;
typedef struct {
	VexNode vexs[maxsize];
	int VexNum, ArcNum;
}ALGraph;


void addArc(ALGraph& G, int n1, int n2) {
	ArcNode* p1 = new ArcNode(n1);
	ArcNode* p2 = new ArcNode(n2);
	int index1 = 0;
	int index2 = 0;
	for (int i = 1; i <= G.VexNum; i++) {
		if (G.vexs[i].data == n1) {
			index1 = i;
		}
		if (G.vexs[i].data == n2) {
			index2 = i;
		}
	}
	p2->next = G.vexs[index1].first;
	G.vexs[index1].first = p2;

	p1->next = G.vexs[index2].first;
	G.vexs[index2].first = p1;
	G.ArcNum++;

}


void print(ALGraph& G) {
	for (int i = 1; i <= G.VexNum; i++) {
		cout << G.vexs[i].data;
		ArcNode* p= G.vexs[i].first;
		while (p) {
			cout << " " << p->val;
			p = p->next;
		}
		cout << endl;
	}
}
void Create(ALGraph& G, int n, int m) {
	G.VexNum = n;
	G.ArcNum = m;
	for (int i = 1; i <= n; i++) {
		G.vexs[i].data = i;
	}
	for (int i = 1; i <= m; i++) {
		int n1; int n2;
		cin >> n1 >> n2;
		addArc(G, n1, n2);
	}
	int new_n1, new_n2;
	cin >> new_n1 >> new_n2;
	addArc(G,new_n1, new_n2);

}

int main() {

	int n, m;
	while (cin >> n >> m && (n != 0 && m != 0)) {
		ALGraph G;

		Create(G,n,m);
		print(G);
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值