「LAOI-2」纳米材料(T3)题解

· · 题解

点分治、字典树。\Theta(n\log v\log n)

Subtask 1

乱搞就行。

Subtask 2

枚举两个点暴力算贡献,O(n^3)

Subtask 3

以每个点为根 dfs 一遍,如果可以就累加答案,O(n^2)

Subtask 4

个人认为可能有边权为 1 的算法,就设了个这样的子任务。其实我也不知道有什么特殊做法。

Subtask 5

链的情况,可以简单转化为 HDU 6955(即把距离排序,然后每次双指针,像底下标程里面的 calcRES 函数一样),用一个字典树就行了。

Subtask 6

首先,我们发现异或值 \ge k 的条件很难维护,于是考虑二分,将题目转化为:长度不大于 mid 的情况下,最大异或值是否 \ge k。

接着不考虑权值。钦定一个根 u,则长度不超过 k 的路径只可能是以下 4 种: (定义「异或值」为图中的 1 到 i 的边权值异或和)

所以我们先使用双指针处理后三种的两两异或值最大,且距离 \le mid 的答案,再递归分治处理第一种情况。于是问题转化成了,如何在距离 \le mid 的情况下,求出最大异或。

我们知道,最大异或的传统 trick 是开一棵字典树,然后每次查询时从高位到低位尽量走与此数不一样的节点。于是我们可以把节点按照深度排序后用双指针,i 表示异或值已经插入字典树的节点,j 表示正在需要更新的节点。当 j=0,即更新结束就跳出循环。注意不要爆栈了,而且 i 也不要越界。这样就是一个 \Theta(n\log^2v\log n) 的做法。

Subtask 7

我们还可以做的更好。尝试去掉二分,还是照深度排序。这次让 i 来更新,j 来插入。首先排除肯定不可能最优的点后,把所有东西全丢进字典树。这样每次如果最大异或和 \ge k 的话就代表可以,用 i,j 距离更新答案,并且删除 j 节点(因为后面肯定用不到了,要用到肯定会让答案变大)。这样就是 \Theta(n\log n\log v)。

标程:

//#pragma GCC optimize("Ofast", "inline")
#include<bits/stdc++.h>

namespace std
{
    template<typename T1,typename T2,typename T3>struct triplet{
        T1 first;T2 second;T3 third;
        friend bool operator ==(triplet x,triplet y){return x.first==y.first&&x.second==y.second&&x.third==y.third;}
        friend bool operator !=(triplet x,triplet y){return x.first!=y.first||x.second!=y.second||x.third!=y.third;}
        friend bool operator <(triplet x,triplet y){return x.first==y.first?x.second==y.second?x.third<y.third:x.second<y.second:x.first<y.first;}
        friend bool operator >(triplet x,triplet y){return x.first==y.first?x.second==y.second?x.third>y.third:x.second>y.second:x.first>y.first;}
        friend bool operator <=(triplet x,triplet y){return x.first==y.first?x.second==y.second?x.third<=y.third:x.second<y.second:x.first<y.first;}
        friend bool operator >=(triplet x,triplet y){return x.first==y.first?x.second==y.second?x.third>=y.third:x.second>y.second:x.first>y.first;}
        friend triplet operator +(triplet x,triplet y){return (triplet){x.first+y.first,x.second+y.second,x.third+y.third};}
        friend triplet operator -(triplet x,triplet y){return (triplet){x.first-y.first,x.second-y.second,x.third-y.third};}

        triplet& operator +=(triplet x){return (*this)=(*this)+x;}
        triplet& operator -=(triplet x){return (*this)=(*this)-x;}
    };
    template<typename T1,typename T2,typename T3>triplet<T1,T2,T3> make_triplet(T1 x,T2 y,T3 z){
        triplet<T1,T2,T3> res;
        res.first=x;res.second=y;res.third=z;return res;
    }
}

#define Rep(i, n) for(int i=0; i< (int)(n); i++)
#define Rpp(i, n) for(int i=1; i<=(int)(n); i++)
#define Dpp(i, n) for(int i=(int)n; i; i--)
#define Frr(i, s, e) for(int i=(int)(s); i<=(int)(e); i++)
#define Tc int T; cin >> T; while(T--)
#define Eps 1e-7
#define Pinf 0x3f3f3f3f3f3f3f3fLL
#define Ninf (long long)0xcfcfcfcfcfcfcfcfLL
#define Mem0(Cont) memset(Cont, 0, sizeof(Cont))
#define MemP(Cont) memset(Cont, 0x3f, sizeof(Cont))
#define MemN(Cont) memset(Cont, 0xcf, sizeof(Cont))
#define endl '\n'
//#define int long long
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define Yes cout << "Yes\n"
#define No cout << "No\n"
#define yes cout << "yes\n"
#define no cout << "no\n"
//#define Files
using namespace std;

template <typename T> inline void Print(T x, char ed = '\n') { cout << x << ed; }
template <typename T> inline void Exit(T x, int cd = 0) { cout << x << endl; exit(cd); }
template <typename T> inline bool CheckMax(T& x, T y) { if(x < y) { x = y; return 1; } else return 0; }
template <typename T> inline bool CheckMin(T& x, T y) { if(y < x) { x = y; return 1; } else return 0; }
inline void Print_if(bool sth, string s1 = "Yes", string s2 = "No") { if(sth) cout << s1 << endl; else cout << s2 << endl; }

constexpr int N = 114514 << 1;
struct Trie { //字典树
    int t[N << 5][2], cn[N << 5], tot = 1;
    void clear() { //递归清空
        _clr(1);
        tot = 1;
    }
    void insert(int x) { //插入一个数
        int now = 1;
        for(int i=30; ~i; i--) {
            ++cn[now];
            int xi = x>>i & 1;
            if(!t[now][xi]) //没有节点就新建节点
                t[now][xi] = ++tot;
            now = t[now][xi]; //在树上跳
        }
        ++cn[now];
    }
    void erase(int x) { //删除一个数
        int now = 1;
        for(int i=30; ~i; i--) {
            --cn[now];
            int xi = x>>i & 1;
            if(!t[now][xi])
                t[now][xi] = ++tot;
            now = t[now][xi];
        }
        --cn[now];
    }
    int getmax(int x) {
        int now = 1, res = 0;
        for(int i=30; ~i; i--) {
            int xi = x>>i & 1, nxt = xi^1;
            //要使异或和最大,就要让高位尽量不一样
            if(!t[now][nxt] ||
                !cn[t[now][nxt]]) nxt = xi;
            //如果高位不一样的没有节点就不行
            res += (nxt ^ xi) << i; //累加答案
            now = t[now][nxt];
        }
        return res;
    }
    Trie() { clear(); }
private:
    void _clr(int now) {
        cn[now] = 0;
        if(!now) return ;
        _clr(t[now][0]);
        _clr(t[now][1]);
        t[now][0] = t[now][1] = 0;
    }
};

struct Edge {
    int u, v, l, w, nxt;
} E[N << 1];
int head[N], tot = 1, n, k, xo[N], dep[N];
long long res=Pinf, cc;
//i到j的路径的xor和等于xo[i] xor xo[j],原因见题解
bool vis[N]; //vis[u] = 1: u已经当过重心了,要把它删掉
pair <long long, long long> p[N]; //所有可能的节点
Trie trie;
void addEdge(int u, int v, int l, int w) {
    E[++tot] = {u, v, l, w, head[u]}; head[u] = tot;
    E[++tot] = {v, u, l, w, head[v]}; head[v] = tot;
}

void preDfs(int u, int p) {
    //注意dep[1]=xo[1]=0
    for(int i=head[u]; i; i=E[i].nxt) {
        int v = E[i].v, l = E[i].l, w = E[i].w;
        if(v == p) continue;
        xo[v] = xo[u] ^ w;
        dep[v] = dep[u] + l;
        preDfs(v, u);
    }
}

int calcSize(int u, int p) { //子树u的大小
    int res = 1;
    for(int i=head[u]; i; i=E[i].nxt) {
        int v = E[i].v;
        if(v == p) continue;
        if(vis[v]) continue;
        res += calcSize(v, u);
    }
    return res;
}

//其实res和psz可以放全局,但为了算法模块化及debug方便,传了局部引用
int calcCTR(int u, int p, int all, int& res, int& psz) {
    int sz = 1, mssz = Ninf; //子树u的大小;最大子节点的大小
    //注意我们要让“最大子节点”最小,所以初值是正无穷还是负无穷要搞清
    for(int i=head[u]; i; i=E[i].nxt) {
        int v = E[i].v;
        if(v == p) continue;
        if(vis[v]) continue;
        int sonsz = calcCTR(v, u, all, res, psz);
        sz += sonsz; CheckMax(mssz, sonsz);
        //累加子树u大小;更新最大子节点的大小
    }
    CheckMax(mssz, all - sz); //别忘了还有父节点那一块
    if(CheckMin(psz, mssz)) res = u;
    return sz;
}

int centre(int u) { //u 所在的子树的重心
    int psz = Pinf, //每个点最大的子节点最小的大小(节点数)是多少
        res = 0, all = calcSize(u, 0); //重心;整棵树的大小
    calcCTR(u, 0, all, res, psz);
    return res;
}

#define pjj pair <int, int> //宏定义,节省时间
#define FF first
#define SS second

void calcRES() {
    trie.clear(); int j = cc;
    while(j && p[j].SS >= res) --j; //排除不能更新答案的冗余
    Rpp(i, j) trie.insert(p[i].FF); //可能的都插入字典树
    Rpp(i, cc) while(j && trie.getmax(p[i].FF) >= k) {
        //双指针,而且统计完一个的答案就删掉(这就是排序的好处)
        CheckMin(res, p[i].SS + p[j].SS);
        trie.erase(p[j--].FF);
    }
}

void getNodes(int u, int fa, int d, int xo) {
    if(d > k) return ;
    p[++cc] = {xo, d};
    for(int i=head[u]; i; i=E[i].nxt) {
        int v = E[i].v, l = E[i].l, w = E[i].w;
        if(v == fa) continue;
        if(vis[v]) continue;
        getNodes(v, u, d + l, xo ^ w);
    }
}

void div_con(int u) { //点分治
    cc = 0; //清空
    //第一维是节点,第二维是深度
    // cout << "u" << u << endl << flush;
    getNodes(u, 0, 0, 0);
    sort(p+1, p+cc+1, [](pjj p1, pjj p2) {
        return p1.SS < p2.SS;
        //一定要深度小的排前,否则calcRES就错误了
    });
    // if(u == 5) { calcRES(); exit(0); }
    // Rpp(i, cc)
        // cout << u << ' ' << p[i].FF << ' ' << p[i].SS << ' ' << res << endl;
    // cout << cc << endl << flush;
    calcRES();
    vis[u] = 1;
    for(int i=head[u]; i; i=E[i].nxt) {
        if(!vis[E[i].v]) div_con(centre(E[i].v));
    }
}

signed main()
{
#ifdef Files
    freopen(".in", "r", stdin);
    freopen(".out", "w",stdout);
#endif
    ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

    cin >> n >> k;
    int suml=0;
    Rpp(i, n-1) {
        int u, v, l, w;
        cin >> u >> v >> l >> w;
        addEdge(u, v, l, w); suml += l;
//      if(i <= 15000 && w > 1073741823) {
//          cerr << i << ' ' << w << endl;
//          return 0;
//      }
    }
    preDfs(1, 0);
    div_con(centre(1));
    cout << (res > 1145141919810 ? -1 : res) << endl;
    return 0;
}