假设无向图G采用邻接表存储,编写一个算法输出邻接矩阵。
输入
第一行为一个整数n,表示顶点的个数(顶点编号为0到n-1)。第二行表示顶点0可直接到达的顶点编号,其他行定义相同。
输出
输出图G的邻接矩阵。整数矩阵大小为n*n,表示图的邻接关系。数字为0表示不邻接,1表示邻接。
样例输入
5
1 3 4
0 2 3
1 3 4
0 1 2 4
0 2 3
样例输出
01011
10110
01011
11101
10110
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
int n;
cin>>n;
int a[200][200];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
}
}
getchar();
for(int i=0;i<n;i++)
{
char temp;
for(int j=0;j<100;j++)
{
temp = getchar();
if(temp=='\n')
{
break;
}
else if(temp==' ')
{
continue;
}
else
{
a[i][temp-'0']=1;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j];
}
cout<<endl;
}
return 0;
}