제출 #1094333

#제출 시각아이디문제언어결과실행 시간메모리
1094333MattNattFeczanTracks in the Snow (BOI13_tracks)C++14
78.13 / 100
1920 ms1048576 KiB
#include<bits/stdc++.h>
#include <bitset>
using namespace std;

const int N = 4000;
string S[2];
vector<pair<int,int>>Graf1[N*N]; //czy rozne kolory, wierzcholek
int idx[N*N], c=1;
bitset<N*N> c1, c2;
vector<int>Graf[N*N];

void DFS1(int n, int v){
  c1[n] = c, idx[n] = v;
  for(auto& elm : Graf1[n]){
    if(c1[elm.second] == 0 && elm.first == 0){
      DFS1(elm.second, v);
    }
  }
}

int makeTree(int n, int m){
  int v = 0, d = 0;
  for(int i=0;i<n*m;i++){
    if(c1[i] == 0)
      DFS1(i, v++);
  }
  for(int i=0;i<n*m;i++){
    for(auto& elm : Graf1[i]){
      if(idx[elm.second] != idx[i])
        Graf[idx[i]].push_back(idx[elm.second]);
    }
    Graf1[i].clear();
  }
  queue<pair<int,int>>Q;
  Q.push({0, 1});
  while(!Q.empty()){
    auto now = Q.front();
    Q.pop();
    if(c2[now.first] == c)
      continue;
    c2[now.first] = c, d = max(d, now.second);
    for(auto& elm : Graf[now.first]){
      if(c2[elm] != c){
        Q.push({elm, now.second+1});
      }
    }
  }
  return d;
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n,m;
  cin>>n>>m;
  for(int i=0;i<n;i++){
    S[0] = S[1];
    cin>>S[1];
    for(int j=0;j<m;j++){
      if(S[1][j] == '.')
        continue;
      if(i != 0){
        if(S[0][j] == S[1][j]){
          Graf1[i*m+j].push_back({0, (i-1)*m+j}), Graf1[(i-1)*m+j].push_back({0, i*m+j});
        }
        else if(S[0][j] != '.'){
          Graf1[i*m+j].push_back({1, (i-1)*m+j}), Graf1[(i-1)*m+j].push_back({1, i*m+j});
        }
      }
      if(j != 0){
        if(S[1][j-1] == S[1][j]){
          Graf1[i*m+j-1].push_back({0, i*m+j}), Graf1[i*m+j].push_back({0, i*m+j-1});
        }
        else if(S[1][j-1] != '.'){
          Graf1[i*m+j-1].push_back({1, i*m+j}), Graf1[i*m+j].push_back({1, i*m+j-1});
        }
      }
    }
  }
  cout<<makeTree(n, m)<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...