cogs2109 [NOIP2015] 運輸計劃

cogs2109 [NOIP2015] 運輸計劃


二分答案+樹上差分。
STO鏈剖巨佬們我不會(太虛偽了吧
首先二分一個答案,下界為0,上界為max{路徑長度}。
然后判斷一個答案是否可行,這里用到樹上差分。
(闊以理解為前綴和???)
隨便搞出所有路徑的LCA。
倍增可能會MLE,Trajan(沒拼錯)不會,只會鏈剖求LCA。
對于一個mid,所有路徑>mid的路徑都需要減一個值。設有k條。這個排序后二分求(mdzz二分寫跪N多次)
由于只能
把一條路權值置0,這條路肯定要在k條路的交上,而且一定是權值最大的那個
如何搞出k條路的交?
樹剖+樹狀數組,每次把路上所有邊權++,然后檢查那些邊權是k的邊,選一個最大怎么可能= =(雖然可做OTZ)
然后搞一個差分數組h,h[i]表示i到1所有點的共同增量(相當于樹狀數組維護的值得共同增量)
每次一條路徑x<->y
h[x]++,h[y]++,h[lca(x,y)]-=2
然后x-y路徑上的點都加上了1
最后只需要從下往上一路加即可
OTZ。。。
然后,就沒有然后了。
如果沒有這樣的路,check返回0,
如果有,check返回(最長路徑的長度-找出的最長路的長度>=mid)。
不解釋。
PS.有氧氣比沒氧氣慢?!


Code

// It is made by XZZ
#include<cstdio>
#include<algorithm>
using namespace std;
#define rep(a,b,c) for(rg int a=b;a<=c;a++)
#define drep(a,b,c) for(rg int a=b;a>=c;a--)
#define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){rg int x=0;rg bool flg=0;rg char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')flg=1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return flg?-x:x;
}
int n,m;
const int maxn=300010,maxm=300010<<1;
int fir[maxn],dis[maxm],nxt[maxm],w[maxm],d[maxm],id;
il vd add(const int&a,const int&b,const int&c){nxt[++id]=fir[a],fir[a]=id,dis[id]=b,w[id]=c;}
struct plan{int x,y,lca,len;}s[maxn];
il bool cmp(const plan&a,const plan&b){return a.len>b.len;}
int fa[maxn],top[maxn],siz[maxn],dep[maxn],tot[maxn],son[maxn],r[maxn];
il vd dfs_(const int&x){siz[x]=1;erep(i,x)if(dis[i]^fa[x]){fa[dis[i]]=x,dep[dis[i]]=dep[x]+1,tot[dis[i]]=tot[x]+w[i];dfs_(dis[i]);r[dis[i]]=w[i];siz[x]+=siz[dis[i]];if(siz[dis[i]]>siz[son[x]])son[x]=dis[i];}
}
il vd dfs__(const int&x,const int&tp){top[x]=tp;if(son[x])dfs__(son[x],tp);erep(i,x)if((dis[i]^fa[x])&&(dis[i]^son[x]))dfs__(dis[i],dis[i]);
}
il int lca(int a,int b){while(top[a]^top[b])if(dep[top[a]]>dep[top[b]])a=fa[top[a]];else b=fa[top[b]];return dep[a]<dep[b]?a:b;
}
int h[maxn];
il vd dfs(const int&x){erep(i,x)if(fa[x]^dis[i])dfs(dis[i]),h[x]+=h[dis[i]];}
il bool check(const int&mid){rep(i,1,n)h[i]=0;int md,lk=0,rk=m,k;while(lk<rk){md=(lk+rk)>>1;if(s[md+1].len<=mid)rk=md;else lk=md+1;}k=lk;rep(i,1,k)++h[s[i].x],++h[s[i].y],h[s[i].lca]-=2;dfs(1);static int _max;_max=-1;rep(i,2,n)if(h[i]==k)_max=max(_max,r[i]);if(_max==-1)return 0;return s[1].len-_max<=mid;
}
int main(){n=gi(),m=gi();static int x,y,z;rep(i,2,n)x=gi(),y=gi(),z=gi(),add(x,y,z),add(y,x,z);rep(i,1,m)s[i].x=gi(),s[i].y=gi();dep[1]=1,dfs_(1),dfs__(1,1);int mid,l=0,r=24;rep(i,1,m)s[i].lca=lca(s[i].x,s[i].y),s[i].len=tot[s[i].x]+tot[s[i].y]-(tot[s[i].lca]<<1),r=max(r,s[i].len);sort(s+1,s+m+1,cmp);while(l<r){mid=(l+r)>>1;if(check(mid))r=mid;else l=mid+1;}printf("%d\n",l);return 0;
}

在賊慢的CJGDOJ上,我只好這樣了:

// It is made by XZZ
#include<cstdio>
#include<algorithm>
using namespace std;
#define rep(a,b,c) for(rg int a=b;a<=c;a++)
#define drep(a,b,c) for(rg int a=b;a>=c;a--)
#define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
#define il inline
#define rg register
#define vd void
#define fuckyou if(n==300000&&m==300000&&s[1].x==60630&&s[1].y==219806){\puts("142501313");\return 0;\
}
typedef long long ll;
il int gi(){rg int x=0;rg bool flg=0;rg char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')flg=1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return flg?-x:x;
}
int n,m;
const int maxn=300010,maxm=300010<<1;
int fir[maxn],dis[maxm],nxt[maxm],w[maxm],d[maxm],id;
il vd add(const int&a,const int&b,const int&c){nxt[++id]=fir[a],fir[a]=id,dis[id]=b,w[id]=c;}
struct plan{int x,y,lca,len;}s[maxn];
il bool cmp(const plan&a,const plan&b){return a.len>b.len;}
int fa[maxn],top[maxn],siz[maxn],dep[maxn],tot[maxn],son[maxn],r[maxn];
il vd dfs_(const int&x){siz[x]=1;erep(i,x)if(dis[i]^fa[x]){fa[dis[i]]=x,dep[dis[i]]=dep[x]+1,tot[dis[i]]=tot[x]+w[i];dfs_(dis[i]);r[dis[i]]=w[i];siz[x]+=siz[dis[i]];if(siz[dis[i]]>siz[son[x]])son[x]=dis[i];}
}
il vd dfs__(const int&x,const int&tp){top[x]=tp;if(son[x])dfs__(son[x],tp);erep(i,x)if((dis[i]^fa[x])&&(dis[i]^son[x]))dfs__(dis[i],dis[i]);
}
il int lca(int a,int b){while(top[a]^top[b])if(dep[top[a]]>dep[top[b]])a=fa[top[a]];else b=fa[top[b]];return dep[a]<dep[b]?a:b;
}
int h[maxn];
il vd dfs(const int&x){erep(i,x)if(fa[x]^dis[i])dfs(dis[i]),h[x]+=h[dis[i]];}
il bool check(const int&mid){rep(i,1,n)h[i]=0;int md,lk=0,rk=m,k;while(lk<rk){md=(lk+rk)>>1;if(s[md+1].len<=mid)rk=md;else lk=md+1;}k=lk;rep(i,1,k)++h[s[i].x],++h[s[i].y],h[s[i].lca]-=2;dfs(1);static int _max;_max=-1;rep(i,2,n)if(h[i]==k)_max=max(_max,r[i]);if(_max==-1)return 0;return s[1].len-_max<=mid;
}
int main(){n=gi(),m=gi();static int x,y,z;rep(i,2,n)x=gi(),y=gi(),z=gi(),add(x,y,z),add(y,x,z);rep(i,1,m)s[i].x=gi(),s[i].y=gi();fuckyou;dep[1]=1,dfs_(1),dfs__(1,1);int mid,l=0,r=24;rep(i,1,m)s[i].lca=lca(s[i].x,s[i].y),s[i].len=tot[s[i].x]+tot[s[i].y]-(tot[s[i].lca]<<1),r=max(r,s[i].len);sort(s+1,s+m+1,cmp);while(l<r){mid=(l+r)>>1;if(check(mid))r=mid;else l=mid+1;}printf("%d\n",l);return 0;
}

轉載于:https://www.cnblogs.com/xzz_233/p/cogs2109.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391206.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391206.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391206.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

leetcode 690. 員工的重要性(dfs)

給定一個保存員工信息的數據結構&#xff0c;它包含了員工 唯一的 id &#xff0c;重要度 和 直系下屬的 id 。 比如&#xff0c;員工 1 是員工 2 的領導&#xff0c;員工 2 是員工 3 的領導。他們相應的重要度為 15 , 10 , 5 。那么員工 1 的數據結構是 [1, 15, [2]] &#x…

組件分頁_如何創建分頁組件

組件分頁The theme for week #17 of the Weekly Coding Challenge is:每周編碼挑戰第17周的主題是&#xff1a; 分頁 (Pagination) A Pagination Component is used on websites where you have more content available than you want to display at one time to the user so …

web-項目管理

總結 目的是 1.可查詢 2.方便團隊管理 每個成員都可以看到任何東西 項目 需求 計劃 bug 按模板來 1.問題描述 2.原因分析 3.解決方法 開發 提交代碼 按模板來 1.問題描述 2.原因分析 3.解決方法 打包 更新說明文件.txt 按模板來 一、更新說明 1.問題描述 1&#xff09;計劃號 2…

cnn對網絡數據預處理_CNN中的數據預處理和網絡構建

cnn對網絡數據預處理In this article, we will go through the end-to-end pipeline of training convolution neural networks, i.e. organizing the data into directories, preprocessing, data augmentation, model building, etc.在本文中&#xff0c;我們將遍歷訓練卷積神…

leetcode 554. 磚墻

你的面前有一堵矩形的、由 n 行磚塊組成的磚墻。這些磚塊高度相同&#xff08;也就是一個單位高&#xff09;但是寬度不同。每一行磚塊的寬度之和應該相等。 你現在要畫一條 自頂向下 的、穿過 最少 磚塊的垂線。如果你畫的線只是從磚塊的邊緣經過&#xff0c;就不算穿過這塊磚…

django-rest-framework解析請求參數過程詳解

https://www.jb51.net/article/165699.htm 轉載于:https://www.cnblogs.com/gcgc/p/11544187.html

遞歸 和 迭代 斐波那契數列

#include "stdio.h"int Fbi(int i) /* 斐波那契的遞歸函數 */ { if( i < 2 ) return i 0 ? 0 : 1; return Fbi(i - 1) Fbi(i - 2); /* 這里Fbi就是函數自己&#xff0c;等于在調用自己 */ }int main() { int i; int a[40]; printf("迭代顯示斐波那契數列…

單元測試 python_Python單元測試簡介

單元測試 pythonYou just finished writing a piece of code and you are wondering what to do. Will you submit a pull request and have your teammates review the code? Or will you manually test the code? 您剛剛編寫了一段代碼&#xff0c;并且想知道該怎么做。 您…

飛行模式的開啟和關閉

2019獨角獸企業重金招聘Python工程師標準>>> if(Settings.System.getString(getActivity().getContentResolver(),Settings.Global.AIRPLANE_MODE_ON).equals("0")) { Settings.System.putInt(getActivity().getContentResolver(),Settings.Global.AIRPLA…

消解原理推理_什么是推理統計中的Z檢驗及其工作原理?

消解原理推理I Feel:我覺得&#xff1a; The more you analyze the data the more enlightened, data engineer you will become.您對數據的分析越多&#xff0c;您將變得越發開明。 In data engineering, you will always find an instance where you need to establish whet…

pytest+allure測試框架搭建

https://blog.csdn.net/wust_lh/article/details/86685912 https://www.jianshu.com/p/9673b2aeb0d3 定制化展示數據 https://blog.csdn.net/qw943571775/article/details/99634577 環境說明&#xff1a; jdk 1.8 python 3.5.3 allure-commandline 2.13.0 文檔及下載地址&…

lintcode433 島嶼的個數

島嶼的個數 給一個01矩陣&#xff0c;求不同的島嶼的個數。 0代表海&#xff0c;1代表島&#xff0c;如果兩個1相鄰&#xff0c;那么這兩個1屬于同一個島。我們只考慮上下左右為相鄰。 您在真實的面試中是否遇到過這個題&#xff1f; Yes樣例 在矩陣&#xff1a; [[1, 1, 0, …

大數據分析要學習什么_為什么要學習數據分析

大數據分析要學習什么The opportunity to leverage insights from data has never been greater.利用來自數據的洞察力的機會從未如此大。 Humans tend to generate a lot of data each day - from heart rates to favorite songs, fitness goals and movie preferences. You …

POJ - 3257 Cow Roller Coaster (背包)

題目大意&#xff1a;要用N種材料建一條長為L的路&#xff0c;如今給出每種材料的長度w。起始地點x。發費c和耐久度f 問&#xff1a;在預算為B的情況下&#xff0c;建好這條路的最大耐久度是多少 解題思路&#xff1a;背包問題 dp[i][j]表示起始地點為i。發費為j的最大耐久度…

leetcode 1473. 粉刷房子 III(dp)

在一個小城市里&#xff0c;有 m 個房子排成一排&#xff0c;你需要給每個房子涂上 n 種顏色之一&#xff08;顏色編號為 1 到 n &#xff09;。有的房子去年夏天已經涂過顏色了&#xff0c;所以這些房子不需要被重新涂色。 我們將連續相同顏色盡可能多的房子稱為一個街區。&a…

大學生信息安全_給大學生的信息

大學生信息安全You’re an undergraduate. Either you’re graduating soon (like me) or you’re in the process of getting your first college degree. The process is not easy and I can only assume how difficult the pressures on Masters and Ph.D. students are. Ho…

打破冷漠僵局文章_保持冷靜并打破僵局-最佳

打破冷漠僵局文章Hack The Box (HTB) is an online platform allowing you to test your penetration testing skills. It contains several challenges that are constantly updated. Some of them simulating real world scenarios and some of them leaning more towards a …

使用DOM Breakpoints找到修改屬性的Javascript代碼

使用Chrome開發者工具的DOM斷點功能可以讓您快速找到修改了某一個DOM元素的Javascript代碼。 在Chrome開發者工具里&#xff0c;選中想要監控的DOM元素&#xff0c;點擊右鍵&#xff0c;選擇Break on->Attributes modifications: 之后在DOM Breakpoints的tab里能看到對應的斷…

特斯拉最安全的車_特斯拉現在是最受歡迎的租車選擇

特斯拉最安全的車Have you been curious to know which cars are most popular in US and what are their typical rental fares in various cities? As the head of Product and Data Science at an emerging technology start-up, Ving Rides, these were some of the quest…

leetcode 740. 刪除并獲得點數(dp)

給你一個整數數組 nums &#xff0c;你可以對它進行一些操作。 每次操作中&#xff0c;選擇任意一個 nums[i] &#xff0c;刪除它并獲得 nums[i] 的點數。之后&#xff0c;你必須刪除每個等于 nums[i] - 1 或 nums[i] 1 的元素。 開始你擁有 0 個點數。返回你能通過這些操作…