题意 给出方向(有向)然后判断从一个点到另一个点的方案数。如果有无数条那么对应位置置为-1
直接先dp处理出来。dp[i][j] = sum(dp[i][k]*dp[k][j])
同时如果两点之间有无限条路径。那么这两点之间必然有一环存在。有f[k][k]!=0
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <climits> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define PI 3.1415926535897932626 using namespace std; int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);} #define MAXN 35 int dp[MAXN][MAXN]; int N,M; void read() {N = 0;memset(dp,0,sizeof(dp));while (M--){int u,v;scanf("%d%d",&u,&v);dp[u][v] = 1;N = max(N,max(u,v));} } int main() {//freopen("sample.txt","r",stdin);int kase = 0;while (scanf("%d",&M) != EOF){read();for (int k = 0; k <= N; k++)for (int i = 0; i <= N; i++)for (int j = 0 ; j <= N; j++)dp[i][j] += dp[i][k] * dp[k][j];for (int i = 0; i <= N; i++)if (dp[i][i]){for (int j = 0; j <= N; j++)for (int k = 0; k <= N; k++)if (dp[j][i] && dp[i][k]) dp[j][k] = -1;}printf("matrix for city %d\n",kase++);for (int i = 0; i <= N; i++){for (int j = 0; j <= N; j++){if (!j) printf("%d",dp[i][j]);else printf(" %d",dp[i][j]);}putchar('\n');}}return 0; }