清醒疯子 2018-03-06
问题描述
有一个n个结点m条边的有向图,请输出他的关联矩阵。
输入格式
第一行两个整数n、m,表示图中结点和边的数目。n<=100,m<=1000。
接下来m行,每行两个整数a、b,表示图中有(a,b)边。
注意图中可能含有重边,但不会有自环。
输出格式
输出该图的关联矩阵,注意请勿改变边和结点的顺序。
样例输入
5 9
1 2
3 1
1 5
2 5
2 3
2 3
3 2
4 3
5 4
样例输出
1 -1 1 0 0 0 0 0 0
-1 0 0 1 1 1 -1 0 0
0 1 0 0 -1 -1 1 -1 0
0 0 0 0 0 0 0 1 -1
0 0 -1 -1 0 0 0 0 1
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100 + 5
#define M 1000 + 5
//#define LOCAL
/*
//这里输出的是 n*n 的关联矩阵
int a[N][N];
int main() {
#ifdef LOCAL
freopen("algo_48.txt", "r", stdin);
#endif
int n, m, x, y;
memset(a, 0, sizeof(a));
cin >> n >> m;
while(m--) {
cin >> x >>y;
if(a[x][y] == 0) a[x][y] = 1;
if(!a[y][x] == 0) a[y][x] = -1;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(j == 0) cout << a[i][j];
else cout << " " << a[i][j];
}
cout << endl;
}
}
*/
int a[N][M];
int main() {
#ifdef LOCAL
freopen("algo_48.txt", "r", stdin);
#endif
int n, m, x, y;
memset(a, , sizeof(a));
cin >> n >>m;
int temp = ;
while(temp < m) {
cin >> x >> y;
if(a[x][temp] == ) a[x][temp] = ;
if(a[y][temp] == ) a[y][temp] = -;
temp++;
}
for(int i = ; i <= n; i++) {
for(int j = ; j < m; j++) {
if(j == ) cout << a[i][j];
else cout << " " << a[i][j];
}
cout << endl;
}
return ;
}