牛客網NOIP賽前集訓營-提高組(第七場)C 洞穴
阿新 • • 發佈:2018-10-31
思路:
矩陣乘法
由於只需要知道(a[i][j]) ^ l 的 第a行第b個元素
所以我們每次在做矩陣乘法時只需要算第a行就可以了
還要像矩陣快速冪一樣預處理出(a[i][j]) ^ (1<<d)
程式碼:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #definepi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pli, int> #definemem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 105; bool a[N][N][35]; bool t[N][2]; int main() { int n, m, u, v, q, l; scanf("%d %d", &n, &m);for (int i = 0; i < m; i++) { scanf("%d %d", &u, &v); a[u][v][0] = true; } for (int d = 1; d <= 30; d++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if(a[i][j][d-1]) for (int k = 1; k <= n; k++) { if(a[j][k][d-1]) { a[i][k][d] = true; } } } } } scanf("%d", &q); while(q--) { scanf("%d %d %d", &l, &u, &v); int now = 1; for (int i = 1; i <= n; i++) t[i][now] = 0; t[u][now] = 1; for (int i = 30; i >= 0; i--) { if(l&(1<<i)) { now ^= 1; for (int j = 1; j <= n; j++) t[j][now] = 0; for (int j = 1; j <= n; j++) { if(t[j][now^1]) { for (int k = 1; k <= n; k++) { if(a[j][k][i]) t[k][now] = 1; } } } } } if(t[v][now]) puts("YES"); else puts("NO"); } return 0; }