摘自计蒜客: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;
}