結果

問題 No.1640 簡単な色塗り
ユーザー LayCurseLayCurse
提出日時 2021-08-06 21:35:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 9,928 bytes
コンパイル時間 3,143 ms
コンパイル使用メモリ 220,820 KB
実行使用メモリ 45,748 KB
最終ジャッジ日時 2023-09-12 01:42:42
合計ジャッジ時間 18,363 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
12,524 KB
testcase_01 AC 2 ms
5,432 KB
testcase_02 AC 2 ms
5,460 KB
testcase_03 AC 2 ms
5,428 KB
testcase_04 AC 55 ms
36,040 KB
testcase_05 AC 55 ms
36,044 KB
testcase_06 AC 2 ms
5,696 KB
testcase_07 AC 2 ms
5,400 KB
testcase_08 AC 2 ms
5,416 KB
testcase_09 AC 2 ms
5,412 KB
testcase_10 AC 220 ms
23,312 KB
testcase_11 AC 174 ms
20,112 KB
testcase_12 AC 132 ms
18,388 KB
testcase_13 AC 470 ms
33,096 KB
testcase_14 AC 456 ms
32,852 KB
testcase_15 AC 76 ms
15,032 KB
testcase_16 AC 108 ms
16,960 KB
testcase_17 AC 329 ms
28,088 KB
testcase_18 AC 12 ms
7,532 KB
testcase_19 AC 122 ms
18,368 KB
testcase_20 AC 212 ms
22,716 KB
testcase_21 AC 171 ms
19,584 KB
testcase_22 AC 8 ms
6,920 KB
testcase_23 AC 245 ms
24,036 KB
testcase_24 AC 35 ms
10,716 KB
testcase_25 AC 135 ms
18,488 KB
testcase_26 AC 282 ms
26,028 KB
testcase_27 AC 72 ms
14,660 KB
testcase_28 AC 414 ms
31,516 KB
testcase_29 AC 276 ms
25,868 KB
testcase_30 AC 12 ms
10,396 KB
testcase_31 AC 173 ms
45,748 KB
testcase_32 AC 116 ms
34,476 KB
testcase_33 AC 68 ms
23,712 KB
testcase_34 AC 122 ms
33,924 KB
testcase_35 AC 52 ms
24,308 KB
testcase_36 AC 12 ms
10,124 KB
testcase_37 AC 18 ms
11,620 KB
testcase_38 AC 124 ms
37,084 KB
testcase_39 AC 48 ms
20,628 KB
testcase_40 AC 37 ms
17,944 KB
testcase_41 AC 74 ms
27,684 KB
testcase_42 AC 44 ms
20,772 KB
testcase_43 AC 71 ms
23,724 KB
testcase_44 AC 48 ms
19,744 KB
testcase_45 AC 33 ms
16,872 KB
testcase_46 AC 12 ms
10,588 KB
testcase_47 AC 10 ms
9,048 KB
testcase_48 AC 148 ms
36,364 KB
testcase_49 AC 5 ms
6,972 KB
testcase_50 AC 2 ms
5,472 KB
testcase_51 AC 2 ms
5,400 KB
testcase_52 AC 243 ms
44,148 KB
testcase_53 TLE -
07_evil_01.txt -- -
07_evil_02.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
using namespace std;
template<class T> struct cLtraits_identity{
  using type = T;
}
;
template<class T> using cLtraits_try_make_signed =
  typename conditional<
    is_integral<T>::value,
    make_signed<T>,
    cLtraits_identity<T>
    >::type;
template <class S, class T> struct cLtraits_common_type{
  using tS = typename cLtraits_try_make_signed<S>::type;
  using tT = typename cLtraits_try_make_signed<T>::type;
  using type = typename common_type<tS,tT>::type;
}
;
void*wmem;
char memarr[96000000];
template<class S, class T> inline auto min_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
  return (typename cLtraits_common_type<S,T>::type) a <= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
template<class S, class T> inline auto max_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
  return (typename cLtraits_common_type<S,T>::type) a >= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
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');
  }
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    my_putchar_unlocked(c[i]);
  }
}
template<class T, class S> struct maxflow{
  int node;
  int st;
  int ed;
  int*es;
  int*emem;
  int**edge;
  int**rev;
  int*level;
  int*qq;
  T**flow;
  T eps;
  void malloc(int N){
    int i;
    es = (int*)std::malloc(N*sizeof(int));
    emem = (int*)std::malloc(N*sizeof(int));
    level = (int*)std::malloc(N*sizeof(int));
    qq = (int*)std::malloc(N*sizeof(int));
    edge = (int**)std::malloc(N*sizeof(int*));
    rev = (int**)std::malloc(N*sizeof(int*));
    flow = (T**)std::malloc(N*sizeof(T*));
    for(i=(0);i<(N);i++){
      emem[i] = 0;
      edge[i] = rev[i] = NULL;
      flow[i] = NULL;
    }
  }
  void malloc(int N, int init_flag){
    int i;
    es = (int*)std::malloc(N*sizeof(int));
    emem = (int*)std::malloc(N*sizeof(int));
    level = (int*)std::malloc(N*sizeof(int));
    qq = (int*)std::malloc(N*sizeof(int));
    edge = (int**)std::malloc(N*sizeof(int*));
    rev = (int**)std::malloc(N*sizeof(int*));
    flow = (T**)std::malloc(N*sizeof(T*));
    for(i=(0);i<(N);i++){
      emem[i] = 0;
      edge[i] = rev[i] = NULL;
      flow[i] = NULL;
    }
    if(init_flag){
      init(N);
    }
  }
  void walloc(int N, void**mem = &wmem){
    int i;
    walloc1d(&es, N, mem);
    walloc1d(&emem, N, mem);
    walloc1d(&level, N, mem);
    walloc1d(&qq, N, mem);
    walloc1d(&edge, N, mem);
    walloc1d(&rev, N, mem);
    walloc1d(&flow, N, mem);
    (*mem) = (flow + N);
  }
  void walloc(int N, int init_flag, void**mem = &wmem){
    int i;
    walloc1d(&es, N, mem);
    walloc1d(&emem, N, mem);
    walloc1d(&level, N, mem);
    walloc1d(&qq, N, mem);
    walloc1d(&edge, N, mem);
    walloc1d(&rev, N, mem);
    walloc1d(&flow, N, mem);
    (*mem) = (flow + N);
    if(init_flag){
      init(N);
    }
  }
  void levelize(void){
    int i;
    int j;
    int k;
    int t;
    int q_st = 0;
    int q_ed = 1;
    for(i=(0);i<(node);i++){
      level[i] = -1;
    }
    level[st] = 0;
    qq[0] = st;
    while(q_st != q_ed){
      i = qq[q_st++];
      t = level[i] + 1;
      for(j=(0);j<(es[i]);j++){
        if(flow[i][j] > eps){
          k = edge[i][j];
          if(level[k]!=-1){
            continue;
          }
          level[k] = t;
          qq[q_ed++] = k;
          if(k==ed){
            return;
          }
        }
      }
    }
  }
  S pushflow(int i, S lim){
    int j;
    int k;
    int ji;
    S s;
    S t;
    S res = 0;
    if(i==ed){
      return lim;
    }
    for(j=(0);j<(es[i]);j++){
      if(flow[i][j] > eps){
        k = edge[i][j];
        if(level[k] != level[i]+1){
          continue;
        }
        s =min_L(lim, (S)flow[i][j]);
        t = pushflow(k, s);
        if(!t){
          continue;
        }
        res += t;
        lim -= t;
        ji = rev[i][j];
        flow[i][j] -= t;
        flow[k][ji] += t;
        if(!lim){
          break;
        }
      }
    }
    if(lim){
      level[i] = -1;
    }
    return res;
  }
  S solve(int st_, int ed_){
    S res = 0;
    st = st_;
    ed = ed_;
    for(;;){
      levelize();
      if(level[ed] == -1){
        break;
      }
      res += pushflow(st, numeric_limits<S>::max());
    }
    return res;
  }
  void init(int N){
    int i;
    node = N;
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    eps = (T)1e-9;
  }
  void memoryExpand(int i, int sz){
    if(sz <= emem[i]){
      return;
    }
    sz =max_L(sz,max_L(3, emem[i]*2));
    emem[i]=sz;
    edge[i] = (int*)realloc(edge[i], sz*sizeof(int));
    rev[i] = (int*)realloc(rev[i], sz*sizeof(int));
    flow[i] = (T*)realloc(flow[i], sz*sizeof(T));
  }
  void addEdge(int n1, int n2, T f1, T f2 = 0){
    int s1 = es[n1]++;
    int s2 = es[n2]++;
    if(s1 >= emem[n1]){
      memoryExpand(n1, es[n1]);
    }
    if(s2 >= emem[n2]){
      memoryExpand(n2, es[n2]);
    }
    edge[n1][s1]=n2;
    edge[n2][s2]=n1;
    flow[n1][s1]=f1;
    flow[n2][s2]=f2;
    rev[n1][s1]=s2;
    rev[n2][s2]=s1;
  }
  void addEdgeAdv(int n1, int n2, T f1, T f2 = 0){
    int s1 = es[n1]++;
    int s2 = es[n2]++;
    edge[n1][s1]=n2;
    edge[n2][s2]=n1;
    flow[n1][s1]=f1;
    flow[n2][s2]=f2;
    rev[n1][s1]=s2;
    rev[n2][s2]=s1;
  }
  void setGraph(int N, int M, int n1[], int n2[], T f1[], T f2[]){
    int i;
    node = N;
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[n1[i]]++;
      es[n2[i]]++;
    }
    for(i=(0);i<(N);i++){
      memoryExpand(i, es[i]);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      addEdgeAdv(n1[i], n2[i], f1[i], f2[i]);
    }
    eps = (T)1e-9;
  }
  void setGraph_w(int N, int M, int n1[], int n2[], T f1[], T f2[], void **mem = wmem){
    int i;
    int j;
    int k;
    node = N;
    for(i=(0);i<(N);i++){
      es[i] = emem[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[n1[i]]++;
      es[n2[i]]++;
    }
    edge[0] = (int*)(*mem);
    int Oq6TK1Zh = N;
    for(i=(1);i<(Oq6TK1Zh);i++){
      edge[i] = edge[i-1] + es[i-1];
    }
    rev[0] = edge[N-1] + es[N-1];
    int jG1yfsum = N;
    for(i=(1);i<(jG1yfsum);i++){
      rev[i] = rev[i-1] + es[i-1];
    }
    flow[0] = (T*)(rev[N-1] + es[N-1]);
    int n23oAcQn = N;
    for(i=(1);i<(n23oAcQn);i++){
      flow[i] = flow[i-1] + es[i-1];
    }
    *mem = (void*)(flow[N-1] + es[N-1]);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      addEdgeAdv(n1[i], n2[i], f1[i], f2[i]);
    }
    eps = (T)1e-9;
  }
}
;
int N;
int A[100000];
int B[100000];
int main(){
  wmem = memarr;
  int i;
  int j;
  int k;
  int node;
  int st;
  int ed;
  int f;
  maxflow<int,int> flow;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1);
      rd(B[Lj4PdHRW]);B[Lj4PdHRW] += (-1);
    }
  }
  node = 2*N;
  st = node++;
  ed = node++;
  flow.walloc(node,1);
  for(i=(0);i<(N);i++){
    flow.addEdge(st, i, 1);
  }
  for(i=(0);i<(N);i++){
    flow.addEdge(N+i, ed, 1);
  }
  for(i=(0);i<(N);i++){
    flow.addEdge(i, N+A[i], 1);
    flow.addEdge(i, N+B[i], 1);
  }
  f = flow.solve(st,ed);
  if(f==N){
    wt_L("Yes");
    wt_L('\n');
    for(i=(0);i<(N);i++){
      for(j=(0);j<(flow.es[i]);j++){
        k = flow.edge[i][j] - N;
        if(flow.flow[i][j] == 0 && 0 <= k  &&  k < N){
          wt_L(k+1);
          wt_L('\n');
        }
      }
    }
  }
  else{
    wt_L("No");
    wt_L('\n');
  }
  return 0;
}
// cLay version 20210717-1 [beta]

// --- original code ---
// int N, A[1d5], B[];
// {
//   int i, j, k;
//   int node, st, ed, f;
//   maxflow<int,int> flow;
//   rd(N,(A--,B--)(N));
//   node = 2*N;
//   st = node++;
//   ed = node++;
//   flow.walloc(node,1);
//   rep(i,N) flow.addEdge(st, i, 1);
//   rep(i,N) flow.addEdge(N+i, ed, 1);
//   rep(i,N) flow.addEdge(i, N+A[i], 1), flow.addEdge(i, N+B[i], 1);
//   f = flow.solve(st,ed);
//   if(f==N){
//     wt("Yes");
//     rep(i,N) rep(j,flow.es[i]){
//       k = flow.edge[i][j] - N;
//       if(flow.flow[i][j] == 0 && 0 <= k < N) wt(k+1);
//     }
//   } else {
//     wt("No");
//   }
// }
0