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 Studio】Gradle DSL method not found:'android()'

    如图所示: 参考:http://www.jianshu.com/p/d370d41fb7da 又遇到了这个问题: 参考:http://stackoverflow.com/questions/24204 ...

  2. 在Docker中部署Spring Boot项目

    想要获取更多文章可以访问我的博客 - 代码无止境. 微服务现在在互联网公司可谓非常流行了,之前找工作的的时候很多HR电话约面试的时候都会问对微服务是否有过接触.而微服务和Docker可以非常完美的结合 ...

  3. (13)ASP.NET Core 中的选项模式(Options)

    1.前言 选项(Options)模式是对配置(Configuration)的功能的延伸.在12章(ASP.NET Core中的配置二)Configuration中有介绍过该功能(绑定到实体类.绑定至对 ...

  4. 6.源码分析---和dubbo相比SOFARPC是如何实现负载均衡的?

    官方目前建议使用的负载均衡包括以下几种: random(随机算法) localPref(本地优先算法) roundRobin(轮询算法) consistentHash(一致性hash算法) 所以我们接 ...

  5. dotnetcore 与 hbase 之一——hbase 环境准备

    转载请注明出处www.cnblogs.com/hsxian! 总述 这是一系列针对 .net core (c#) 读取 hbase 的教程.本人苦于找不到 c#的原生 hbase 客户端,多番寻觅之下 ...

  6. exlipse php 插件安装地址

    以前的exlipse PHP插件老是有问题,下面这个地址可以使用. http://www.phpsrc.org/eclipse/pti/

  7. 既然synchronized是"万能"的,为什么还需要volatile呢?

    在我的博客和公众号中,发表过很多篇关于并发编程的文章,之前的文章中我们介绍过了两个在Java并发编程中比较重要的两个关键字:synchronized和volatile 我们简单回顾一下相关内容: 1. ...

  8. C#中appium自动化执行移动命令mobile:shell用法

    官网:https://appium.readthedocs.io/en/latest/en/commands/mobile-command/#android 1.执行ADB shell命令(需要设置服 ...

  9. Java网络编程 -- BIO 阻塞式网络编程

    阻塞IO的含义 阻塞(blocking)IO :阻塞是指结果返回之前,线程会被挂起,函数只有在得到结果之后(或超时)才会返回 非阻塞(non-blocking)IO :非阻塞和阻塞的概念相对应,指在不 ...

  10. 上个月,我赚了2W外快。。。

    前段时间和室友一起给某个公司做了一个管理系统,每个人分2W多.这里和大家分享一下做完项目后一点点感受,想到啥就说点啥. 核心竞争力 两个月就挣了2W块,挣了我爸妈两个人一年的收入,每天还贼辛苦,披星戴 ...