Problem p00749 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct Peace {
  int minx;
  int maxx;
  double weightPlace;
  set<int> link;
};

// ツピツーツスツづ個湘ョツ陛ア
int peaceCnt = 0;
Peace G[200];
int w, h;

vector<double> blockList;

bool passed[100][100];
char field[100][100];
int place[100][100];

int minx;
int maxx;

bool no = false;
int rootNode;

const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, 1, -1, 0};

void dfs(int x, int y, char num) {
  place[y][x] = peaceCnt;
  passed[y][x] = true;
  blockList.push_back(x + 0.5);

  // ツ地ツ姪環づ可づつつ「ツづつ「ツつスツづァツδ仰ーツトツノツーツド
  if (y == h - 1) {
    rootNode = peaceCnt;
  }

  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (ny >= 0 && nx >= 0 && ny < h && nx < w && !passed[ny][nx] &&
        field[ny][nx] == num) {
      dfs(nx, ny, num);
    }
  }
}

// ツ渉ーツづーツ仰づ淞、ツδ環δ督クツづーツ催ャツづゥ
void dfs2(int x, int y, int num) {
  passed[y][x] = true;
  // minx=min(minx,x);
  // maxx=max(maxx,x+1);

  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (ny >= 0 && nx >= 0 && ny < h && nx < w && !passed[ny][nx] &&
        place[ny][nx] == num) {
      dfs2(nx, ny, num);
    } else if (ny < h && ny >= 0 && nx >= 0 && nx < w &&
               ((dy[i] == 1 && place[ny][nx] != num && place[ny][nx] != -1))) {
      // ツ可コツづ可暗。ツつ、ツ氾板債づ個ノツーツド
      G[place[ny][nx]].link.insert(num);
      minx = min(minx, nx);
      maxx = max(maxx, nx + 1);
    }
    // ツ地ツ姪環づ可静堋つオツづつ「ツつスツづァ
    if (ny == h) {
      minx = min(minx, nx);
      maxx = max(maxx, nx + 1);
    }
  }
}

// ツ姪淞づィツ値ツづ債重ツ心ツづ個暗環置ツづブツδ債ッツクツづ個重ツつウ
pair<double, int> calc(int nodeNum) {
  if (no)
    return make_pair(0.0, 0);
  double weip = G[nodeNum].weightPlace;
  int cnt = 0;
  int wei = 4;
  for (set<int>::iterator it = G[nodeNum].link.begin();
       it != G[nodeNum].link.end(); it++) {
    int v = *it;
    pair<double, int> tmp = calc(v);

    weip = (tmp.second * tmp.first + weip * wei) / (wei + tmp.second);
    wei = wei + tmp.second;
  }
  // ツ重ツ心ツ計ツ算
  if (weip > G[nodeNum].minx && weip < G[nodeNum].maxx) {
    return make_pair(weip, wei);
  } else {
    no = true;
    return make_pair(0, 0.0);
  }
}

int main() {

  while (cin >> w >> h && !(w == 0 && h == 0)) {
    no = false;
    peaceCnt = 0;

    for (int i = 0; i < h; i++) {
      fill(passed[i], passed[i] + w, false);
    }
    for (int i = 0; i < 100; i++) {
      fill(place[i], place[i] + w, -1);
    }
    for (int i = 0; i < 200; i++) {
      G[i].link.clear();
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
      }
    }
    // ツブツδ債ッツクツづーツづ慊づづ淞、ツ重ツ心ツづーツ仰づ淞づゥ
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != '.' && !passed[i][j]) {
          dfs(j, i, field[i][j]);
          double wa = 0;
          for (int k = 0; k < blockList.size(); k++) {
            wa += blockList[k];
          }
          G[peaceCnt].weightPlace = wa / blockList.size();
          blockList.clear();
          peaceCnt++;
        }
      }
    }
    for (int i = 0; i < h; i++)
      fill(passed[i], passed[i] + w, false);
    // ツδ環δ督クツづーツづつづ按つーツづゥ
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        minx = 100000000;
        maxx = -1;
        if (field[i][j] != '.' && !passed[i][j]) {
          dfs2(j, i, place[i][j]);
          G[place[i][j]].maxx = maxx;
          G[place[i][j]].minx = minx;
        }
      }
    }
    // ツ催帰ツ的ツづ可重ツ心ツづーツ計ツ算
    calc(rootNode);
    if (no)
      cout << "UNSTABLE" << endl;
    else
      cout << "STABLE" << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct Peace {
  int minx;
  int maxx;
  double weightPlace;
  set<int> link;
};

// ツピツーツスツづ個湘ョツ陛ア
int peaceCnt = 0;
Peace G[200];
int w, h;

vector<double> blockList;

bool passed[100][100];
char field[100][100];
int place[100][100];

int minx;
int maxx;

bool no = false;
int rootNode;

const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, 1, -1, 0};

void dfs(int x, int y, char num) {
  place[y][x] = peaceCnt;
  passed[y][x] = true;
  blockList.push_back(x + 0.5);

  // ツ地ツ姪環づ可づつつ「ツづつ「ツつスツづァツδ仰ーツトツノツーツド
  if (y == h - 1) {
    rootNode = peaceCnt;
  }

  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (ny >= 0 && nx >= 0 && ny < h && nx < w && !passed[ny][nx] &&
        field[ny][nx] == num) {
      dfs(nx, ny, num);
    }
  }
}

// ツ渉ーツづーツ仰づ淞、ツδ環δ督クツづーツ催ャツづゥ
void dfs2(int x, int y, int num) {
  passed[y][x] = true;
  // minx=min(minx,x);
  // maxx=max(maxx,x+1);

  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (ny >= 0 && nx >= 0 && ny < h && nx < w && !passed[ny][nx] &&
        place[ny][nx] == num) {
      dfs2(nx, ny, num);
    } else if (ny >= 0 && nx >= 0 && nx < w &&
               ((dy[i] == 1 && place[ny][nx] != num && place[ny][nx] != -1))) {
      // ツ可コツづ可暗。ツつ、ツ氾板債づ個ノツーツド
      G[place[ny][nx]].link.insert(num);
      minx = min(minx, nx);
      maxx = max(maxx, nx + 1);
    }
    // ツ地ツ姪環づ可静堋つオツづつ「ツつスツづァ
    if (ny == h) {
      minx = min(minx, nx);
      maxx = max(maxx, nx + 1);
    }
  }
}

// ツ姪淞づィツ値ツづ債重ツ心ツづ個暗環置ツづブツδ債ッツクツづ個重ツつウ
pair<double, int> calc(int nodeNum) {
  if (no)
    return make_pair(0.0, 0);
  double weip = G[nodeNum].weightPlace;
  int cnt = 0;
  int wei = 4;
  for (set<int>::iterator it = G[nodeNum].link.begin();
       it != G[nodeNum].link.end(); it++) {
    int v = *it;
    pair<double, int> tmp = calc(v);

    weip = (tmp.second * tmp.first + weip * wei) / (wei + tmp.second);
    wei = wei + tmp.second;
  }
  // ツ重ツ心ツ計ツ算
  if (weip > G[nodeNum].minx && weip < G[nodeNum].maxx) {
    return make_pair(weip, wei);
  } else {
    no = true;
    return make_pair(0, 0.0);
  }
}

int main() {

  while (cin >> w >> h && !(w == 0 && h == 0)) {
    no = false;
    peaceCnt = 0;
    for (int i = 0; i < h; i++) {
      fill(passed[i], passed[i] + w, false);
    }
    for (int i = 0; i < 100; i++) {
      fill(place[i], place[i] + w, -1);
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
      }
    }

    // ツブツδ債ッツクツづーツづ慊づづ淞、ツ重ツ心ツづーツ仰づ淞づゥ
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != '.' && !passed[i][j]) {
          dfs(j, i, field[i][j]);
          double wa = 0;
          for (int k = 0; k < blockList.size(); k++) {
            wa += blockList[k];
          }
          G[peaceCnt].weightPlace = wa / blockList.size();
          blockList.clear();
          peaceCnt++;
        }
      }
    }
    for (int i = 0; i < h; i++)
      fill(passed[i], passed[i] + w, false);
    // ツδ環δ督クツづーツづつづ按つーツづゥ
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        minx = 100000000;
        maxx = -1;
        if (field[i][j] != '.' && !passed[i][j]) {
          dfs2(j, i, place[i][j]);
          G[place[i][j]].maxx = maxx;
          G[place[i][j]].minx = minx;
        }
      }
    }
    // ツ催帰ツ的ツづ可重ツ心ツづーツ計ツ算
    calc(rootNode);
    if (no)
      cout << "UNSTABLE" << endl;
    else
      cout << "STABLE" << endl;
  }

  return 0;
}