摘自計蒜客:http://www.jisuanke.com/course/35/7195
//以下主要針對有向圖,如果遇到無向圖的情況,將每條無向邊對應到有向圖中的正反兩條邊就可以了。
#include <iostream>
#include <cstring>
using namespace std;
class Graph {
private:
int **mat;
int n;
public:
Graph(int input_n) {
n = input_n;
mat = new int*[n];
for (int i = 0; i < n; ++i) {
mat[i] = new int[n];
memset(mat[i], 0, sizeof(int) * n);
}
}
~Graph() {
for (int i = 0; i< n; ++i) {
delete[] mat[i];
}
delete[] mat;
}
void insert(int x, int y) {
mat[x][y] = 1;
}
void output() {
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
};
int main() {
int n, m, x, y;
cin >> n >> m;
Graph g(n);
for (int i = 0; i < m; ++i) {
cin >> x >> y;
g.insert(x, y);
}
g.output();
return 0;
}