結果

問題 No.3580 二成分の和
コンテスト
ユーザー Kude
提出日時 2026-07-03 22:04:59
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,212 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,545 ms
コンパイル使用メモリ 362,200 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2026-07-03 22:05:05
合計ジャッジ時間 3,929 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, b;
  cin >> n >> m >> b;
  vector<vector<P>> to(n);
  rep(_, m) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    to[u].emplace_back(v, c);
    to[v].emplace_back(u, c);
  }
  vector<char> visited(n);
  bool ok = true;
  VI ans(n);
  VI side(n);
  rep(i, n) if (!visited[i]) {
    VI vs[2];
    bool added = false;
    auto dfs = [&](auto&& self, int u) -> void {
      vs[side[u]].emplace_back(u);
      visited[u] = true;
      for (auto [v, c] : to[u]) {
        if (visited[v]) {
          if (side[v] != side[u] || added) {
            if ((ans[u] + ans[v]) % b != c) ok = false;
          } else {
            added = true;
            int d = (ans[u] + ans[v] + b - c) % b;
            if (!side[u]) d = (b - d) % b;
            // 2x = d mod b
            if (d % 2 == 0) d /= 2;
            else if (b % 2) d = (b + 1) / 2 * (ll)d % b;
            else {
              ok = false;
              continue;
            }
            for (int& w : vs[0]) ans[w] = (ans[w] + d) % b;
            for (int& w : vs[1]) ans[w] = (ans[w] + b - d) % b;
            assert((ans[u] + ans[v]) % b == c);
          }
        } else {
          ans[v] = (c + b - ans[u]) % b;
          side[v] = side[u] ^ 1;
          self(self, v);
        }
      }
    };
    dfs(dfs, i);
  }
  if (!ok) {
    cout << "No\n";
  } else {
    cout << "Yes\n";
    rep(i, n) cout << ans[i] << " \n"[i + 1 == n];
  }
}
0