異或最小生成樹

2021-10-08 18:43:01 字數 4371 閱讀 8335

/*

對於n個點的完全圖,每兩個點之間的距離等於兩點的權值的異或和,求最小生成樹

*/int n;

struct xortrie

void

insert

(ll x,

int id)

val[rt]

= x;

} ll answerpos

(int rt,

int pos,ll x)

return rt;

}void

traceback

(int rt)

ll divide

(int rt,

int pos)

return minl+

divide

(t[rt][0

],pos-1)

+divide

(t[rt][1

],pos-1)

;}else

if(t[rt][0

])return

divide

(t[rt][0

],pos-1)

;else

if(t[rt][1

])return

divide

(t[rt][1

],pos-1)

;return0;

}}trie;

例題有codeforces888g,和牛客多校第五場b graph

888g

b graph

**如下:

888g:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

typedef

unsigned

long

long ull;

typedef pair<

int,

int> pii;

#define rep(i, a, n) for(int i = a; i < n; i++)

#define per(i, a, n) for(int i = n-1; i >= a; i--)

#define ios std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

#define fopen freopen("file.in","r",stdin);freopen("file.out","w",stdout);

#define fclose fclose(stdin);fclose(stdout);

#define pi 3.14159265358979323846

const

int inf =

1e9;

const ll inf =

1e18

;const

int maxn =

2e5+10;

inline

intread()

while

(ch>=

'0'&&ch<=

'9')

return x*f;

}int n;/*

*/struct xortrie

void

insert

(ll x,

int id)

val[rt]

= x;

} ll answerpos

(int rt,

int pos,ll x)

return rt;

}void

traceback

(int rt)

ll divide

(int rt,

int pos)

return minl+

divide

(t[rt][0

],pos-1)

+divide

(t[rt][1

],pos-1)

;}else

if(t[rt][0

])return

divide

(t[rt][0

],pos-1)

;else

if(t[rt][1

])return

divide

(t[rt][1

],pos-1)

;return0;

}}tire;

signed

main()

tire.

solve()

;return0;

}

b-graph

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

typedef

unsigned

long

long ull;

typedef pair<

int,

int> pii;

#define rep(i, a, n) for(int i = a; i < n; i++)

#define per(i, a, n) for(int i = n-1; i >= a; i--)

#define ios std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

#define fopen freopen("file.in","r",stdin);freopen("file.out","w",stdout);

#define fclose fclose(stdin);fclose(stdout);

#define pi 3.14159265358979323846

const

int inf =

1e9;

const ll inf =

1e18

;const

int maxn =

2e5+10;

inline

intread()

while

(ch>=

'0'&&ch<=

'9')

return x*f;}/*

對於n個點的完全圖,每兩個點之間的距離等於兩點的權值的異或和,求最小生成樹

*/int n;

struct xortrie

void

insert

(ll x,

int id)

val[rt]

= x;

} ll answerpos

(int rt,

int pos,ll x)

return rt;

}void

traceback

(int rt)

ll divide

(int rt,

int pos)

return minl+

divide

(t[rt][0

],pos-1)

+divide

(t[rt][1

],pos-1)

;}else

if(t[rt][0

])return

divide

(t[rt][0

],pos-1)

;else

if(t[rt][1

])return

divide

(t[rt][1

],pos-1)

;return0;

}}trie;

std::vector v[maxn]

;void

dfs(

int rt,

int pre)

}signed

main()

), v[y]

.push_back()

;}dfs(1,

0); trie.

solve()

;return0;

}

G Xor MST(異或最小生成樹)

異或最小生成樹,這裡採用了一種分治的方法來貪心求解最值 為什麼這樣是對的 顯然我們分成兩個集合我們可以抵消掉高位的一大堆一樣的東西,這個時候,我們可以保證我們的貪心策略是正確的。為什麼我們要合併兩個集合 假設左邊集合有n nn個點,右邊集合有m mm個點,顯然左邊最多鏈結n 1 n 1 n 1條邊,...

B Graph(異或最小生成樹)

圖是聯通的,並且加邊的時候要保證環一定是異或值為0,所以我們可以保證從乙個點到另乙個點的路徑異或值是不變的,這個時候就簡單了,不就是乙個異或最小生成樹了嘛。我們只要預處理一下,任選乙個點作為根節點去得到從這個點到其他點的路徑異或值,然後再做一遍異或最小生成樹即可。pragma gcc optimiz...

最小生成樹 次小生成樹

一 最小生成樹 說到生成樹首先要解釋一下樹,樹是乙個聯通的無向無環圖,多棵樹的集合則被稱為森林。因此,樹具有許多性質 1.兩點之間的路徑是唯一的。2.邊數等於點數減一。3.連線任意兩點都會生成乙個環。對於乙個無向聯通圖g的子圖,如果它包含g的所有點,則它被稱為g的生成樹,而各邊權和最小的生成樹則被稱...