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>
using namespace std;
const int N = 100; int main()
{
int M[N][N]; // 0 0起点的邻接矩阵
int n, u, k, v; cin >> n;
for(int i = 0; i < n; ++ i)
{
for(int j = 0; j < n; ++ j)
{
M[i][j] = 0;
}
} for(int i = 0; i < n; ++ i)
{
cin >> u >> k;
u --; // 转换为0起点
for(int j = 0; j < k; ++ j)
{
cin >> v;
v --; // 转换为0起点
M[u][v] = 1; // 在u和v之间画出一条边
}
} for(int i = 0; i < n; ++ i)
{
for(int j = 0; j < n; ++ j)
{
if(j) cout << " ";
cout << M[i][j];
}
cout << endl;
} return 0;
}

  

Graph I - Graph的更多相关文章

  1. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  2. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

  3. 纸上谈兵: 图 (graph)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...

  4. 图数据库(graph database)资料收集和解析 - daily

    Motivation 图数据库中的高科技和高安全性中引用了一个关于图数据库(graph database)的应用前景的乐观估计: 预计到2017年,图数据库产业在数据库市场的份额将从2个百分点增长到2 ...

  5. Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)

    D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. [Falcor] Intro to JSON Graph

    JSON is a very commonly used data interchange format. Unfortunately while most application domain mo ...

  7. codeforces 340D Bubble Sort Graph(dp,LIS)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has lea ...

  8. Graph.js

    Graph.js Graph.js A JavaScript library for rendering a graph of nodes

  9. 【转】使用Boost Graph library(二)

    原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...

随机推荐

  1. ChannelSftp.chmod方法赋权文件夹

    最近对接分账工作需要上传分账文件到指定sftp服务器,在给sftp上文件夹赋权是遇到一个问题: 具体是ChannelSftp的chmod(int permissions, String path)方法 ...

  2. java util 中set,List 和Map的使用

    https://www.cnblogs.com/Lxiaojiang/p/6231724.html 转载

  3. ASP.NET MVC与ASP.NET WebForm

    ASP.NET MVC是微软公司的一款WEB开发框架,整合了“模型-视图-控制器”架构的高效与整洁,是敏捷开发最现代的思想与技术.它是传统ASP.NET WebForm的一个完善的替代品. 1.当今的 ...

  4. Java 基础:数组

    一.数组声明: int[] x; int x[]; 在Java中一般使用前者,机把int[]看做一个类型,C++中只能后者 二.数组初始化: 直接提供值: int[] x = {1, 3, 4}; i ...

  5. Struts 类型转换之局部和全局配置

    我们碰到过很多情况,就是时间日期常常会出现错误,这是我们最头疼的事,在struts2中有一些内置转换器,也有一些需要我们自己配置. 我们为什么需要类型转换呢? 在基于HTTP协议的Web应用中 客户端 ...

  6. 51NOD1965:奇怪的式子

    传送门 拆开变成 \[\prod_{i=1}^{n}\sigma_0(i)^{\mu(i)}\prod_{i=1}^{n}\sigma_0(i)^{i}\] 考虑 \(\prod_{i=1}^{n}\ ...

  7. LOJ572: Misaka Network 与求和

    传送门 假设 \(f^k(i)\) 就是 \(f(i)\) 莫比乌斯反演得到 \[ans=\sum_{i=1}^{N}\lfloor\frac{N}{i}\rfloor^2\sum_{d|i}f(d) ...

  8. php 生成唯一id的几种解决方法(实例)

    php 生成唯一id,网上查了下,有很多的方法 1.md5(time() . mt_rand(1,1000000)); 这种方法有一定的概率会出现重复 2.php内置函数uniqid() uniqid ...

  9. vs code上配置Scala

    转自:https://www.cnblogs.com/steven-yang/p/5852988.html 百度的结果表达太奇怪,简单记一笔. 1.下载一个scala的压缩包,https://www. ...

  10. 英文版win10更新以后, 中文软件变成乱码

    原因是非Unicode程序的语言设置失效了 在区域设置里把当前系统区域设置改成"英语(美国)", 重启电脑后再改回"中文(简体, 中国)", 再重启就好了.