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. LinkedHashMap的特殊之处

    一.前言 乍眼一看会怀疑或者问LinkedHashMap与HashMap有什么区别? 它有什么与众不同之处?  由于前面已经有两篇文章分析了HashMap,今天就看看LinkedHashMap.(基于 ...

  2. 【iOS】Signing for "project_name" requires a development team. Select a development team in the project editor

    Xcode 8.3.2 运行 GitHub 上下载的代码时报了这个错. 解决方法: 单击工程名 --> Signing --> Team --> 选择对应的Account(如果没有A ...

  3. 【Mac】解压文件夹中文乱码

    Mac 用的英文系统,发现下载含中文的文件后,解压为乱码,如图所示: 解决方法: 下载一个解压软件:"The Unarchiver" 安装后设置下,如图: 之后设置压缩文件的默认打 ...

  4. JVM总结(二)

    JVM总结(2)java内存区域.字节码执行引擎 1.内存区域 程序计数器:知道线程执行位置,保证线程切换后能恢复到正确的执行位置. 虚拟机栈:存栈帧.栈帧里存局部变量表.操作栈.动态连接.方法返回地 ...

  5. 利用DoHome APP和音箱控制继电器通断电实验参考步骤

    准备材料: Arduino Uno 一块 Arduino 扩展板        购买链接 DT-06模块一个       购买链接 安卓手机一个 小度音箱一个 继电器模块一个 杜邦线若干 1.DT-0 ...

  6. Kubernetes 服务发现

    目录 什么是服务发现? 环境变量 DNS 服务 Linux 中 DNS 查询原理 Kubernetes 中 DNS 查询原理 调试 DNS 服务 存根域及上游 DNS 什么是服务发现? 服务发现就是一 ...

  7. sql server数据库查询链接服务器

    服务器对象->链接服务器: 或者 select  * from sys.servers: 找到服务器对象名称 select  * from [服务器对象名称].[数据库名称].dbo.[表名]:

  8. 如何利用jenkins插件查看allure报告-----完整篇(解决404和无数据问题)

    背景: python3+appium+pytest+allure写了安卓的自动化脚本,在windows本机pycharm上跑通过后生成了allure报告.  公司jenkins搭建在linux服务器上 ...

  9. Streaming-大数据的未来

    分享一篇关于实时流式计算的经典文章,这篇文章名为Streaming 101: The world beyond batch 那么流计算如何超越批处理呢? 从这几个方面说明:实时流计算系统,数据处理模式 ...

  10. copy and mutableCopy

    结论: 1, 深复制与浅复制 2,immutable和mutable 3,代码分析: #pragma mark - String - (void)stringCopyAndMutableCopy { ...