Official
N - 通貨変換ネットワーク / Currency Conversion Network Editorial
by
N - 通貨変換ネットワーク / Currency Conversion Network Editorial
by
physics0523
Editorial for printing
本問題で要求されている操作は以下の \(4\) つです。
- 辺を追加する。
- 辺を削除する。
- 辺の重みを書き換える。
- パス上のアフィンの合成を求める。
これら全ての操作は、 Link Cut Tree によって実現可能です。
ただし、多くのライブラリでは Link Cut Tree は頂点に重みを持たせるような実装をしています。
これを辺重みに変換するためには、以下の改造が必要です。
- 頂点 \(u,v\) を結ぶ辺に対応する補助頂点 \(w\) を追加し、頂点 \(w\) に辺重みを結びつける。
- 辺 \(u-v\) の代わりに、辺 \(u-w,w-v\) を追加する。
各頂点には「浅い側から深い側に辿ったときの重み」「深い側から浅い側に辿ったときの重み」を記録しておき、補助木 (Splay 木) 中の flip を呼ぶ際に上下関係の入れ替わった頂点に対する情報も swap することにすると、本問題の要求に適った LCT を作ることができます。
コンテスト本番中には使えない手ですが、コンテスト外でのライブラリ作成などで生成 AI を活用することで、このような大変なライブラリの整備も比較的容易に行えます。
実装例 (C++):
#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
// Original source: https://qandori.github.io/cp-library/tree/link_cut_tree.hpp.html (tree/link_cut_tree.hpp)
// Modified by Gemini
template <class S, auto op, auto e, bool commutative = false>
class LinkCutTree {
struct Node {
int ch[2] = {}, parent = 0;
std::uint32_t count : 31 = 1;
std::uint32_t rev : 1 = 0;
S value, rev_value, prod, back;
explicit Node(const S &v, const S &rv) : value(v), rev_value(rv), prod(v), back(rv) {}
explicit Node(const S &v) : Node(v, v) {}
};
int n = 0;
std::vector<Node> t;
std::vector<int> free_edges;
void init(int n_) {
assert(0 <= n_ && n_ < std::numeric_limits<int>::max() / 2);
n = n_;
t.reserve(std::size_t(2) * n + 1);
t.emplace_back(e());
t[0].count = 0;
}
int node(int v) const {
assert(0 <= v && v < n);
return v + 1;
}
int alloc_edge(S w_uv, S w_vu) {
if (free_edges.empty()) {
int eid = static_cast<int>(t.size());
t.emplace_back(std::move(w_uv), std::move(w_vu));
return eid;
}
int eid = free_edges.back();
free_edges.pop_back();
t[eid] = Node(std::move(w_uv), std::move(w_vu));
return eid;
}
void free_edge(int eid) {
t[eid].ch[0] = t[eid].ch[1] = t[eid].parent = 0;
t[eid].count = 1, t[eid].rev = 0;
free_edges.push_back(eid);
}
bool is_root(int x) const { return t[x].parent <= 0; }
void pull(int x) {
auto &v = t[x];
int l = v.ch[0], r = v.ch[1];
v.count = t[l].count + t[r].count + 1;
v.prod = l ? op(t[l].prod, v.value) : v.value;
if (r) v.prod = op(std::move(v.prod), t[r].prod);
v.back = r ? op(t[r].back, v.rev_value) : v.rev_value;
if (l) v.back = op(std::move(v.back), t[l].back);
}
void flip(int x) {
if (!x) return;
std::swap(t[x].ch[0], t[x].ch[1]);
std::ranges::swap(t[x].value, t[x].rev_value);
std::ranges::swap(t[x].prod, t[x].back);
t[x].rev ^= 1;
}
void push(int x) {
if (!t[x].rev) return;
flip(t[x].ch[0]);
flip(t[x].ch[1]);
t[x].rev = 0;
}
void rotate(int x) {
int p = t[x].parent, g = t[p].parent;
int d = t[p].ch[1] == x, c = t[x].ch[d ^ 1];
if (g > 0) t[g].ch[t[g].ch[1] == p] = x;
t[x].parent = g;
t[p].ch[d] = c;
t[c].parent = p;
t[x].ch[d ^ 1] = p;
t[p].parent = x;
pull(p);
}
void splay(int x) {
if (is_root(x)) {
push(x);
return;
}
do {
int p = t[x].parent, g = t[p].parent;
bool top = is_root(p);
if (!top) push(g);
push(p);
push(x);
if (!top) rotate((t[p].ch[0] == x) == (t[g].ch[0] == p) ? p : x);
rotate(x);
} while (!is_root(x));
}
int access(int x) {
splay(x);
if (t[x].ch[1]) t[t[x].ch[1]].parent = -x;
t[x].ch[1] = 0;
int last = x;
while (t[x].parent) {
int p = -t[x].parent;
splay(p);
if (t[p].ch[1]) t[t[p].ch[1]].parent = -p;
t[p].ch[1] = x;
t[x].parent = p;
rotate(x);
last = p;
}
return last;
}
void reroot(int x) {
access(x);
flip(x);
}
int expose(int u, int v) {
u = node(u), v = node(v);
reroot(u);
access(v);
assert(u == v || t[u].parent != 0);
return v;
}
public:
explicit LinkCutTree(int n_ = 0) {
init(n_);
for (int i = 0; i < n; ++i) t.emplace_back(t[0].value);
}
int size() const { return n; }
void build_forest(std::span<const std::tuple<int, int, S, S>> edges) {
assert(edges.size() < std::size_t(n) || (edges.empty() && n == 0));
int m = static_cast<int>(edges.size());
int total = n + m;
t.resize(n + 1, Node(t[0].value));
free_edges.clear();
std::vector<int> degree(total), neighbors(total), parent(total, -1), order;
order.reserve(total);
for (int i = 0; i < m; ++i) {
const auto &[u, v, w_uv, w_vu] = edges[i];
assert(0 <= u && u < n && 0 <= v && v < n && u != v);
int eid = alloc_edge(w_uv, w_vu) - 1;
++degree[u], ++degree[eid], ++degree[v], ++degree[eid];
neighbors[u] ^= eid, neighbors[eid] ^= u;
neighbors[v] ^= eid, neighbors[eid] ^= v;
}
for (int v = 0; v < total; ++v) if (degree[v] <= 1) order.push_back(v);
for (std::size_t i = 0; i < order.size(); ++i) {
int v = order[i];
if (!degree[v]) continue;
int p = neighbors[v];
parent[v] = p;
degree[v] = 0;
neighbors[p] ^= v;
if (--degree[p] == 1) order.push_back(p);
}
assert(order.size() == std::size_t(total));
for (int i = 0; i < m; ++i) {
const auto &[u, v, w_uv, w_vu] = edges[i];
int eid = n + i;
auto &x = t[eid + 1];
if (parent[eid] == v) {
x.value = w_vu;
x.rev_value = w_uv;
}
}
for (int v = 0; v < total; ++v) {
auto &x = t[v + 1];
x.ch[0] = x.ch[1] = 0;
x.parent = -parent[v] - 1;
x.count = 1, x.rev = 0;
x.prod = x.value;
x.back = x.rev_value;
}
}
bool link(int u, int v, S w_uv, S w_vu) {
int nu = node(u), nv = node(v);
if (nu == nv) return false;
reroot(nv);
access(nu);
if (t[nv].parent) return false;
int eid = alloc_edge(std::move(w_uv), std::move(w_vu));
t[nv].parent = -eid;
t[eid].parent = -nu;
return true;
}
bool link(int u, int v, S w) { return link(u, v, w, w); }
bool cut(int u, int v) {
u = node(u), v = node(v);
if (u == v) return false;
reroot(u);
access(v);
int l = t[v].ch[0];
if (!t[u].parent || t[l].count != 2) return false;
int eid = (l == u) ? t[u].ch[1] : l;
t[v].ch[0] = 0;
t[u].ch[0] = t[u].ch[1] = t[u].parent = 0;
t[u].count = 1, t[u].rev = 0;
free_edge(eid);
return true;
}
void set_edge(int u, int v, S w_uv, S w_vu) {
int x = expose(u, v), l = t[x].ch[0];
assert(t[l].count == 2);
int eid = (l == node(u)) ? t[l].ch[1] : l;
splay(eid);
t[eid].value = std::move(w_uv);
t[eid].rev_value = std::move(w_vu);
}
void set_edge(int u, int v, S w) { set_edge(u, v, w, w); }
S get_edge(int u, int v) {
int x = expose(u, v), l = t[x].ch[0];
assert(t[l].count == 2);
int eid = (l == node(u)) ? t[l].ch[1] : l;
splay(eid);
return t[eid].value;
}
void make_root(int v) { reroot(node(v)); }
int root(int v) {
int x = node(v);
access(x);
push(x);
while (t[x].ch[0]) push(x = t[x].ch[0]);
splay(x);
return x - 1;
}
bool same(int u, int v) {
u = node(u), v = node(v);
if (u == v) return true;
access(u);
access(v);
return t[u].parent != 0;
}
S prod(int u, int v) {
int x = expose(u, v), l = t[x].ch[0];
return l ? op(t[l].prod, t[x].value) : t[x].value;
}
int distance(int u, int v) {
int x = expose(u, v);
return t[t[x].ch[0]].count / 2;
}
int path_size(int u, int v) { return distance(u, v) + 1; }
int jump(int u, int v, int k) {
int x = expose(u, v);
if (k < 0 || 2 * k > int(t[t[x].ch[0]].count)) return -1;
int target = 2 * k;
for (;;) {
push(x);
int l = t[x].ch[0], count = t[l].count;
if (target == count) break;
if (target < count) x = l;
else target -= count + 1, x = t[x].ch[1];
}
splay(x);
return x - 1;
}
int lca(int r, int u, int v) {
r = node(r), u = node(u), v = node(v);
reroot(r);
access(u);
assert(r == u || t[r].parent != 0);
int w = access(v);
assert(u == v || t[u].parent != 0);
return w - 1;
}
};
using namespace atcoder;
using mint=modint998244353;
typedef struct{
mint a;
mint b;
}Aff;
Aff e(){return {(mint)1,(mint)0};}
Aff op(Aff l,Aff r){
return {l.a*r.a, l.b*r.a+r.b};
}
using pi=pair<int,int>;
int main(){
int N,Q;
cin >> N >> Q;
LinkCutTree<Aff,op,e,false> lct(N+Q+5);
set<pi> st;
for(int qn=1;qn<=Q;qn++){
string typ;
cin >> typ;
if(typ=="L"){
int u,v,a,b,c,d;
cin >> u >> v >> a >> b >> c >> d;
lct.link(u,v,Aff{((mint)a),((mint)b)},Aff{((mint)c),((mint)d)});
st.insert({u,v});
}
if(typ=="C"){
int u,v;
cin >> u >> v;
if(st.find({u,v})==st.end()){ swap(u,v); }
lct.cut(u,v);
st.erase({u,v});
}
if(typ=="U"){
int u,v,a,b,c,d;
cin >> u >> v >> a >> b >> c >> d;
if(st.find({u,v})==st.end()){ swap(u,v); }
lct.cut(u,v);
lct.link(u,v,Aff{((mint)a),((mint)b)},Aff{((mint)c),((mint)d)});
}
if(typ=="Q"){
int x,y,s;
cin >> x >> y >> s;
if(!lct.same(x,y)){
cout << "-1\n";
continue;
}
auto op=lct.prod(x,y);
mint oup(s);
oup*=op.a;
oup+=op.b;
cout << oup.val() << "\n";
}
}
return 0;
}
posted:
last update:
