結果
| 問題 | No.3863 Almost Bipartite Matching |
| コンテスト | |
| ユーザー |
👑 Nachia
|
| 提出日時 | 2026-12-05 00:27:57 |
| 結果 |
AC
不安定
|
| 実行時間 | 271 ms / 2,000 ms |
| + 680µs | |
| コード長 | 11,914 bytes |
| 記録 | |
| コンパイル時間 | 969 ms |
| コンパイル使用メモリ | 143,948 KB |
| 実行使用メモリ | 6,400 KB |
| 最終ジャッジ日時 | 2026-12-06 02:10:46 |
| 合計ジャッジ時間 | 28,038 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 59 |
ソースコード
#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
// disable assert
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template <class T> using V = vector<T>;
template <class A, class B> void chmax(A& l, const B& r){ if(l < r) l = r; }
template <class A, class B> void chmin(A& l, const B& r){ if(r < l) l = r; }
#include <utility>
#include <cassert>
namespace nachia{
// ax + by = gcd(a,b)
// return ( x, - )
std::pair<long long, long long> ExtGcd(long long a, long long b){
long long x = 1, y = 0;
while(b){
long long u = a / b;
std::swap(a-=b*u, b);
std::swap(x-=y*u, y);
}
return std::make_pair(x, a);
}
} // namespace nachia
namespace nachia{
template<unsigned int MOD>
struct StaticModint{
private:
using u64 = unsigned long long;
unsigned int x;
public:
using my_type = StaticModint;
template< class Elem >
static Elem safe_mod(Elem x){
if(x < 0){
if(0 <= x+MOD) return x + MOD;
return MOD - ((-(x+MOD)-1) % MOD + 1);
}
return x % MOD;
}
StaticModint() : x(0){}
StaticModint(const my_type& a) : x(a.x){}
StaticModint& operator=(const my_type&) = default;
template< class Elem >
StaticModint(Elem v) : x(safe_mod(v)){}
unsigned int operator*() const { return x; }
my_type& operator+=(const my_type& r) { auto t = x + r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
my_type operator+(const my_type& r) const { my_type res = *this; return res += r; }
my_type& operator-=(const my_type& r) { auto t = x + MOD - r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
my_type operator-(const my_type& r) const { my_type res = *this; return res -= r; }
my_type operator-() const noexcept { my_type res = *this; res.x = ((res.x == 0) ? 0 : (MOD - res.x)); return res; }
my_type& operator*=(const my_type& r){ x = (u64)x * r.x % MOD; return *this; }
my_type operator*(const my_type& r) const { my_type res = *this; return res *= r; }
bool operator==(const my_type& r) const { return x == r.x; }
my_type pow(unsigned long long i) const {
my_type a = *this, res = 1;
while(i){ if(i & 1){ res *= a; } a *= a; i >>= 1; }
return res;
}
my_type inv() const { return my_type(ExtGcd(x, MOD).first); }
unsigned int val() const { return x; }
int hval() const { return int(x > MOD/2 ? x - MOD : x); }
static constexpr unsigned int mod() { return MOD; }
static my_type raw(unsigned int val) { auto res = my_type(); res.x = val; return res; }
my_type& operator/=(const my_type& r){ return operator*=(r.inv()); }
my_type operator/(const my_type& r) const { return operator*(r.inv()); }
};
} // namespace nachia
using Mint = nachia::StaticModint<998244353>;
#include <ostream>
namespace nachia{
template<class Elem>
struct MatrixModulo{
private:
int h;
int w;
std::vector<Elem> elems;
public:
MatrixModulo(int new_h=0, int new_w=0)
: h(new_h), w(new_w), elems(h*w, Elem(0)){}
MatrixModulo(const MatrixModulo &) = default;
int numRow() const { return h; }
int numColumn() const { return w; }
int height() const { return numRow(); }
int width() const { return numColumn(); }
typename std::vector<Elem>::iterator operator[](int y){ return elems.begin() + (y*w); }
typename std::vector<Elem>::const_iterator operator[](int y) const { return elems.begin() + (y*w); }
static MatrixModulo Identity(int n){
auto res = MatrixModulo(n,n);
for(int i=0; i<n; i++) res[i][i]=Elem(1);
return res;
}
MatrixModulo transposed() const {
auto res = MatrixModulo(w,h);
for(int i=0; i<h; i++) for(int j=0; j<w; j++) res[j][i] = elems[i*w+j];
return res;
}
void swapColumns(int x1, int x2){
assert(0 <= x1 && x1 < numColumn());
assert(0 <= x2 && x2 < numColumn());
for(int y=0; y<numRow(); y++) std::swap((*this)[y][x1], (*this)[y][x2]);
}
void swapRows(int y1, int y2){
assert(0 <= y1 && y1 < numRow());
assert(0 <= y2 && y2 < numRow());
for(int x=0; x<numColumn(); x++) std::swap((*this)[y1][x], (*this)[y2][x]);
}
MatrixModulo operator*(const MatrixModulo& r) const {
assert(width() == r.height());
auto res = MatrixModulo(h, r.w);
for(int i=0; i<h; i++) for(int j=0; j<w; j++) for(int k=0; k<r.w; k++){
res[i][k] += (*this)[i][j] * r[j][k];
}
return res;
}
std::vector<Elem> operator*(const std::vector<Elem>& r) const {
assert(width() == int(r.size()));
auto res = std::vector<Elem>(h);
for(int i=0; i<h; i++) for(int j=0; j<w; j++){
res[i] += (*this)[i][j] * r[j];
}
return res;
}
MatrixModulo& operator*=(const Elem& r){
int z = (int)elems.size();
for(int i=0; i<z; i++) elems[i] *= r;
return *this;
}
Elem det() const {
assert(height() == width());
MatrixModulo g = *this;
Elem ans = 1;
for(int i=0; i<h; i++){
int tg = -1;
for(int j=i; j<h; j++){ if(g[j][i].val() != 0) tg = j; }
if(tg == -1) return 0;
if(tg != i) ans = -ans;
for(int j=0; j<h; j++) std::swap(g[i][j], g[tg][j]);
tg = i;
ans *= g[i][i];
Elem const_coeff = g[i][i].inv();
for(int j=0; j<h; j++) g[i][j] *= const_coeff;
for(int j=i+1; j<h; j++) for(int k=h-1; k>=i; k--) g[j][k] -= g[j][i] * g[i][k];
}
return ans;
}
int rank() const {
if(height() == 0 || width() == 0) return 0;
MatrixModulo g = *this;
int y = 0;
for(int d=0; d<w; d++){
if(y == h) break;
int tg = -1;
for(int i=y; i<h; i++){ if(g[i][d].val() != 0){ tg = i; break; } }
if(tg == -1) continue;
for(int j=d; j<w; j++) std::swap(g[y][j], g[tg][j]);
tg = y;
Elem const_coeff = g[y][d].inv();
for(int j=d; j<w; j++) g[y][j] *= const_coeff;
for(int i=y+1; i<h; i++) for(int j=w-1; j>=d; j--) g[i][j] -= g[i][d] * g[y][j];
y++;
}
return y;
}
MatrixModulo pow(unsigned long long i){
auto a = *this;
auto res = Identity(height());
while(i){
if(i%2) res = res * a;
a = a * a; i /= 2;
}
return res;
}
friend std::ostream& operator<<(std::ostream& os, const MatrixModulo& a){
int n = a.height(), m = a.width();
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(j) os << " ";
os << a[i][j].val();
} os << "\n";
}
return os;
}
};
} // namespace nachia
using Mat = nachia::MatrixModulo<Mint>;
#include <unordered_map>
#include <cstdint>
#include <array>
namespace nachia{
int Popcount(unsigned long long c){
#ifdef __GNUC__
return __builtin_popcountll(c);
#else
c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3));
c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5));
c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17));
c = (c * (~0ull/257)) >> 56;
return c;
#endif
}
// please ensure x != 0
int MsbIndex(unsigned long long x){
assert(x != 0ull);
#ifdef __GNUC__
return 63 - __builtin_clzll(x);
#else
using u64 = unsigned long long;
int q = (x >> 32) ? 32 : 0;
auto m = x >> q;
constexpr u64 hi = 0x88888888;
constexpr u64 mi = 0x11111111;
m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 35;
m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 31;
q += (m & 0xf) << 2;
q += 0x3333333322221100 >> (((x >> q) & 0xf) << 2) & 0xf;
return q;
#endif
}
// please ensure x != 0
int LsbIndex(unsigned long long x){
assert(x != 0ull);
#ifdef __GNUC__
return __builtin_ctzll(x);
#else
return MsbIndex(x & -x);
#endif
}
}
namespace nachia{
class Xoshiro256pp{
public:
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
private:
std::array<u64, 4> s;
// https://prng.di.unimi.it/xoshiro256plusplus.c
static inline uint64_t rotl(const uint64_t x, int k) noexcept {
return (x << k) | (x >> (64 - k));
}
inline uint64_t gen(void) noexcept {
const uint64_t result = rotl(s[0] + s[3], 23) + s[0];
const uint64_t t = s[1] << 17;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
s[3] = rotl(s[3], 45);
return result;
}
// https://xoshiro.di.unimi.it/splitmix64.c
u64 splitmix64(u64& x) {
u64 z = (x += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
// generate x : 0 <= x <= r
u64 random_unsigned_from_zero(u64 r){
if(!r) return 0;
u64 mask = 1ull << MsbIndex(r);
mask += mask - 1;
while(true){
auto res = rng64() & mask;
if(res <= r) return res;
}
}
public:
void seed(u64 x = 7001){
assert(x != 0);
s[0] = x;
for(int i=1; i<4; i++) s[i] = splitmix64(x);
}
std::array<u64, 4> getState() const { return s; }
void setState(std::array<u64, 4> a){ s = a; }
Xoshiro256pp(){ seed(); }
u64 rng64() { return gen(); }
u64 operator()(){ return gen(); }
// generate x : l <= x <= r
u64 random_unsigned(u64 l,u64 r){
assert(l<=r);
return l + random_unsigned_from_zero(r - l);
}
// generate x : l <= x <= r
i64 random_signed(i64 l,i64 r){
assert(l<=r);
return (i64)(random_unsigned_from_zero((u64)r - (u64)l) + (u64)l);
}
// permute x : n_left <= x <= n_right
// output r from the front
template<class Int>
std::vector<Int> random_nPr(Int n_left, Int n_right, Int r){
Int n = n_right-n_left;
assert(n>=0);
assert(r<=(1ll<<27));
if(r==0) return {};
assert(n>=r-1);
std::vector<Int> V;
std::unordered_map<Int,Int> G;
for(int i=0; i<r; i++){
Int p = random_signed(i,n);
Int x = p - G[p];
V.push_back(x);
G[p] = p - (i - G[i]);
}
for(Int& v : V) v+=n_left;
return V;
}
// shuffle using swaps
template<class E>
void shuffle_inplace(std::vector<E>& V){
size_t N = V.size();
for(size_t i=1; i<N; i++){
std::swap(V[i], V[random_unsigned(0, i)]);
}
}
template<class E>
std::vector<E> shuffled(const std::vector<E>& V){
auto cp = V;
shuffle_inplace(cp);
return cp;
}
};
} // namespace nachia
#include <random>
#include <chrono>
nachia::Xoshiro256pp rng;
namespace RngInitInstance { struct RngInit { RngInit(){
unsigned long long seed1 = std::random_device()();
unsigned long long seed2 = std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto s = seed1 ^ seed2;
#ifdef NACHIA
std::cerr << "seed = " << s << std::endl;
#endif
rng.seed(s);
} } a; }
void testcase(){
ll L, R; cin >> L >> R;
Mat A(R, R);
ll x[3], y[3];
Mint xw[3], yw[3];
REP(li,L){
ll k0, k1;
cin >> k0;
REP(i,k0){ cin >> x[i]; x[i]--; xw[i] = rng(); }
cin >> k1;
REP(i,k1){ cin >> y[i]; y[i]--; yw[i] = rng(); }
REP(i,k0) REP(j,k1) A[x[i]][y[j]] += xw[i] * yw[j];
REP(i,k0) REP(j,k1) A[y[j]][x[i]] += -xw[i] * yw[j];
}
cout << (L + (A.rank() + 1) / 2) << "\n";
}
int main(){
cin.tie(0)->sync_with_stdio(0);
testcase();
return 0;
}
Nachia