結果

問題 No.2998 Rainbow Christmas Tree
ユーザー ecottea
提出日時 2024-12-23 03:20:44
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 18,782 bytes
コンパイル時間 29,474 ms
コンパイル使用メモリ 364,584 KB
最終ジャッジ日時 2025-02-26 16:13:08
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

// QCFium
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#ifndef HIDDEN_IN_VS //
//
#define _CRT_SECURE_NO_WARNINGS
//
#include <bits/stdc++.h>
using namespace std;
//
using ll = long long; using ull = unsigned long long; // -2^63 2^63 = 9e18int -2^31 2^31 = 2e9
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
//
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
//
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
//
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 n-1
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s t
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s t
#define repe(v, a) for(const auto& v : (a)) // a
#define repea(v, a) for(auto& v : (a)) // a
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} //
#define EXIT(a) {cout << (a) << endl; exit(0);} //
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) //
//
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // true
    
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // true
    
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // mod
//
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif //
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
using mint = modint998244353;
//using mint = static_modint<1000000007>;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // Visual Studio
#include "local.hpp"
#else // gcc
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(...)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE MLE
#endif
//Link-Cut Tree
/*
* Link_cut_tree<S, op, o>(int n) : O(n)
* o() n
* (S, op, o)
*
* Link_cut_tree<S, op, o>(vS a) : O(n log n)
* a[0..n) n
*
* cut(int i) : O(log n)
* i
* i
*
* cut(int s, int t) : O(log n)
* s-t
*
* link(int s, int t) : O(log n)
* s t s→t
* t
*
* set_root(int rt) : O(log n)
* rt
*
* S sum(int i) : O(log n)
* i
*
* set(int i, S x) : O(log n)
* i x
*
* add(int i, S x) : O(log n)
* i x
*
* bool connectedQ(int s, int t) : O(log n)
* s, t
*/
template <class S, S(*op)(S, S), S(*o)()>
class Link_cut_tree {
// : https://www.slideshare.net/iwiwi/2-12188845
// : https://ei1333.github.io/library/structure/lct/link-cut-tree.hpp.html
// splay
// key Link-Cut
struct Node {
S val; //
S acc; //
bool rev; //
Node* p; // heavy path
Node* l, * r; //
Node(S val = o()) : val(val), acc(val), rev(false), p(nullptr), l(nullptr), r(nullptr) {}
//
bool rootQ() {
//
// heavy path
return !p || (p->l != this && p->r != this);
}
//
void pushup() {
acc = op(l ? l->acc : o(), op(val, r ? r->acc : o()));
}
void pushdown() {
//
if (rev) {
rev = false;
swap(l, r);
// flip
if (l) l->rev = !l->rev;
if (r) r->rev = !r->rev;
}
// acc
pushup();
}
//
void rotR() {
Node* my_p = p, * my_pp = my_p->p;
//
if (my_p->l = r) r->p = my_p;
my_p->pushup();
//
r = my_p; my_p->p = this;
pushup();
//
if (p = my_pp) {
if (my_pp->l == my_p) my_pp->l = this;
if (my_pp->r == my_p) my_pp->r = this;
}
}
//
void rotL() {
Node* my_p = p, * my_pp = my_p->p;
//
if (my_p->r = l) l->p = my_p;
my_p->pushup();
//
l = my_p; my_p->p = this;
pushup();
//
if (p = my_pp) {
if (my_pp->l == my_p) my_pp->l = this;
if (my_pp->r == my_p) my_pp->r = this;
}
}
// heavy path
void splay() {
pushdown();
//
while (!rootQ()) {
Node* my_p = p;
//
if (my_p->rootQ()) {
my_p->pushdown();
pushdown();
//
if (my_p->l == this) rotR();
else rotL();
}
//
else {
Node* my_pp = my_p->p;
my_pp->pushdown();
my_p->pushdown();
pushdown();
// zig-zig
// zig-zag
if (my_pp->l == my_p) {
// zig-zig
if (my_p->l == this) my_p->rotR(), rotR();
// zig-zag
else rotL(), rotR();
}
else {
// zig-zig
if (my_p->r == this) my_p->rotL(), rotL();
// zig-zag
else rotR(), rotL();
}
}
}
}
};
int n; //
vector<Node> vs; //
// v 1 heavy path v heavy path
void expose(Node* v) {
auto v0(v);
// rt : heavy path
Node* rt = nullptr;
while (v) {
// v heavy path
v->splay();
// v heavy path
v->r = rt;
rt = v;
v->pushup();
// 1 heavy path
v = v->p;
}
// v heavy path
v0->splay();
}
// v
void cut(Node* v) {
// v heavy path
expose(v);
// v v 1 v
Node* l = v->l;
// v
v->l = nullptr;
l->p = nullptr;
v->pushup();
}
// v, vp v vp v
void link(Node* v, Node* vp) {
// v, vp heavy path
expose(v);
expose(vp);
// vp v v v vp
v->p = vp;
vp->r = v;
vp->pushup();
}
// v
void evert(Node* v) {
expose(v);
v->rev = !v->rev;
}
public:
// o() n
Link_cut_tree(int n) : n(n), vs(n) { }
// a[0..n) n
Link_cut_tree(const vector<S>& a) : n(sz(a)), vs(n) {
// verify : https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_path_sum
rep(i, n) vs[i].val = vs[i].acc = a[i];
}
// i
void cut(int i) {
cut(&vs[i]);
}
// s-t
void cut(int s, int t) {
// verify : https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_path_sum
if (s == t) return;
expose(&vs[s]), expose(&vs[t]);
if (!vs[s].p) return;
if (vs[t].l == &vs[s]) {
Node* l = vs[t].l;
vs[t].l = nullptr;
l->p = nullptr;
vs[t].pushup();
}
else {
expose(&vs[s]);
Node* l = vs[s].l;
vs[s].l = nullptr;
l->p = nullptr;
vs[s].pushup();
}
}
// s t s→t
void link(int s, int t) {
// verify : https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_path_sum
link(&vs[t], &vs[s]);
}
// rt
void set_root(int rt) {
// verify : https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_path_sum
evert(&vs[rt]);
}
// i
S sum(int i) {
// verify : https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_path_sum
expose(&vs[i]);
return vs[i].acc;
}
// i x
void set(int i, S x) {
expose(&vs[i]);
vs[i].val = x;
vs[i].pushup();
}
// i x
void add(int i, S x) {
// verify : https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_path_sum
expose(&vs[i]);
vs[i].val = op(x, vs[i].val);
vs[i].pushup();
}
// s, t
bool connectedQ(int s, int t) {
// verify : https://atcoder.jp/contests/abc350/tasks/abc350_g
if (s == t) return true;
expose(&vs[s]), expose(&vs[t]);
return vs[s].p;
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, Link_cut_tree LCT) {
// verify : https://atcoder.jp/contests/abc355/tasks/abc355_f
rep(i, LCT.n) {
os << LCT.vs[i].val << " ";
}
return os;
}
#endif
};
//
/* verify : https://atcoder.jp/contests/arc035/tasks/arc035_d */
using S001 = int;
S001 op001(S001 a, S001 b) { return a + b; }
S001 e001() { return 0; }
#define Sum_monoid S001, op001, e001
bool check(int n, vvi p, vi res) {
dsu d(n);
rep(j, n) {
if (res[j] == -1) continue;
if (d.same(j, p[res[j]][j])) return false;
d.merge(j, p[res[j]][j]);
}
return true;
}
void TLE() {
int n, K;
cin >> n >> K;
vvi p(n - 1, vi(n));
cin >> p;
--p;
Link_cut_tree<Sum_monoid> g(n);
mt19937_64 mt(2);
uniform_int_distribution<int> rnd(0, INF);
// 0 : 35, 36, 37, 43
// 1 : 35, 36, 38
// 2 : 35, 37, 43, 44
vi res(n, -1);
vi res_inv(n, -1);
vi q(n - 1);
iota(all(q), 0);
vi rem;
rep(i, n) rem.push_back(i);
while (!q.empty()) {
dump("-----------------------------------");
dump(res); dump(res_inv); dump(q);
int W = sz(q);
int w = rnd(mt) % W;
int i = q[w];
if (w != W - 1) swap(q[w], q[W - 1]);
q.pop_back();
shuffle(all(rem), mt);
int L = sz(rem);
dump(rem);
bool ok = false;
rep(l, L) {
int j = rem[l];
if (p[i][j] == -1) {
continue;
}
if (g.connectedQ(p[i][j], j)) continue;
g.set_root(j);
g.link(p[i][j], j);
res[j] = i;
res_inv[i] = j;
if (l != L - 1) swap(rem[l], rem[L - 1]);
rem.pop_back();
ok = true;
break;
}
if (ok) continue;
int l = rnd(mt) % L;
int j = rem[l];
if (p[i][j] == -1) {
q.push_back(i);
continue;
}
int j2 = p[i][j];
int i2 = res[j2];
g.cut(p[i2][j2], j2);
rem[l] = j2;
res[j2] = -1;
res_inv[i2] = -1;
q.push_back(i2);
g.set_root(j);
g.link(p[i][j], j);
res[j] = i;
res_inv[i] = j;
}
dump("-----------------------------------");
dump(res); dump(res_inv); dump(q); dump(rem);
dump("OK?:", check(n, p, res));
Yes(1);
rep(i, n) cout << res[i] + 1 << " \n"[i == n - 1];
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
auto start = chrono::system_clock::now();
int n, K;
cin >> n >> K;
vvi p(n - 1, vi(n));
cin >> p;
--p;
mt19937_64 mt(0);
uniform_int_distribution<int> rnd(0, INF);
// 0 : 37, 44
{
Link_cut_tree<Sum_monoid> g(n);
vi res(n, -1);
vi res_inv(n, -1);
queue<int> q;
rep(i, n - 1) q.push(i);
vi rem;
rep(i, n) rem.push_back(i);
while (!q.empty()) {
dump("-----------------------------------");
dump(res); dump(res_inv); dump(q);
int i = q.front(); q.pop();
shuffle(all(rem), mt);
int L = sz(rem);
dump(rem);
bool ok = false;
rep(l, L) {
int j = rem[l];
if (p[i][j] == -1) {
continue;
}
if (g.connectedQ(p[i][j], j)) continue;
g.set_root(j);
g.link(p[i][j], j);
res[j] = i;
res_inv[i] = j;
if (l != L - 1) swap(rem[l], rem[L - 1]);
rem.pop_back();
ok = true;
break;
}
if (ok) continue;
int l = rnd(mt) % L;
int j = rem[l];
if (p[i][j] == -1) {
q.push(i);
continue;
}
int j2 = p[i][j];
int i2 = res[j2];
g.cut(p[i2][j2], j2);
rem[l] = j2;
res[j2] = -1;
res_inv[i2] = -1;
q.push(i2);
g.set_root(j);
g.link(p[i][j], j);
res[j] = i;
res_inv[i] = j;
auto now = chrono::system_clock::now();
auto msec = chrono::duration_cast<chrono::milliseconds>(now - start).count();
if (msec >= 1450) break;
}
dump("-----------------------------------");
dump(res); dump(res_inv); dump(q); dump(rem);
dump("OK?:", check(n, p, res));
if (q.empty()) {
Yes(1);
rep(i, n) cout << res[i] + 1 << " \n"[i == n - 1];
return 0;
}
}
// 2
{
Link_cut_tree<Sum_monoid> g(n);
vi res(n, -1);
vi res_inv(n, -1);
queue<int> q;
repir(i, n - 2, 0) q.push(i);
vi rem;
rep(i, n) rem.push_back(i);
while (!q.empty()) {
dump("-----------------------------------");
dump(res); dump(res_inv); dump(q);
int i = q.front(); q.pop();
shuffle(all(rem), mt);
int L = sz(rem);
dump(rem);
bool ok = false;
rep(l, L) {
int j = rem[l];
if (p[i][j] == -1) {
continue;
}
if (g.connectedQ(p[i][j], j)) continue;
g.set_root(j);
g.link(p[i][j], j);
res[j] = i;
res_inv[i] = j;
if (l != L - 1) swap(rem[l], rem[L - 1]);
rem.pop_back();
ok = true;
break;
}
if (ok) continue;
int l = rnd(mt) % L;
int j = rem[l];
if (p[i][j] == -1) {
q.push(i);
continue;
}
int j2 = p[i][j];
int i2 = res[j2];
g.cut(p[i2][j2], j2);
rem[l] = j2;
res[j2] = -1;
res_inv[i2] = -1;
q.push(i2);
g.set_root(j);
g.link(p[i][j], j);
res[j] = i;
res_inv[i] = j;
}
dump("-----------------------------------");
dump(res); dump(res_inv); dump(q); dump(rem);
dump("OK?:", check(n, p, res));
if (q.empty()) {
Yes(1);
rep(i, n) cout << res[i] + 1 << " \n"[i == n - 1];
return 0;
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0