結果

問題 No.3275 Minesweeper on Graph
ユーザー tnakao0123
提出日時 2025-09-20 13:02:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,962 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 52,308 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-20 13:02:25
合計ジャッジ時間 4,560 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36 WA * 4
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:78:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   78 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:79:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   79 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~
main.cpp:82:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   82 |     scanf("%d%d", &u, &v);
      |     ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3275.cc:  No.3275 Minesweeper on Graph - yukicoder
 */

#include<cstdio>
#include<algorithm>
#include<numeric>

using namespace std;

/* constant */

const int MAX_N = 16;

/* typedef */

struct Rat {
  int a, b;
  Rat(): a(0), b(1) {}
  Rat(int _a): a(_a), b(1) {}
  Rat(int _a, int _b): a(_a), b(_b) { __normalize(); }

  void __normalize() {
    if (b == 0) a = 0;
    else {
      if (b < 0) a = -a, b = -b;
      int g = gcd(abs(a), b);
      a /= g, b /= g;
    }
  }

  Rat operator+(const Rat &r) const {
    return Rat(a * r.b + r.a * b, b * r.b);
  }
  Rat operator-(const Rat &r) const {
    return Rat(a * r.b - r.a * b, b * r.b);
  }
  Rat operator*(const Rat &r) const { return Rat(a * r.a, b * r.b); }
  Rat operator/(const Rat &r) const { return Rat(a * r.b, b * r.a); }

  bool operator==(const Rat &r) const { return a == r.a && b == r.b; }
  bool operator!=(const Rat &r) const { return a != r.a || b != r.b; }

  void print() const {
    printf("%d", a);
    if (b > 1) printf("/%d", b);
  }
};

/* global variables */

int as[MAX_N], bs[MAX_N];
Rat es[MAX_N][MAX_N], vs[MAX_N];

/* subroutines */

void swaprow(int n, int i, int j) {
  Rat tmp[MAX_N];
  copy(es[i], es[i] + n, tmp);
  copy(es[j], es[j] + n, es[i]);
  copy(tmp, tmp + n, es[j]);
  swap(vs[i], vs[j]);
}

void printmat(int n) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) putchar(' '), es[i][j].print();
    printf(" = "); vs[i].print(); putchar('\n');
  }
  putchar('\n');
}

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", as + i);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    es[u][v] = es[v][u] = 1;
  }

  for (int i = 0; i < n; i++) vs[i] = as[i];
  //printmat(n);

  for (int j = 0, i0 = 0; j < n; j++) {
    if (es[i0][j] == 0) {
      for (int i = i0 + 1; i < n; i++)
	if (es[i][j] != 0) {
	  swaprow(n, i0, i);
	  break;
	}
      if (es[i0][j] == 0) continue;
    }

    if (es[i0][j] != 1) {
      auto e0 = es[i0][j];
      for (int k = j; k < n; k++) es[i0][k] = es[i0][k] / e0;
      vs[i0] = vs[i0] / e0;
    }

    for (int i = i0 + 1; i < n; i++)
      if (es[i][j] != 0) {
	auto e0 = es[i][j];
	for (int k = j; k < n; k++) es[i][k] = es[i][k] - es[i0][k] * e0;
	vs[i] = vs[i] - vs[i0] * e0;
      }

    //printf(" i0=%d\n", i0); printmat(n);
    
    i0++;
  }

  for (int i = n - 1; i >= 0; i--) {
    int j0 = 0;
    while (j0 < n && es[i][j0].a == 0) j0++;
    if (j0 >= n) {
      if (vs[i] != 0) { puts("No"); return 0; }
    }
    else {
      if (vs[i].b != 1 || vs[i].a < 0) { puts("No"); return 0; }
      bs[j0] = vs[i].a;
      
      for (int k = i - 1; k >= 0; k--)
	if (es[k][j0] != 0) {
	  vs[k] = vs[k] - es[k][j0] * vs[i];
	  es[k][j0] = 0;
	}
    }
  }
  //printmat(n);

  puts("Yes");
  for (int i = 0; i < n; i++)
    printf("%d%c", bs[i], (i + 1 < n) ? ' ' : '\n');
  
  return 0;
}
0