P2634 [国家集训队]聪聪可可(点分治做法)
生活随笔
收集整理的這篇文章主要介紹了
P2634 [国家集训队]聪聪可可(点分治做法)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
P2634 [國家集訓(xùn)隊(duì)]聰聰可可
題意:
一顆n個(gè)點(diǎn)的樹,問其中兩點(diǎn)之間的邊上數(shù)的和加起來是3的倍數(shù)的點(diǎn)對(duì)有多少個(gè)?
輸出這樣的點(diǎn)對(duì)所占比例
題解:
因?yàn)槭乔笕谋稊?shù),我們num來記錄%3=0,1,2的數(shù)量,對(duì)于u得到的d[],累加num[3-d[]]的數(shù)量。
其實(shí)就是常規(guī)的點(diǎn)分治模板
代碼:
#include <bits/stdc++.h> #include <unordered_map> #define debug(a, b) printf("%s = %d\n", a, b); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; clock_t startTime, endTime; //Fe~Jozky const ll INF_ll= 1e18; const int INF_int= 0x3f3f3f3f; void read(){}; template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar) {x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...); } template <typename T> inline void write(T x) {if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0'); } void rd_test() { #ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin); #endif } void Time_test() { #ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC); #endif } const int maxn= 2e5 + 9; vector<PII> vec[maxn]; int vis[maxn], d[maxn], sz[maxn]; int rt, cnt; int num[4]; void dfs_rt(int u, int fa, int tot) {sz[u]= 1;int v, w, n= 0;for (auto it : vec[u]) {v= it.first;if (v == fa || vis[v])continue;dfs_rt(v, u, tot);sz[u]+= sz[v];n= max(n, sz[v]);}n= max(n, tot - sz[u]);if (2 * n <= tot)rt= u; } void dfs_dis(int u, int fa, int dis) {d[++cnt]= dis;for (auto it : vec[u]) {int v= it.first;int w= it.second;if (v == fa || vis[v])continue;dfs_dis(v, u, dis + w);} } int work(int u, int fa, int tot) {dfs_rt(u, fa, tot);u= rt;vis[u]= 1;ll ans= 0;for (auto it : vec[u]) {int v= it.first;int w= it.second;if (vis[v])continue;cnt= 0;dfs_dis(v, u, w);sz[v]= cnt;for (int i= 1; i <= cnt; i++) {ans+= num[(3 - d[i] % 3) % 3];if (d[i] % 3 == 0)ans++;}for (int i= 1; i <= cnt; i++) {num[d[i] % 3]++;}}num[0]= num[1]= num[2]= 0;for (auto it : vec[u]) {int v= it.first;if (vis[v])continue;ans+= work(v, u, sz[v]);}return ans; } int main() {//rd_test();int n;read(n);for (int i= 1; i < n; i++) {int u, v, w;read(u, v, w);vec[u].push_back({v, w});vec[v].push_back({u, w});}ll a= 2ll * work(1, 0, n) + n;ll b= 1ll * n * n;ll g= __gcd(a, b);printf("%lld/%lld\n", a / g, b / g);return 0;//Time_test(); }總結(jié)
以上是生活随笔為你收集整理的P2634 [国家集训队]聪聪可可(点分治做法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《马力欧卡丁车 8 豪华版》通行证第 6
- 下一篇: 《英雄联盟》S13 半决赛:WBG 3-