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 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...
随机推荐
- 字符串匹配问题(lfyzoj)
问题描述 字符串中只含有括号 (),[],<>,{},判断输入的字符串中括号是否匹配.如果括号有互相包含的形式,从内到外必须是<>,(),[],{},例如.输入: [()] 输 ...
- Groovy内存机制详解
groovy每执行一次脚本,都会生成一个脚本的class对象,并new一个InnerLoader去加载这个对象. 所有的脚本都是由GroovyClassLoader加载的,每次加载脚本都会生成一个新的 ...
- jquery怎么取得有好几个并且name是相同的值
jQuery("input[name='number']").each(function(){ alert(jQuery(this).val()); });
- python安装后无法用cmd命令pip 装包
出现问题: 原因:没有添加环境变量. 解决方法:将python安装目录下的Script目录添加进环境变量,其中有pip.exe,在cmd中输入pip install命令时要运行pip.exe. win ...
- TopCoder14580: EllysRPS
题意 \(yyb\)要去与\(m\)\((m\le100)\)个人玩游戏 由于\(yyb\)忙着切大火题,他没有太多的精力浪费在游戏上 所以仁慈的\(yyb\)决定放\(m\)个人一条生路,不吊打他们 ...
- JavaScript函数与面向对象
一.JS面向对象 function Func(name,age){ //this = obj this.Name = name; this.Age = age; } obj = new Func('r ...
- Java 实时论坛 - Sym 1.4.0 发布
简介 Sym 是一个用 Java 写的实时论坛,欢迎来体验! 如果你需要搭建一个企业内网论坛,请使用 SymX. 作者 Sym 的主要作者是 Daniel 与 Vanessa,所有贡献者可以在这里看到 ...
- Python爬虫教程-08-post介绍(百度翻译)(下)
Python爬虫教程-08-post介绍(下) 为了更多的设置请求信息,单纯的通过urlopen已经不太能满足需求,此时需要使用request.Request类 构造Request 实例 req = ...
- volley6--CacheDispatcher从缓存中获取数据
源码: /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, V ...
- security权限控制
目录 前言 数据库和dimain 静态页面 配置文件 web.xml引入 service校验方法 用户名的获取 不同角色访问控制权限 jsp页面 后台 前言 spring自带角色权限控制框架 用户-角 ...