Links:
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1612
Test instructions: Give a matrix a size n*n, B = a^1 + a^2 + a^3 + .... A^n, there are no 0 items in B.
The title can be converted to n points numbered 1-n, for any point V, after some arbitrary step to reach U (U for any point set of any one). In discrete mathematics, the matrix-related knowledge a^k of graphs represents the number of steps in the matrix from I to J in step K.
The question is whether the graph is strong or not.
Learn the Kosaraju algorithm can easily solve the relevant proof please see:
Http://www.cnblogs.com/tnt_vampire/archive/2010/04/12/1709895.html
http://blog.csdn.net/dm_vincent/article/details/8554244
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < sstream> #include <cstdio> #include <vector> #include <cmath> #include <queue> #include < stack> #include <set> #include <map> #define Lson o<<1, L, M#define Rson o<<1|1, m+1, R#define PI I Pair<int, int> #define ALL (x) X.begin (), x.end () #define MEM (a) memset (A,0,sizeof (a)) typedef long Long Ll;const Double pi = ACOs ( -1.0); const int MAX = 0x3f3f3f3f;const ll mod = 1000000007ll;const int N = 1005;using namespace std; int T, N, X;int A[n][n], scc[n], CNT, V[n];vector <int> g[n], g2[n], S; void dfs1 (int u) {if (V[u]) return; V[u] = 1; for (int i = 0; i < g[u].size (); i++) {DFS1 (g[u][i]); } s.push_back (u);} void dfs2 (int u) {if (Scc[u]) return; Scc[u] = cnt; for (int i = 0; i < g2[u].size (); i++) {DFS2 (g2[u][i]); }} int main () {//Freopen ("In.txt", "R", stdin);//Freopen ("OUT.txt", "w", stdout); Cin >> T; while (t--) {cin >> n; CNT = 0; MEM (SCC); Mem (v); for (int i = 0; i < n; i++) {g2[i].clear (); G[i].clear (); } s.clear (); for (int i = 0; i < n; i++) {for (int j = 0; J < N; j + +) {scanf ("%d", &x); if (x) {g[i].push_back (j); G2[j].push_back (i); }}} for (int i = 0; i < n; i++) {DFS1 (i); } for (int i = n-1; I >= 0; i--) {if (!scc[s[i]]) {cnt++; DFS2 (S[i]); }} puts (cnt = = 1?) "Not EXISTS": "exists"); } return 0; }
Kosaraju algorithm for tunnels strong connected component of CSU 1612:destroy