邻接表转邻接矩阵

来源:互联网 发布:ios刷机用什么软件 编辑:程序博客网 时间:2024/06/06 05:20

假设无向图G采用邻接矩阵存储,编写一个算法输出邻接表。



Description

第一行为一个整数n,表示顶点的个数顶点编号为0n-1,接下来是为一个n*n大小的整数矩阵,表示图的邻接关系。数字为0表示不邻接,1表示邻接。


Input

输出图G的邻接表。第一行表示顶点0可直接到达的顶点编号。其他行定义相同。


Output
1
2
3
4
5
6
5
0 1 0 1 1
1 0 1 1 0
0 1 0 1 1
1 1 1 0 1
1 0 1 1 0
Sample Input
1
2
3
4
5
134
023
134
0124
023
Sample Output

#include<iostream>

#include<stdlib.h>
#include<string.h>
using namespace std;
struct point
{
int date;
struct point *next;
};
int main()
{
void res(int total);
int total;
while(cin>>total)
{
res(total);
}
return 0;
}
void res(int total)
{
struct point *p,*q,arr[1000];
int e[100][100];
int i,j;
for(i=0;i<total;i++)
{
arr[i].next=NULL;
for(j=0;j<total;j++)
{
cin>>e[i][j];
p=(struct point *)malloc(sizeof(struct point));
p->date=j;
p->next=NULL;
if(e[i][j]==1)
{
if(arr[i].next==NULL)
{
arr[i].next=p;
}
else
{
q->next=p;
}
q=p;
}//想了好久终于出来了。。。

}
}
for(i=0;i<total;i++)
{
p=arr[i].next;
while(p!=NULL)
{
cout<<p->date;
p=p->next;
}
cout<<endl;
}
}
0 0
原创粉丝点击