#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
#include<string>
#include<stdlib.h>
#include<list>
#include<vector>
using namespace std;
int main()
{
// freopen("fuck.txt","r",stdin);
int i,j,k;
int map1[100][100];
int map2[100][100];
int t;
int n;
cin>>t;
while (t--)
{
cin>>n;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
cin>>map1[i][j];
memset(map2,0,sizeof(map2));
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
map2[i][i]+=map1[i][j];
}
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (i!=j)
map2[i][j]-=map1[i][j];
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
cout<<map2[i][j]<<' ';
cout<<endl;
}
cout<<endl;
}
}
邻接矩阵转化为Laplace矩阵
Time Limit: 1000 ms Memory Limit: 65535 kB Solved: 97 Tried: 186
Description
图是研究对象关系(本题考虑的关系是对称的关系)的一个强有力工具。在研究中,一般用结点表示对象,若两对象间有联系,则在两结点间连一条线。利用这种方法,可以把众多问题抽象成图模型,从而利用图论的方法进行解决。
在计算机中存储图,常用的一种方式是邻接矩阵,方法是先对图中所有结点编号:1,2,3,…,n(假设有n个结点),对于矩阵第i行第j列元素,如果结点i与结点j有连线,则置该元素为1,否则置为0。显然,邻接矩阵是n*n的,且该矩阵是对称矩阵。下面是图论中著名的Peterson图及其对应的邻接矩阵。
对于图中的结点,与之相连的边数,称为该结点的度。在图论研究中,Laplace矩阵也是图的一种常用表示,该矩阵可以由图的邻接矩阵得到,方法是先求出图中各结点的度,再按照结点编号顺序得到一个度对角矩阵,利用度对角矩阵减去邻接矩阵,就得到图的Laplace矩阵。下面是Peterson图的Laplace矩阵。
从上面矩阵可以观察出,邻接矩阵为1的元素在Laplace矩阵中变为-1,对角元素是邻接矩阵对应行的行和。
Input
有多组测试数据。输入的第一行是整数T(0<T<=200),表示测试数据的组数。每组测试数据的第一行,是一个整数R,表示本组测试数据中邻接矩阵是R*R的,随后是一个R*R的邻接矩阵,每个矩阵元素后有一个空格。其中,2<=R<=20。
Output
对应每组测试数据,输出一个对应的Laplace矩阵,每个矩阵元素后应有一个空格。相邻两个矩阵输出用一个空行隔开。
Simple Input
2
5
0 1 0 1 1
1 0 0 1 0
0 0 0 1 1
1 1 1 1 1
1 0 1 1 1
6
0 1 0 1 1 0
1 1 0 1 1 0
0 0 1 1 0 1
1 1 1 1 1 1
1 1 0 1 0 0
0 0 1 1 0 1
Simple Output
3 -1 0 -1 -1
-1 2 0 -1 0
0 0 2 -1 -1
-1 -1 -1 5 -1
-1 0 -1 -1 4
3 -1 0 -1 -1 0
-1 4 0 -1 -1 0
0 0 3 -1 0 -1
-1 -1 -1 6 -1 -1
-1 -1 0 -1 3 0
0 0 -1 -1 0 3