1. Title Description: Click to open the link
2. Problem-solving ideas: The subject is actually known as an no-map, asking the length of the path is less than or equal to a total number of paths. It can be solved by establishing the transfer matrix by using the matrix fast power. wherein, the transfer matrix is the input time adjacency matrix, while adding the last column, all set to 1, indicating the total number of answers starting from I, the path with a length of not more than M (the last line of the 1~n column is all 0 rows, can be understood as the empty set), Then the transfer matrix squared M-1 times is the path length of the transfer matrix m (here the path length refers to the number of vertices, vertex = number of sides +1, so only need to multiply M-1). Why it is easy to sum, you can set a matrix of the first row is 1 B,b*trans after b[1][n+1] is the sum of all items in column n+1 of a. Output can be.
3. Code:
#define _crt_secure_no_warnings#include<iostream> #include <algorithm> #include <cassert> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include <queue> #include <deque> #include <cstdlib> #include <cstdio> #include < cstring> #include <cmath> #include <ctime> #include <cctype> #include <functional>using namespace Std; #define ME (s) memset (s,0,sizeof (s)) #define PB push_backtypedef Long long ll;typedef unsigned int uint;typed EF unsigned long long ull;typedef pair <ll, int> p;const int MOD = 2015;const int N = 100000 + 5;const int sz = 55;s Truct Matrix{int M[sz][sz]; Matrix () {Me (M);} Matrix operator* (const matrix&b) {matrix c;for (int i = 0; i<sz; i++) for (int j = 0; j<sz; j + +) for (int k = 0; k& Lt;sz; k++) C.m[i][j] = (C.m[i][j] + m[i][k] * b.m[k][j])% Mod;return C;} Matrix get (int n) {Matrix res, b = *this;for (int i = 0; i<sz; i++) res.m[i][i] = 1;while (n>0) {if (N & 1) res = Res*b;b = B*b;n >>= 1;} return res;}}; int main () {int t;for (scanf ("%d", &t); t--;) {Matrix A, b;int m, N, CNT, x;scanf ("%d%d", &n, &m); for (int i = 1; I <= n + 1; i++) A.m[i][n + 1] = 1;// The n+1 column is set to 1 for easy summation for (int i = 1; I <= n; i++) {scanf ("%d", &cnt), for (int j = 1; J <= CNT; j + +) {scanf ("%d", &x); A.M[I][X] = 1;}} for (int i = 1; I <= n + 1; i++) The 1th row of the//b matrix is all 1 to facilitate summation of the n+1 column of the A matrix b.m[1][i] = 1;a = A.get (m), B = b*a;if (M = = 1) printf ("%d\n", n + 1), Else printf ("%d\n", B.m[1][n + 1]);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5411 CRB and Puzzle (2015-year School competition 10th)