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. 14、IO (字节流、字符流)

    输入和输出 * A:输入和输出 * a: 参照物 * 到底是输入还是输出,都是以Java程序为参照 * b: Output * 把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作 ...

  2. 用JavaScript将数字转换为大写金额(收藏)

    (非原创, 来自网络,仅作收藏) var digitUppercase = function(n) { var fraction = ['角', '分']; var digit = [ '零', '壹 ...

  3. 阿里java面试题,你能答对多少?

    答对以下这些面试题,可以淘汰掉 80 % 的求职竞争者. 1.hashcode相等两个类一定相等吗?equals呢?相反呢? 2.介绍一下集合框架? 3.hashmap hastable 底层实现什么 ...

  4. Class类文件结构简介

    Java跨平台的基础 各种不同平台的虚拟机与所有平台都统一使用的程序存储格式——字节码(ByteCode)是构成平台无关性的基石,也是语言无关性的基础.Java虚拟机不和包括Java在内的任何语言绑定 ...

  5. 深入辨析jvm内存区域

    Java内存区域 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域: 程序计数器.虚拟机栈.本地方法栈.Java堆.方法区(运行时常量池).直接内存 程序计数器 当 ...

  6. 深入理解LinkedBlockingQueue

      说明 通过阅读源码,了解LinkedBlockingQueue的特性.本文基于JDK1.7源码 正文 通过查询API对LinkedBlockingQueue特点进行简单的了解: LinkedBlo ...

  7. mybatis必知必会一

    入门: 1.每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的.SqlSessionFactory 的实例可以通过 SqlSessionFactoryBui ...

  8. Chromium base库分割字符串SplitString

    前一段时间在工作过程中遇到一个场景需要将http response中的request header中的cookie字段取出并进行解析,但是手头没有解析cookie的工具类,同时cookie的表现就是个 ...

  9. js中contains()方法的了解

    今天第一次碰到了contains()方法,处于好奇了解了一下:发现在某些场合还是挺有用的. contains(),js原生方法,用于判断DOM元素的包含关系: 需要注意的是:它以HTMLElement ...

  10. 安装和使用phpstorm

    下载网址:https://www.jetbrains.com/phpstorm/ 第一步:解压在官网上所下的文件,双击 Phpstorm10.0.3.exe 第二步:安装目录选择,自定义选择安装目录, ...