Graph(邻接表转为邻接矩阵)

滴答滴答---题目链接 

There are two standard ways to represent a graph G=(V,E)G=(V,E), where VV is a set of vertices and EE is a set of edges; Adjacency list representation and Adjacency matrix representation.

An adjacency-list representation consists of an array Adj[|V|]Adj[|V|] of |V||V| lists, one for each vertex in VV. For each u∈Vu∈V, the adjacency list Adj[u]Adj[u] contains all vertices vv such that there is an edge (u,v)∈E(u,v)∈E. That is, Adj[u]Adj[u] consists of all vertices adjacent to uu in GG.

An adjacency-matrix representation consists of |V|×|V||V|×|V| matrix A=aijA=aij such that aij=1aij=1 if (i,j)∈E(i,j)∈E, aij=0aij=0 otherwise.

Write a program which reads a directed graph GG represented by the adjacency list, and prints its adjacency-matrix representation. GG consists of n(=|V|)n(=|V|) vertices identified by their IDs 1,2,..,n1,2,..,nrespectively.

Input

In the first line, an integer nn is given. In the next nn lines, an adjacency list Adj[u]Adj[u] for vertex uu are given in the following format:

uu kk v1v1 v2v2 ... vkvk

uu is vertex ID and kk denotes its degree. vivi are IDs of vertices adjacent to uu.

Output

As shown in the following sample output, print the adjacent-matrix representation of GG. Put a single space character between aijaij.

Constraints

  • 1≤n≤1001≤n≤100

Sample Input

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

Sample Output

0 1 0 1
0 0 0 1
0 0 0 0
0 0 1 0
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=101;
int e[maxn][maxn];
int main()
{
    int n,u,k,v;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            e[i][j]=0;
    for(int i=0; i<n; i++)
    {
        scanf("%d%d",&u,&k);
        u--;
        for(int j=0; j<k; j++)
        {
            scanf("%d",&v);
            v--;//转化为0起点
            e[u][v]=1;
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(j)printf(" ");
            printf("%d",e[i][j]);
        }
        cout<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值