4008基于邻接矩阵的新边的增加

描述

给定一个无向图,在此无向图中增加一条边。

输入

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

输出

每组数据输出n行。为增加边后的邻接矩阵。每两个数字之间用空格隔开。

输入样例 1 

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

输出样例 1

0 1 2 3
1 0 1 1
2 1 0 1
3 1 1 0
0 1 2 3
1 0 1 1
2 1 0 0
3 1 0 0
//基于邻接矩阵的新顶点的增加
#include <iostream>
#define MVNum 100//最大顶点数 
using namespace std;
typedef struct{
	int vexs[MVNum];//顶点表 
	int arcs[MVNum][MVNum];//邻接矩阵
	int vexnum,arcnum; 
}AMGraph;
void Create_V(AMGraph &G,int name){
	int pos=++G.vexnum;
	G.vexs[pos-1]=name;//存入点名 
	for(int i=1;i<=pos;i++){//点所在的行列清零 
		G.arcs[i-1][pos-1]=0;
		G.arcs[pos-1][i-1]=0;
	}
}
void Create_Arc(AMGraph &G,int h,int k){
	G.arcs[h-1][k-1]=G.arcs[k-1][h-1]=1;//点与对称点都归1 
}
void Out_Graph(AMGraph G){
	cout<<"0 ";//开头输出0 
	for(int i=1;i<G.vexnum;i++) cout<<G.vexs[i-1]<<" ";//输出所有点名 
	cout<<G.vexs[G.vexnum-1]<<endl;
	for(int i=1;i<=G.vexnum;i++){//输出所有弧
		cout<<G.vexs[i-1]<<" ";
		for(int j=1;j<G.vexnum;j++){
			cout<<G.arcs[i-1][j-1]<<" ";
		}
		cout<<G.arcs[i-1][G.vexnum-1]<<endl;
	}
}
void Calculate(int m,int n){
	AMGraph G;
	G.vexnum=G.arcnum=0;
	for(int i=1;i<=m;i++) Create_V(G,i);//构造前n个顶点 
	for(int i=1;i<=n+1;i++){//构造n条边(再新增一条边 买n送1) 
		int h,k;
		cin>>h>>k;//输入左右顶点 
		Create_Arc(G,h,k);//构造边 
	}
	Out_Graph(G);//输出图 
}
int main(){
	int m,n;
	while(cin>>m>>n&&m!=0&&n!=0){//每次处理一行数据 
		Calculate(m,n);
	}
	return 0;
} 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘下来邦我吧

头发加了一根

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值