样例邻接表输出错误求助
  • 板块B3643 图的存储
  • 楼主ordoki
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/8/12 23:30
  • 上次更新2023/11/3 04:10:44
查看原帖
样例邻接表输出错误求助
735252
ordoki楼主2023/8/12 23:30
#include<bits/stdc++.h>
using namespace std;
int n,m,Map[1005][1005],target[100005],Next[100005],head[1005],tot;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		Map[u][v]=Map[v][u]=1;
		target[++tot]=v;
		Next[tot]=head[u];
		head[u]=tot;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)
			printf("%d ",Map[i][j]);
		printf("\n");
	}
	for(int i=1;i<=n;i++){
		int degree=0,p=head[i];
		while(p){
			degree++;
			p=Next[p];
		}
		printf("%d ",degree);
		p=head[i];
		while(p){
			printf("%d ",target[p]);
			p=Next[p];
		}
		printf("\n");
	}
	return 0;
}
2023/8/12 23:30
加载中...