結果
問題 | No.1660 Matrix Exponentiation |
ユーザー | LayCurse |
提出日時 | 2021-08-27 21:50:08 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 15 ms / 2,000 ms |
コード長 | 4,472 bytes |
コンパイル時間 | 2,814 ms |
コンパイル使用メモリ | 220,712 KB |
実行使用メモリ | 13,504 KB |
最終ジャッジ日時 | 2024-11-21 01:44:16 |
合計ジャッジ時間 | 4,131 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 3 ms
7,488 KB |
testcase_02 | AC | 3 ms
6,816 KB |
testcase_03 | AC | 3 ms
6,816 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 3 ms
6,816 KB |
testcase_07 | AC | 3 ms
6,820 KB |
testcase_08 | AC | 3 ms
6,820 KB |
testcase_09 | AC | 4 ms
7,628 KB |
testcase_10 | AC | 4 ms
9,680 KB |
testcase_11 | AC | 4 ms
7,764 KB |
testcase_12 | AC | 3 ms
6,816 KB |
testcase_13 | AC | 2 ms
6,820 KB |
testcase_14 | AC | 3 ms
6,820 KB |
testcase_15 | AC | 2 ms
6,824 KB |
testcase_16 | AC | 3 ms
7,620 KB |
testcase_17 | AC | 2 ms
6,816 KB |
testcase_18 | AC | 3 ms
7,748 KB |
testcase_19 | AC | 12 ms
10,652 KB |
testcase_20 | AC | 9 ms
8,604 KB |
testcase_21 | AC | 10 ms
9,076 KB |
testcase_22 | AC | 4 ms
7,780 KB |
testcase_23 | AC | 7 ms
8,176 KB |
testcase_24 | AC | 15 ms
13,076 KB |
testcase_25 | AC | 8 ms
11,216 KB |
testcase_26 | AC | 15 ms
11,356 KB |
testcase_27 | AC | 8 ms
13,504 KB |
testcase_28 | AC | 12 ms
11,148 KB |
testcase_29 | AC | 9 ms
11,480 KB |
ソースコード
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("inline") #include<bits/stdc++.h> using namespace std; void*wmem; char memarr[96000000]; template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } template<class S, class T> inline S chmax(S &a, T b){ if(a<b){ a=b; } return a; } struct graph{ int N; int*es; int**edge; void setDirectEdge(int N__, int M, int A[], int B[], void **mem = &wmem){ int i; N = N__; walloc1d(&es, N, mem); walloc1d(&edge, N, mem); walloc1d(&edge[0], M, mem); for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ es[A[i]]++; } for(i=(0);i<(N);i++){ walloc1d(&edge[i], es[i], mem); } for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ edge[A[i]][es[A[i]]++] = B[i]; } } int TopologicalSort(int res[], void *mem=wmem){ int i; int j; int k; int rs; int*deg; int*q; int qs = 0; int qe = 0; walloc1d(°, N, &mem); walloc1d(&q, N, &mem); rs = 0; for(i=(0);i<(N);i++){ deg[i] = 0; } for(i=(0);i<(N);i++){ for(j=(0);j<(es[i]);j++){ deg[edge[i][j]]++; } } for(i=(0);i<(N);i++){ if(deg[i]==0){ q[qe++] = i; } } while(qs < qe){ i = q[qs++]; res[rs++] = i; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; deg[k]--; if(deg[k]==0){ q[qe++] = k; } } } if(rs==N){ return 1; } return 0; } int longestPath_length(void *mem = wmem){ int*dp; int*arr; int res = 0; int i; int j; int ii; int jj; walloc1d(&dp, N, &mem); walloc1d(&arr, N, &mem); if(!TopologicalSort(arr, mem)){ return -1; } for(i=(0);i<(N);i++){ dp[i] = 0; } for(ii=(0);ii<(N);ii++){ i = arr[ii]; chmax(res, dp[i]); for(jj=(0);jj<(es[i]);jj++){ j = edge[i][jj]; chmax(dp[j], dp[i] + 1); } } return res; } } ; int N; int K; int A[100000]; int B[100000]; graph g; int main(){ wmem = memarr; int res; rd(N); rd(K); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(K);Lj4PdHRW++){ rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1); rd(B[Lj4PdHRW]);B[Lj4PdHRW] += (-1); } } g.setDirectEdge(N,K,A,B); res = g.longestPath_length(); if(res==-1){ wt_L(res); wt_L('\n'); return 0; } wt_L(res+1); wt_L('\n'); return 0; } // cLay version 20210819-1 [beta] // --- original code --- // int N, K, A[1d5], B[1d5]; // graph g; // { // int res; // rd(N,K,(A--,B--)(K)); // g.setDirectEdge(N,K,A,B); // res = g.longestPath_length(); // if(res==-1) wt(res), return 0; // wt(res+1); // }