输入
第一行为一个整数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<stdlib.h>
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}Photo;
int main()
{
int n;
int a[105][105]={0};
cin>>n;
Photo *l,*r,*s;
vector<node *>v(n);
getchar();
for(int i=0;i<n;i++)
{
l=(Photo *)malloc(sizeof(Photo));
r=l;
char ch;
while((ch=getchar())!='\n')
{
if('0'<=ch&&ch<='9')
{
s=(Photo *)malloc(sizeof(Photo));
s->data=ch-'0';
r->next=s;
r=s;
}
r->next=NULL;
}
v[i]=l;
}
for(int i=0;i<n;i++)
{
l=v[i];
l=l->next;
while(l)
{
a[i][l->data]=1;
l=l->next;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j];
}
cout<<endl;
}
return 0;
}