Graph I - Graph
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的更多相关文章
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- Theano Graph Structure
Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...
- 纸上谈兵: 图 (graph)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...
- 图数据库(graph database)资料收集和解析 - daily
Motivation 图数据库中的高科技和高安全性中引用了一个关于图数据库(graph database)的应用前景的乐观估计: 预计到2017年,图数据库产业在数据库市场的份额将从2个百分点增长到2 ...
- 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 ...
- [Falcor] Intro to JSON Graph
JSON is a very commonly used data interchange format. Unfortunately while most application domain mo ...
- codeforces 340D Bubble Sort Graph(dp,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Bubble Sort Graph Iahub recently has lea ...
- Graph.js
Graph.js Graph.js A JavaScript library for rendering a graph of nodes
- 【转】使用Boost Graph library(二)
原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...
随机推荐
- javascript window.opener的用法分析
window.opener 返回的是创建当前窗口的那个窗口的引用 window.opener 的用法 window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接 ...
- java 包装类和基础数据
package com.tercher.demo; public class LangClass { public static void main(String[] args) { //所有的包装类 ...
- jq中的isArray方法分析,如何判断对象是否是数组
<!DOCTYPE html> <html> <head> <title>jq中的isArray方法分析</title> <meta ...
- JavaScript中各存在性函数
JavaScript中有很多表示存在性和从属关系的函数,本文介绍如下几个: 1)有关实例与构造函数原型之间的关系:isPrototypeOf(),Object.getPrototypeOf(); 2) ...
- 让你跟上nodejs的资源
For a long time, JavaScript developers hoped for a server-side solution that would allow them to ful ...
- JavaWeb学习总结(五):HttpServletRespone对象(一)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- 关于echarts绘制树图形的注意事项(文字倾斜、数据更新、缓存重绘问题等)
最近项目中使用到echarts的树操作,对其中几点注意事项进行下总结. 效果图: 1.基础配置 options的配置如下: { tooltip: { trigger: 'item', triggerO ...
- 服务器学习笔记之servlet
初衷 想学习下服务器这边的知识,制定了一条学习路线:java8--->servlet--->springMvc--->springBoot--->springCloud.在此当 ...
- js中的正则表达式的运用
正则表达式是一个拆分字符串并查询相关信息的过程:是现代开发中很重要的一环.作为一个web开发人员必须牢牢掌握这项技能,才能尽情得在js中驰骋. 1.创建正则表达式: 正则表达式(regular exp ...
- visual studio 2015通过附加进程调试wcf服务
网站: 打开wcf服务所在的项目 然后调用iis上部署的HLFC(crm)项目的接口就可以进行调试 注意 WCF服务项目要以管理员身份打开,调用wcf服务的项目要在iis中部署并点击调用后才能在附加到 ...