結果

問題 No.1668 Grayscale
ユーザー monnumonnu
提出日時 2021-09-04 20:38:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,972 bytes
コンパイル時間 4,402 ms
コンパイル使用メモリ 233,572 KB
実行使用メモリ 73,512 KB
最終ジャッジ日時 2023-08-23 11:59:30
合計ジャッジ時間 8,869 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 428 ms
73,384 KB
testcase_08 AC 172 ms
46,504 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
4,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll=long long;
using Graph=vector<vector<int>>;
#define MAX 60
#define MOD 1000000007
#define INF 1000000000000000000

vector<int> parent;
int find(int x){
  int y=parent[x];
  if(y!=parent[y]){
    y=find(y);
  }
  return parent[x]=y;
}
void unite(int a,int b){
  int x=find(a);
  int y=find(b);
  if(x!=y){
    parent[y]=x;
  }
}

int main(){
  int H,W,N;
  cin>>H>>W>>N;
  vector<vector<int>> C(H,vector<int>(W));
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin>>C[i][j];
    }
  }

  parent.resize(H*W);
  iota(parent.begin(),parent.end(),0);
  for(int i=0;i<H;i++){
    for(int j=0;j<W-1;j++){
      if(C[i][j]==C[i][j+1]){
        unite(i*W+j,i*W+(j+1));
      }
    }
  }
  for(int i=0;i<H-1;i++){
    for(int j=0;j<W;j++){
      if(C[i][j]==C[i+1][j]){
        unite(i*W+j,(i+1)*W+j);
      }
    }
  }

  Graph G(H*W);
  vector<int> cnt(H*W,0);
  for(int i=0;i<H;i++){
    for(int j=0;j<W-1;j++){
      if(C[i][j]<C[i][j+1]){
        G[find(i*W+j)].push_back(find(i*W+(j+1)));
        cnt[find(i*W+(j+1))]++;
      }else if(C[i][j]>C[i][j+1]){
        G[find(i*W+(j+1))].push_back(find(i*W+j));
        cnt[find(i*W+j)]++;
      }
    }
  }
  for(int i=0;i<H-1;i++){
    for(int j=0;j<W;j++){
      if(C[i][j]<C[i+1][j]){
        G[find(i*W+j)].push_back(find((i+1)*W+j));
        cnt[find((i+1)*W+j)]++;
      }else if(C[i][j]>C[i+1][j]){
        G[find((i+1)*W+j)].push_back(find(i*W+j));
        cnt[find(i*W+j)]++;
      }
    }
  }

  vector<int> length(H*W,0);
  queue<int> q;
  for(int i=0;i<H*W;i++){
    if(cnt[i]==0){
      q.push(i);
    }
  }

  while(!q.empty()){
    int v=q.front();
    q.pop();
    for(auto nv:G[v]){
      cnt[nv]--;
      length[nv]=max(length[nv],length[v]+1);
      if(cnt[nv]==0){
        q.push(nv);
      }
    }
  }

  int ans=1;
  for(int i=0;i<H*W;i++){
    ans=max(ans,length[i]+1);
  }
  cout<<ans<<'\n';
}
0