結果
問題 | No.1023 Cyclic Tour |
ユーザー | LayCurse |
提出日時 | 2020-04-10 23:24:13 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 315 ms / 2,000 ms |
コード長 | 12,114 bytes |
コンパイル時間 | 2,848 ms |
コンパイル使用メモリ | 228,984 KB |
実行使用メモリ | 27,008 KB |
最終ジャッジ日時 | 2024-09-16 01:05:05 |
合計ジャッジ時間 | 14,728 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 55 ms
14,080 KB |
testcase_05 | AC | 54 ms
13,952 KB |
testcase_06 | AC | 64 ms
14,848 KB |
testcase_07 | AC | 54 ms
14,464 KB |
testcase_08 | AC | 103 ms
13,312 KB |
testcase_09 | AC | 127 ms
17,152 KB |
testcase_10 | AC | 121 ms
17,152 KB |
testcase_11 | AC | 135 ms
17,920 KB |
testcase_12 | AC | 125 ms
18,816 KB |
testcase_13 | AC | 119 ms
17,920 KB |
testcase_14 | AC | 84 ms
9,984 KB |
testcase_15 | AC | 125 ms
18,560 KB |
testcase_16 | AC | 118 ms
12,416 KB |
testcase_17 | AC | 277 ms
27,008 KB |
testcase_18 | AC | 114 ms
12,288 KB |
testcase_19 | AC | 90 ms
11,264 KB |
testcase_20 | AC | 273 ms
21,632 KB |
testcase_21 | AC | 278 ms
21,760 KB |
testcase_22 | AC | 277 ms
22,400 KB |
testcase_23 | AC | 314 ms
23,424 KB |
testcase_24 | AC | 304 ms
25,728 KB |
testcase_25 | AC | 265 ms
21,504 KB |
testcase_26 | AC | 280 ms
21,760 KB |
testcase_27 | AC | 263 ms
21,248 KB |
testcase_28 | AC | 300 ms
25,088 KB |
testcase_29 | AC | 281 ms
25,472 KB |
testcase_30 | AC | 308 ms
25,344 KB |
testcase_31 | AC | 278 ms
24,064 KB |
testcase_32 | AC | 301 ms
25,856 KB |
testcase_33 | AC | 315 ms
25,600 KB |
testcase_34 | AC | 231 ms
18,944 KB |
testcase_35 | AC | 235 ms
19,840 KB |
testcase_36 | AC | 271 ms
21,760 KB |
testcase_37 | AC | 280 ms
24,192 KB |
testcase_38 | AC | 287 ms
24,960 KB |
testcase_39 | AC | 296 ms
23,296 KB |
testcase_40 | AC | 291 ms
23,680 KB |
testcase_41 | AC | 284 ms
23,552 KB |
testcase_42 | AC | 253 ms
20,352 KB |
testcase_43 | AC | 82 ms
10,624 KB |
testcase_44 | AC | 71 ms
14,464 KB |
testcase_45 | AC | 101 ms
23,808 KB |
testcase_46 | AC | 108 ms
23,936 KB |
testcase_47 | AC | 67 ms
22,528 KB |
testcase_48 | AC | 204 ms
22,400 KB |
testcase_49 | AC | 250 ms
22,528 KB |
testcase_50 | AC | 177 ms
22,528 KB |
testcase_51 | AC | 164 ms
20,864 KB |
testcase_52 | AC | 189 ms
20,864 KB |
ソースコード
#pragma GCC optimize ("Ofast") #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); } 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(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } a[k] = aval; } template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } a[k] = aval; b[k] = bval; } template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; } template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } for(i=sz-1;i>k;i--){ d[i] = d[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; d[k] = dval; } struct unionFind{ int *d; int N; int M; inline void malloc(const int n){ d = (int*)std::malloc(n*sizeof(int)); M = n; } inline void malloc(const int n, const int fg){ d = (int*)std::malloc(n*sizeof(int)); M = n; if(fg){ init(n); } } inline void free(void){ std::free(d); } inline void walloc(const int n, void **mem=&wmem){ walloc1d(&d, n, mem); M = n; } inline void walloc(const int n, const int fg, void **mem=&wmem){ walloc1d(&d, n, mem); M = n; if(fg){ init(n); } } inline void init(const int n){ int i; N = n; for(i=(0);i<(n);i++){ d[i] = -1; } } inline void init(void){ init(M); } inline int get(int a){ int t = a; int k; while(d[t]>=0){ t=d[t]; } while(d[a]>=0){ k=d[a]; d[a]=t; a=k; } return a; } inline int connect(int a, int b){ if(d[a]>=0){ a=get(a); } if(d[b]>=0){ b=get(b); } if(a==b){ return 0; } if(d[a] < d[b]){ d[a] += d[b]; d[b] = a; } else{ d[b] += d[a]; d[a] = b; } return 1; } inline int operator()(int a){ return get(a); } inline int operator()(int a, int b){ return connect(a,b); } inline int& operator[](const int a){ return d[a]; } inline int size(int a){ a = get(a); return -d[a]; } inline int sizeList(int res[]){ int i; int sz=0; for(i=(0);i<(N);i++){ if(d[i]<0){ res[sz++] = -d[i]; } } return sz; } } ; template<class T> int coordcomp_L(int n, T arr[], int res[] = NULL, void *mem = wmem){ int i; int k = 0; pair<T,int> *r; walloc1d(&r, n, &mem); for(i=(0);i<(n);i++){ r[i].first = arr[i]; r[i].second = i; } sort(r, r+n); if(res != NULL){ for(i=(0);i<(n);i++){ if(i && r[i].first != r[i-1].first){ k++; } res[r[i].second] = k; } } else{ for(i=(0);i<(n);i++){ if(i && r[i].first != r[i-1].first){ k++; } arr[r[i].second] = k; } } return k+1; } template<class T> int coordcomp_L(int n1, T arr1[], int n2, T arr2[], int res1[] = NULL, int res2[] = NULL, void *mem = wmem){ int i; int k = 0; pair<T,int> *r; walloc1d(&r, n1+n2, &mem); for(i=(0);i<(n1);i++){ r[i].first = arr1[i]; r[i].second = i; } for(i=(0);i<(n2);i++){ r[n1+i].first = arr2[i]; r[n1+i].second = n1+i; } sort(r, r+n1+n2); for(i=(0);i<(n1+n2);i++){ if(i && r[i].first != r[i-1].first){ k++; } if(r[i].second < n1){ if(res1!=NULL){ res1[r[i].second] = k; } else{ arr1[r[i].second] = k; } } else{ if(res2!=NULL){ res2[r[i].second-n1] = k; } else{ arr2[r[i].second-n1] = k; } } } return k+1; } struct graph{ int N; int *es; int **edge; void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){ int i; N = N__; walloc1d(&es, N, mem); walloc1d(&edge, N, mem); for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ es[A[i]]++; es[B[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]; edge[B[i]][es[B[i]]++] = A[i]; } } 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]; } } graph reverse(void **mem = &wmem){ int i; int j; int k; graph g; g.N = N; walloc1d(&g.es, N, mem); walloc1d(&g.edge, N, mem); for(i=(0);i<(N);i++){ g.es[i] = 0; } for(i=(0);i<(N);i++){ for(j=(0);j<(es[i]);j++){ g.es[edge[i][j]]++; } } for(i=(0);i<(N);i++){ walloc1d(&g.edge[i], g.es[i], mem); } for(i=(0);i<(N);i++){ g.es[i] = 0; } for(i=(0);i<(N);i++){ for(j=(0);j<(es[i]);j++){ k = edge[i][j]; g.edge[k][g.es[k]++] = i; } } return g; } inline int sccDFS(int num[], int st, int mx){ int i; int j; num[st]=-2; for(i=(0);i<(es[st]);i++){ j=edge[st][i]; if(num[j]==-1){ mx=sccDFS(num,j,mx); } } num[st]=mx; return mx+1; } int scc(int res[], void *mem = wmem){ int i; int j; int k; int ret=0; graph r; int *st; int st_size; int *num; int *nrv; r = reverse(&mem); walloc1d(&st, N, &mem); walloc1d(&num, N, &mem); walloc1d(&nrv, N, &mem); for(i=(0);i<(N);i++){ res[i] = num[i] = -1; } k = 0; for(i=(0);i<(N);i++){ if(num[i]==-1){ k = sccDFS(num,i,k); } } for(i=(0);i<(N);i++){ nrv[num[i]] = i; } for(k=N-1;k>=0;k--){ i=nrv[k]; if(res[i]>=0){ continue; } res[i]=ret; st_size=0; st[st_size++]=i; while(st_size){ i=st[--st_size]; for(j=(0);j<(r.es[i]);j++){ if(res[r.edge[i][j]]==-1){ res[r.edge[i][j]]=ret; st[st_size++]=r.edge[i][j]; } } } ret++; } return ret; } } ; int N; int M; int A[400000]; int B[400000]; int C[400000]; int n; int m; int a[400000]; int b[400000]; int scc[100000]; graph g; int vis[100000]; int solve(int n, int b = -1){ int Lj4PdHRW; if(vis[n]){ return 0; } vis[n] = 2; for(Lj4PdHRW=(0);Lj4PdHRW<(g.es[n]);Lj4PdHRW++){ auto &i = g.edge[n][Lj4PdHRW]; if(i==b){ continue; } if(vis[i]==2){ return 1; } if(solve(i, n)){ return 1; } } vis[n] = 1; return 0; } int main(){ int i; wmem = memarr; int k; unionFind uf; set<pair<int,int>> s0; set<pair<int,int>> s1; rd(N); rd(M); { int e98WHCEY; for(e98WHCEY=(0);e98WHCEY<(M);e98WHCEY++){ rd(A[e98WHCEY]);A[e98WHCEY] += (-1); rd(B[e98WHCEY]);B[e98WHCEY] += (-1); rd(C[e98WHCEY]);C[e98WHCEY] += (-1); } } for(i=(0);i<(M);i++){ if(C[i]==0){ s0.insert( make_pair(A[i], B[i]) ); if(s1.count(make_pair(A[i], B[i]))){ wt_L("Yes"); wt_L('\n'); return 0; } if(s1.count(make_pair(B[i], A[i]))){ wt_L("Yes"); wt_L('\n'); return 0; } } else{ s1.insert( make_pair(A[i], B[i]) ); if(s0.count(make_pair(A[i], B[i]))){ wt_L("Yes"); wt_L('\n'); return 0; } if(s0.count(make_pair(B[i], A[i]))){ wt_L("Yes"); wt_L('\n'); return 0; } if(s1.count(make_pair(B[i], A[i]))){ wt_L("Yes"); wt_L('\n'); return 0; } } } for(i=(0);i<(M);i++){ if(C[i]==0){ arrInsert(m,m,a,A[i],b,B[i]); } } g.setEdge(N,m,a,b); for(i=(0);i<(N);i++){ if(!vis[i]){ if(solve(i)){ wt_L("Yes"); wt_L('\n'); return 0; } } } uf.malloc(N, 1); for(i=(0);i<(M);i++){ if(C[i]==0){ uf(A[i],B[i]); } } m = 0; for(i=(0);i<(M);i++){ if(C[i]==1){ arrInsert(m,m,a,uf(A[i]),b,uf(B[i])); } } for(i=(0);i<(m);i++){ if(a[i]==b[i]){ wt_L("Yes"); wt_L('\n'); return 0; } } n =coordcomp_L(m,a,m,b); g.setDirectEdge(n,m,a,b); if(g.scc(scc) < n){ wt_L("Yes"); wt_L('\n'); return 0; } wt_L("No"); wt_L('\n'); return 0; } // cLay varsion 20200408-1 // --- original code --- // int N, M, A[4d5], B[4d5], C[4d5]; // // int n, m, a[4d5], b[4d5], scc[1d5]; // graph g; // // int vis[1d5]; // int solve(int n, int b = -1){ // if(vis[n]) return 0; // vis[n] = 2; // rep[g.edge[n]](i,g.es[n]){ // if(i==b) continue; // if(vis[i]==2) return 1; // if(solve(i, n)) return 1; // } // vis[n] = 1; // return 0; // } // // { // int k; // unionFind uf; // set<pair<int,int>> s0, s1; // rd(N,M,(A--,B--,C--)(M)); // // rep(i,M){ // if(C[i]==0){ // s0.insert( make_pair(A[i], B[i]) ); // if(s1.count(make_pair(A[i], B[i]))) wt("Yes"), return 0; // if(s1.count(make_pair(B[i], A[i]))) wt("Yes"), return 0; // } else { // s1.insert( make_pair(A[i], B[i]) ); // if(s0.count(make_pair(A[i], B[i]))) wt("Yes"), return 0; // if(s0.count(make_pair(B[i], A[i]))) wt("Yes"), return 0; // if(s1.count(make_pair(B[i], A[i]))) wt("Yes"), return 0; // } // } // // rep(i,M) if(C[i]==0) arrInsert(m,m,a,A[i],b,B[i]); // g.setEdge(N,m,a,b); // rep(i,N) if(!vis[i]) if(solve(i)) wt("Yes"), return 0; // // uf.malloc(N, 1); // rep(i,M) if(C[i]==0) uf(A[i],B[i]); // // m = 0; // rep(i,M) if(C[i]==1) arrInsert(m,m,a,uf(A[i]),b,uf(B[i])); // rep(i,m) if(a[i]==b[i]) wt("Yes"), return 0; // n = coordcomp(m,a,m,b); // g.setDirectEdge(n,m,a,b); // if(g.scc(scc) < n) wt("Yes"), return 0; // // wt("No"); // }