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. Spring扩展:Spring框架的由来

    一.Spring框架的由来

  2. Ubuntu14.04默认cmake升级为3.x

    由于Ubuntu14.04的cmake版本为2.8.x,而如果需要cmake3.x版本时,无法生成makefile,有两种方法可以安装cmake3.4.1: sudo apt-get install ...

  3. mysql update/delete in 子查询改写

    #子查询(不支持) limit ,); #改写 limit ,) t ; #子查询(不支持) delete from `user` where id in ( ) ); #改写 delete from ...

  4. Android Studio下载/更新SDK

    今天安装配置Android Studio的时候,用SDK Manager下载SDK的时候只显示了一个7.0,别的都刷新不出来(被墙了).去网上搜索怎么解决,发现很多帖子的方法已经过时了(跟现在的AS版 ...

  5. Node.js IO处理输入和回显,以及当今web应用程序的发展史

    1.关于Node.js IO处理输入和回显 在Windows终端或者CD中输入   echo  'I must learn about Node.js' 结果将刚刚输入的   echo  'I mus ...

  6. C#-创建并添加TXT文件

    public static void WriteToText(string txtContent, string txtPath) { using (FileStream fs = new FileS ...

  7. 基于 Web 的 Go 语言 IDE - Wide 1.5.1 发布!

    Wide 是一个基于 Web 的 Go 语言 IDE, 其目标不是彻底代替本地 IDE,而是做本地 IDE 很难做到的事情: 分享代码:类似 playground,但支持多文件并提供嵌入方式,在其他网 ...

  8. Android ListView实现新闻客户端的新闻内容图文混排

    布局文件: <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/andro ...

  9. axios 同步问题

    Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中(这是官方文档给出的一个解释说明) 它的主要作用是向后台发起异步请求,还有在请求中做更多的可控功能 1. ...

  10. DHCP服务搭建

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP ...