結果

問題 No.1420 国勢調査 (Easy)
ユーザー hashiryohashiryo
提出日時 2024-09-02 21:29:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 10,893 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 213,412 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-02 21:29:14
合計ジャッジ時間 7,638 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 32 ms
6,940 KB
testcase_03 AC 32 ms
6,940 KB
testcase_04 AC 32 ms
6,944 KB
testcase_05 AC 31 ms
6,940 KB
testcase_06 AC 31 ms
6,944 KB
testcase_07 AC 31 ms
6,940 KB
testcase_08 AC 31 ms
6,944 KB
testcase_09 AC 31 ms
6,944 KB
testcase_10 AC 31 ms
6,944 KB
testcase_11 AC 31 ms
6,940 KB
testcase_12 AC 7 ms
6,940 KB
testcase_13 AC 12 ms
6,944 KB
testcase_14 AC 7 ms
6,944 KB
testcase_15 AC 12 ms
6,940 KB
testcase_16 AC 12 ms
6,944 KB
testcase_17 AC 13 ms
6,940 KB
testcase_18 AC 12 ms
6,944 KB
testcase_19 AC 12 ms
6,940 KB
testcase_20 AC 12 ms
6,944 KB
testcase_21 AC 13 ms
6,940 KB
testcase_22 AC 44 ms
6,940 KB
testcase_23 AC 43 ms
6,944 KB
testcase_24 AC 44 ms
6,944 KB
testcase_25 AC 44 ms
6,940 KB
testcase_26 AC 43 ms
6,940 KB
testcase_27 AC 35 ms
6,940 KB
testcase_28 AC 35 ms
6,940 KB
testcase_29 AC 35 ms
6,944 KB
testcase_30 AC 36 ms
6,940 KB
testcase_31 AC 36 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// clang-format off
std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,const __int128_t &u){if(!u)os<<"0";__int128_t tmp=u<0?(os<<"-",-u):u;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
std::ostream&operator<<(std::ostream&os,const __uint128_t &u){if(!u)os<<"0";__uint128_t tmp=u;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
#define checkpoint() (void(0))
#define debug(...) (void(0))
#define debugArray(x,n) (void(0))
#define debugMatrix(x,h,w) (void(0))
// clang-format on
class Nimber {
 using u64= unsigned long long;
 using u32= unsigned;
 using u16= unsigned short;
 static inline std::array<u16, 65536> pw, ln;
 template <u16 h= 3> static inline u16 half(u16 A) { return A ? pw[(ln[A] + h) % 65535] : 0; }
 template <u16 h= 0> static inline u16 mul(u16 A, u16 B) { return A && B ? pw[(ln[A] + ln[B] + h) % 65535] : 0; }
 template <u16 h= 0> static inline u16 mul(u16 A, u16 B, u16 C) { return A && B && C ? pw[(ln[A] + ln[B] + ln[C] + h) % 65535] : 0; }
 static inline u16 inv(u16 A) { return assert(A), pw[65535 - ln[A]]; }
 static inline u16 sqrt(u16 A) { return A ? pw[u16((65537 * u32(ln[A])) >> 1)] : 0; }
 static inline u64 mul(u64 A, u64 B) {
  u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48, b0= u16(B), b1= u16(B >> 16), b2= u16(B >> 32), b3= B >> 48, x0= a1 ^ a0, x1= a3 ^ a2, y0= b1 ^ b0, y1= b3 ^ b2, c0= mul(a0, b0), c1= mul(x0, y0) ^ c0, c2= mul<0>(a2 ^ a0, b2 ^ b0), c3= mul<0>(x0 ^ x1, y0 ^ y1) ^ c2 ^ c1;
  return c2^= (c0^= mul<3>(a1, b1)) ^ mul<3>(u16(a3 ^ a1), u16(b3 ^ b1)), c1^= mul<6>(a3, b3) ^ mul<3>(x1, y1), c0^= mul<6>(a2, b2) ^ mul<6>(x1, y1), (u64(c3) << 48) | (u64(c2) << 32) | (u32(c1) << 16) | c0;
 }
 static inline u64 inv(u64 A) {
  u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48, x= a2 ^ a3, y= a1 ^ a3, w= a0 ^ a2, v= a0 ^ a1, b3= mul(a1, a2, a1 ^ x), b2= mul(a0, a2, a0 ^ x), b1= mul(a0, a1, a0 ^ y), b0= mul(a0, v, w), t= mul<3>(w, x, x);
  return b0^= b1 ^ b2, b1^= b3, b2^= b3, b0^= b3^= mul(a0, a0, a3), b1^= t ^ mul<3>(a1, y, y), b0^= t ^ mul<3>(v, y, y), b3^= t= mul<3>(a1, a3, y) ^ mul<3>(a2, x, x), b2^= t ^ mul<3>(a0, a3, a3) ^ mul<3>(a1, a1, a2), b3^= mul<6>(a3, a3, x), b2^= mul<6>(a3, x, x), b1^= mul<6>(a3, a3, y ^ w), b0^= mul<6>(y, x, x), b2^= mul<9>(a3, a3, a3), b0^= mul<9>(a3, a3, y), t= mul<6>(x, b3) ^ mul<6>(a3, b2) ^ mul<3>(a1, b1) ^ mul(a0, b0), t= inv(t), (u64(mul(b3, t)) << 48) | (u64(mul(b2, t)) << 32) | (u32(mul(b1, t)) << 16) | mul(b0, t);
 }
 static inline u64 square(u64 A) {
  u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48;
  return a3= mul(a3, a3), a2= mul(a2, a2), a1= mul(a1, a1), a0= mul(a0, a0), a0^= half(a1) ^ half<6>(a3), a2^= half(a3), a1^= half(a3 ^ a2), (u64(a3) << 48) | (u64(a2) << 32) | (u32(a1) << 16) | a0;
 }
 static inline u64 pow(u64 A, u64 k) {
  for (u64 ret= 1;; A= square(A))
   if (k & 1 ? ret= mul(ret, A) : 0; !(k>>= 1)) return ret;
 }
 template <int mod> static inline int mdif(int a, int b) { return a+= mod & -((a-= b) < 0); }
 template <int mod> static inline int mmul(int a, int b) { return u64(a) * b % mod; }
 static inline int log16(u16 A, u16 B) {
  int a= ln[A], b= ln[B], x= 1;
  if (a == 0) return b == 0 ? 1 : -1;
  for (int q, z, u, y= 0, t= 65535; t;) z= x, u= a, x= y, y= z - y * (q= a / t), a= t, t= u - t * q;
  return b % a ? -1 : u32(b / a) * (x < 0 ? 65535 + x : x) % 65535;
 }
 template <int period, int size> static inline int bsgs(u64 x, u64 y) {
  static constexpr int mask= size - 1;
  std::pair<u64, int> vs[size];
  int os[size + 1]= {};
  u64 so[size], big= 1;
  for (int i= 0; i < size; ++i, big= mul(big, x)) ++os[(so[i]= big) & mask];
  for (int i= 0; i < size; ++i) os[i + 1]+= os[i];
  for (int i= 0; i < size; ++i) vs[--os[so[i] & mask]]= {so[i], i};
  for (int t= 0; t < period; t+= size, y= mul(y, big))
   for (int m= (y & mask), i= os[m], ret; i < os[m + 1]; ++i)
    if (y == vs[i].first) return (ret= vs[i].second - t) < 0 ? ret + period : ret;
  return -1;
 }
 static inline u64 log(u64 A, u64 B) {
  if (B == 1) return 0;
  if (!A && !B) return 1;
  if (!A || !B) return u64(-1);
  static constexpr int P0= 641, P1= 65535, P2= 65537, P3= 6700417, iv10= 40691, iv21= 32768, iv20= 45242, iv32= 3317441, iv31= 3350208, iv30= 3883315;
  int a0= bsgs<P0, 16>(pow(A, 0x663d80ff99c27f), pow(B, 0x663d80ff99c27f));
  if (a0 == -1) return u64(-1);
  int a1= log16(pow(A, 0x1000100010001), pow(B, 0x1000100010001));
  if (a1 == -1) return u64(-1);
  int a2= bsgs<P2, 256>(pow(A, 0xffff0000ffff), pow(B, 0xffff0000ffff));
  if (a2 == -1) return u64(-1);
  int a3= bsgs<P3, 2048>(pow(A, 0x280fffffd7f), pow(B, 0x280fffffd7f));
  if (a3 == -1) return u64(-1);
  int x1= mmul<P1>(mdif<P1>(a1, a0), iv10), x2= mdif<P2>(mmul<P2>(mdif<P2>(a2, a0), iv20), mmul<P2>(x1, iv21)), x3= mdif<P3>(mdif<P3>(mmul<P3>(mdif<P3>(a3, a0), iv30), mmul<P3>(x1, iv31)), mmul<P3>(x2, iv32));
  return u64(P0) * (u64(P1) * (u64(P2) * x3 + x2) + x1) + a0;
 }
 u64 x;
public:
 static inline void init(u32 x= 0, u32 y= 0) {
  constexpr u16 f2n[16]= {0x0001u, 0x2827u, 0x392bu, 0x8000u, 0x20fdu, 0x4d1du, 0xde4au, 0x0a17u, 0x3464u, 0xe3a9u, 0x6d8du, 0x34bcu, 0xa921u, 0xa173u, 0x0ebcu, 0x0e69u};
  for (int i= pw[0]= pw[65535]= 1; i < 65535; ++i) pw[i]= (pw[i - 1] << 1) ^ (0x1681fu & (-(pw[i - 1] >= 0x8000u)));
  for (int i= 1; i < 65535; ln[pw[i]= y]= i, i++)
   for (x= pw[i], y= 0; x; x&= x - 1) y^= f2n[__builtin_ctz(x)];
 }
 Nimber(u64 x_= 0): x(x_) {}
 Nimber &operator+=(const Nimber &r) { return x^= r.x, *this; }
 Nimber &operator-=(const Nimber &r) { return x^= r.x, *this; }
 Nimber &operator*=(const Nimber &r) { return x= mul(x, r.x), *this; }
 Nimber &operator/=(const Nimber &r) { return x= mul(x, inv(r.x)), *this; }
 Nimber operator+(const Nimber &r) const { return Nimber(x ^ r.x); }
 Nimber operator-(const Nimber &r) const { return Nimber(x ^ r.x); }
 Nimber operator*(const Nimber &r) const { return Nimber(mul(x, r.x)); }
 Nimber operator/(const Nimber &r) const { return Nimber(mul(x, inv(r.x))); }
 Nimber operator-() const { return *this; }
 Nimber inv() const { return Nimber(inv(x)); }
 Nimber square() const { return Nimber(square(x)); }
 Nimber sqrt() const {
  u16 a0= u16(x), a1= u16(x >> 16), a2= u16(x >> 32), a3= x >> 48;
  return a1^= half(a3 ^ a2), a2^= half(a3), a0^= half(a1) ^ half<6>(a3), Nimber((u64(sqrt(a3)) << 48) | (u64(sqrt(a2)) << 32) | (u32(sqrt(a1)) << 16) | sqrt(a0));
 }
 u64 val() const { return x; }
 Nimber pow(u64 k) const { return Nimber(pow(x, k)); }
 u64 log(const Nimber &r) const { return log(x, r.x); }
 bool operator==(const Nimber &r) const { return x == r.x; }
 bool operator!=(const Nimber &r) const { return x != r.x; }
 bool operator<(const Nimber &r) const { return x < r.x; }
 bool operator>(const Nimber &r) const { return x > r.x; }
 bool operator<=(const Nimber &r) const { return x <= r.x; }
 bool operator>=(const Nimber &r) const { return x >= r.x; }
 friend std::ostream &operator<<(std::ostream &os, const Nimber &r) { return os << r.x; }
 friend std::istream &operator>>(std::istream &is, Nimber &r) { return is >> r.x, is; }
};
template <bool undoable= false, class weight_t= void> class UnionFind {
 std::vector<int> par;
 std::vector<weight_t> val;
public:
 UnionFind(int n): par(n, -1), val(n) {}
 int leader(int u) {
  if (par[u] < 0) return u;
  int r= leader(par[u]);
  return val[u]+= val[par[u]], par[u]= r;
 }
 //  p(v) - p(u) = w
 bool unite(int u, int v, weight_t w) {
  int u_= leader(u), v_= leader(v);
  if (w+= val[u] - val[v]; u_ == v_) return w == weight_t();
  if (par[u_] > par[v_]) std::swap(u_, v_), w= -w;
  return par[u_]+= par[v_], par[v_]= u_, val[v_]= w, true;
 }
 bool connected(int u, int v) { return leader(u) == leader(v); }
 int size(int u) { return -par[leader(u)]; }
 weight_t potential(int u) { return leader(u), val[u]; }
 //  p(v) - p(u)
 weight_t diff(int u, int v) { return potential(v) - potential(u); }
};
template <> class UnionFind<false, void> {
 std::vector<int> par;
public:
 UnionFind(int n): par(n, -1) {}
 int leader(int u) { return par[u] < 0 ? u : par[u]= leader(par[u]); }
 bool unite(int u, int v) {
  if ((u= leader(u)) == (v= leader(v))) return false;
  if (par[u] > par[v]) std::swap(u, v);
  return par[u]+= par[v], par[v]= u, true;
 }
 bool connected(int u, int v) { return leader(u) == leader(v); }
 int size(int u) { return -par[leader(u)]; }
};
template <class T> class UnionFind<true, T> {
 std::vector<int> par;
 std::vector<T> val;
 std::vector<std::tuple<int, int, T>> his;
public:
 UnionFind(int n): par(n, -1), val(n) {}
 int leader(int u) const { return par[u] < 0 ? u : leader(par[u]); }
 //  p(v) - p(u) = w
 bool unite(int u, int v, T w) {
  int u_= leader(u), v_= leader(v);
  if (w+= val[u] - val[v]; u_ == v_) return w == T();
  if (par[u_] > par[v_]) std::swap(u_, v_), w= -w;
  return his.emplace_back(v_, par[v_], val[v_]), par[u_]+= par[v_], par[v_]= u_, val[v_]= w, true;
 }
 bool connected(int u, int v) const { return leader(u) == leader(v); }
 int size(int u) const { return -par[leader(u)]; }
 T potential(int u) { return par[u] < 0 ? val[u] : val[u] + potential(par[u]); }
 //  p(v) - p(u)
 T diff(int u, int v) { return potential(v) - potential(u); }
 int time() const { return his.size(); }
 void undo() {
  if (his.empty()) return;
  auto [u, s, v]= his.back();
  his.pop_back(), par[par[u]]-= s, par[u]= s, val[u]= v;
 }
 void rollback(int t) {
  for (assert(t <= time()); time() > t;) undo();
 }
};
template <> class UnionFind<true, void> {
 std::vector<int> par;
 std::vector<std::pair<int, int>> his;
public:
 UnionFind(int n): par(n, -1) {}
 int leader(int u) const { return par[u] < 0 ? u : leader(par[u]); }
 bool unite(int u, int v) {
  if ((u= leader(u)) == (v= leader(v))) return false;
  if (par[u] > par[v]) std::swap(u, v);
  return his.emplace_back(v, par[v]), par[u]+= par[v], par[v]= u, true;
 }
 bool connected(int u, int v) const { return leader(u) == leader(v); }
 int size(int u) const { return -par[leader(u)]; }
 int time() const { return his.size(); }
 void undo() {
  if (his.empty()) return;
  auto [u, s]= his.back();
  his.pop_back(), par[par[u]]-= s, par[u]= s;
 }
 void rollback(int t) {
  for (assert(t <= time()); time() > t;) undo();
 }
};
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(false);
 Nimber::init();
 int N, M;
 cin >> N >> M;
 UnionFind<false, Nimber> uf(N);
 bool isok= true;
 for (int i= 0; i < M; ++i) {
  int A, B;
  Nimber Y;
  cin >> A >> B >> Y;
  --A, --B;
  isok&= uf.unite(A, B, Y);
 }
 if (isok)
  for (int i= 0; i < N; ++i) cout << uf.potential(i) << '\n';
 else cout << -1 << '\n';
 return 0;
}
0