POJ-1741

题意:

对于带权的一棵树,求树中距离不超过k的点的对数。

思路:

点分治的裸题。 将这棵树分成很多小的树,分治求解。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFFLL; //
const ll nmos = 0x80000000LL; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //
const int mod = ; const double PI=acos(-1.0); // #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/
const int maxn = 1e5+;
int root = ,S,mx;
int n,k;
int sz[maxn],f[maxn],dis[maxn],cnt;
bool used[maxn];
struct node
{
int to,w,nx;
}e[maxn];
int h[maxn],tot = ;
void add(int u,int v,int w){
e[tot].to = v;
e[tot].w = w;
e[tot].nx = h[u];
h[u] = tot++;
}
void getRoot(int u, int fa){
sz[u] = ,f[u] = ;
for(int i = h[u] ; ~i; i= e[i].nx){
int v = e[i].to;
if(used[v] || fa == v)continue;
getRoot(v,u);
sz[u] += sz[v];
f[u] = max(f[u] , sz[v]);
}
f[u] = max(f[u],S - sz[u]);
if(f[u] < mx){root = u;mx = f[u];}
} void getDis(int u,int fa,int D){
for(int i=h[u] ; ~i; i=e[i].nx){
int v = e[i].to;
if(used[v]||v == fa)continue;
dis[++cnt] = D + e[i].w;
getDis(v,u,dis[cnt]);
}
} int getAns(int x,int D){
dis[cnt = ] = D;
getDis(x,,D);
sort(dis+,dis++cnt);
int le = ,ri =cnt,ans = ;
while(le <= ri){
if(dis[le] + dis[ri] <= k)ans += ri - le,le++;
else ri--;
}
return ans;
} int Divide(int x){
used[x] = true;
ll ans = getAns(x,);
for(int i=h[x]; ~i; i= e[i].nx){
int v = e[i].to;
if(used[v])continue;
ans -= getAns(v,e[i].w);
mx = inf,S = sz[v];
getRoot(v,x);ans += Divide(root);
}
return ans;
}
int main(){ while(~scanf("%d%d", &n, &k) && n+k)
{
memset(h,-,sizeof(h));
memset(used,false,sizeof(used));
tot = ;
for(int i=; i<n; i++){
int u,v,c;
scanf("%d%d%d", &u, &v,&c);
add(u,v,c);
add(v,u,c);
}
S = n;mx = inf;
getRoot(,-);
printf("%d\n",Divide(root));
}
return ;
}

POJ-1741

自己今天又写了一遍。

//点分治
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
/*
using namespace std;
#define pb push_back
#define debug(x) cerr<<#x<<" = " << x<<endl;
#define fi first
#define se second
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f; */
int n,k,ans;
const int maxn = ;
vector<pii>mp[maxn];
int dp[maxn],cen[maxn];
void getsize(int u,int fa){
dp[u] = ;
for(int i=; i<mp[u].size(); i++){
int v = mp[u][i].fi;
if(fa == v || cen[v]) continue;
getsize(v, u);
dp[u] += dp[v];
}
} pii getbig(int u,int fa,int t){
pii res = pii(inf, u);
int mx = ;
for(int i=; i<mp[u].size(); i++){
int v = mp[u][i].fi;
if(v == fa || cen[v])continue;
res = min(res, getbig(v, u, t));
mx = max(mx, dp[v]);
} res = min(res, pii(max(mx, t - dp[u]), u));
return res;
} void dfs(int u,int fa,int c, vector<int> & b){
b.pb(c);
for(int i=; i<mp[u].size(); i++){
int v = mp[u][i].fi,d = c + mp[u][i].se;
if(v == fa || cen[v])continue;
dfs(v,u,d,b);
}
}
int cal(vector<int>&b){ sort(b.begin(), b.end()); int res = ,r = b.size();
for(int i=; i<b.size(); i++){
while(r && b[i] + b[r-] > k) r--;
if(r > i) res += r - ;
else res += r;
}
return res/;
} void solve(int u){
getsize(u, -);
int s = getbig(u, -,dp[u]).se;
cen[s] = ; for(int i=; i<mp[s].size(); i++){
int v = mp[s][i].fi;
if(cen[v])continue;
solve(v);
} vector<int>a;
a.pb();
for(int i=; i<mp[s].size(); i++)
{
int v = mp[s][i].fi;
if(cen[v])continue; vector<int>b;
dfs(v, s, mp[s][i].se, b);
ans -= cal(b);
a.insert(a.end(),b.begin(),b.end());
} ans += cal(a);
cen[s] = ;
}
int main(){
while(~scanf("%d%d", &n, &k) && n + k){
for(int i=; i<=n; i++) mp[i].clear();
for(int i=; i<n; i++){
int u,v,w;
scanf("%d%d%d", &u, &v, &w);
mp[u].pb(pii(v,w));
mp[v].pb(pii(u,w));
}
ans = ;
solve();
printf("%d\n", ans);
}
return ;
}

new

POJ - 1741 - Tree - 点分治 模板的更多相关文章

  1. POJ 1741 Tree ——点分治

    [题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...

  2. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  3. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  4. [bzoj 1468][poj 1741]Tree [点分治]

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  5. POJ 1741 Tree(点分治点对<=k)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  6. [poj 1741]Tree 点分治

    题意 求树上距离不超过k的点对数,边权<=1000 题解     点分治.     点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处 ...

  7. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  8. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  9. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

随机推荐

  1. 【Android】No resource found that matches the given name 'Theme.Sherlock.Light.NoActionBar'

    被这个问题困扰了好久…… 错误如下: error: Error retrieving parent for item: No resource found that matches the given ...

  2. .NET Core CSharp 中级篇 2-2 List,ArrayList和Dictionary

    .NET Core CSharp 中级篇 2-2 本节内容为List,ArrayList,和Dictionary 简介 在此前的文章中我们学习了数组的使用,但是数组有一个很大的问题就是存储空间不足,我 ...

  3. .net持续集成测试篇之Nunit参数化测试

    系列目录 在进行单元测试的时候,很多时候,很多时候我们都是在单元测试方法内部提供特定的值,但是这样测试往往造成样本数不足从而导致覆盖的结果不够全面,很多时候我们更想提供来自外部的,满足条件的一组值来进 ...

  4. hadoop学习(五)----HDFS的java操作

    前面我们基本学习了HDFS的原理,hadoop环境的搭建,下面开始正式的实践,语言以java为主.这一节来看一下HDFS的java操作. 1 环境准备 上一篇说了windows下搭建hadoop环境, ...

  5. 分享我的GD32F450的IAP过程

    最近一个项目使用GD32F450VI+ESP8266需要做远程升级,基本参考正点原子IAP的那一章节,但是在GD32F450上却遇到了问题,无法跳转,然后使用正点原子的开发板stm32f429,以及s ...

  6. 不相交路径[BZOJ1471] 容斥原理 拓扑排序

    最近学容斥的时候又碰到一道类似的题目,所以想分享一个套路,拿这题来举例 [题目描述] 给出一个\(N(N\leq 150)\)个结点的有向无环简单图.给出4个不同的点\(a,b,c,d\),定义不相交 ...

  7. 洛谷 P3195 [HNOI2008]玩具装箱TOY

    题意简述 有n个物体,第i个长度为ci 将n个物体分为若干组,每组必须连续 如果把i到j的物品分到一组,则该组长度为 \( j - i + \sum\limits_{k = i}^{j}ck \) 求 ...

  8. nginx 使用HTTPS协议-SSL证书模块报错解决-附nginx安装 : [emerg] the "ssl" parameter requires ngx_http_ssl_module in nginx.c

    Linux系统下ngnix使用HTTPS协议启动报错: nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_modul ...

  9. java读取本机磁盘及遍历磁盘文件

    1. 获取本机所有盘符信息 //1. 获取本机盘符 File[] roots = File.listRoots(); for (int i = 0; i < roots.length; i++) ...

  10. Elasticsearch6.x和7.x版本常用插件汇总

    elasticsearch插件汇总 基于es 7.3版本试用. 一.安全插件 1.x-pack a.介绍 包括安全(x-pack-security),监视(x-pack-watcher),警报(x-p ...