Skip to content

M19943 图的拉普拉斯矩阵

cpp
#include <iostream>
#include <vector>

using namespace std;

class Vertex {
private:
	vector<int> adj;
	
public:
	void add_edge_to(int v){
		adj.push_back(v);
	}
	
	int get_degree(){
		return adj.size();
	}
	
	vector<int> get_adjacent(){
		return adj;
	}
};

class Graph {
private:
	int n;
	vector<Vertex> node;
	
public:
	void init(int _n){
		n = _n;
		node.resize(n);
	}
	
	void add_edge(int u, int v){
		node[u].add_edge_to(v);
		node[v].add_edge_to(u);
	}
	
	vector<vector<int>> get_laplace(){
		vector<vector<int>> ans(n, vector<int>(n));
		for (int i = 0; i < n; i++){
			ans[i][i] += node[i].get_degree();
			for (int j : node[i].get_adjacent()){
				ans[i][j]--;
			}
		}
		return ans;
	}
};

void output(vector<vector<int>> mat){
	for (vector<int> i : mat){
		for (int j : i){
			cout << j << " ";
		}
		cout << endl;
	}
}

int main(){
	int n, m;
	Graph g;
	cin >> n >> m;
	g.init(n);
	for (int i = 1; i <= m; i++){
		int a, b;
		cin >> a >> b;
		g.add_edge(a, b);
	}
	output(g.get_laplace());
	return 0;
}