当前位置:网站首页>P6118 [joi 2019 final] solution to the problem of Zhenzhou City
P6118 [joi 2019 final] solution to the problem of Zhenzhou City
2022-07-28 02:49:00 【wkh2021】
A summary of the problem
There is a tree with N N N A tree at the top .
Align with the vertex x x x Vertices with a unique distance become unique .
For each vertex, answer the number of tag types of unique cities .
about 4 % 4\% 4% The data of
Do for each vertex DFS or BFS.
Take a and vertex x x x The vertex with the greatest distance , Set to D D D, The unique feature is ( D , x ) (D,x) (D,x) On the path , If any , about ( D , x ) (D,x) (D,x) route , Find a subtree that branches on the way , For the maximum depth of the branching subtree , Candidate unique cities disappear , The last remaining vertices are unique .
By the way , Give Way D 1 D1 D1 and D 2 D2 D2 Is the endpoint of the diameter of the tree ( The two points with the longest distance are right ).
For any vertex x x x, D 1 D1 D1 and D 2 D2 D2 At least one of them is Li x x x The farthest vertex ,
From the top D 1 D1 D1 Conduct DFS To determine each vertex x x x Whether in ( D 1 , x ) (D1,x) (D1,x) There are unique characteristics in the path , Yes D 2 D2 D2 Perform the same DFS To get the correct answer for each vertex ,
From the maximum depth column of each subtree, we can see ( D , x ) (D,x) (D,x) Unique features on the path , Each time through DFS One side of , This column will change the number of constant elements after it .
Set this column as A A A, The problem is :
Given many changes A A A Query for ( But only the last element ), For each p ( 1 ≤ p ≤ ∣ A ∣ ) , p − A p ≤ j < p p(1 \leq p \leq |A|),p-A_{p} \leq j<p p(1≤p≤∣A∣),p−Ap≤j<p, When satisfied j j j When disabled , Not forbidden j j j Set ∣ A ∣ |A| ∣A∣ Set to S S S, S S S The element corresponds to a unique feature .
Complexity O ( N 2 ) O(N^2) O(N2).
about 36 % 36\% 36% The data of
There is only one type of vertex label , Judge whether there are unique characteristics , Just judge S S S Is it empty .
When they arrive in ( D , x ) (D,x) (D,x) The vertex of the path x x x when , Keep the row with the maximum depth of the subtree growing from outside ( Namely reservation A A A The last line ),
Let's call it A ′ A' A′, By comparison S S S The minimum value of is from x x x The maximum depth of the growing subtree to find the answer ,
Find the maximum depth of each subtree in advance , Add to... As you go down the edge A ′ A' A′ At the end is the maximum depth of the subtree brother of the subtree that just fell .
S S S The minimum value of is the original set or ∣ A ′ ∣ + 1 |A'|+1 ∣A′∣+1, Sure O ( 1 ) O(1) O(1) Go down once ,
Complexity O ( N ) O(N) O(N).
about 68 % 68\% 68% The data of
All labels are different , Just need to know ∣ S ∣ |S| ∣S∣ Size .
For each p ( 1 ≤ p ≤ ∣ A ∣ ) p(1 \leq p \leq |A|) p(1≤p≤∣A∣) Don't use satisfaction p − A p ≤ j < p p-A_{p} \leq j<p p−Ap≤j<p Of j j j,
however , Prepare auxiliary array B B B, For each p ( 1 ≤ p ≤ ∣ A ∣ ) , B j ← B j + 1 p(1 \leq p \leq |A|),B_{j} \gets B_{j}+1 p(1≤p≤∣A∣),Bj←Bj+1 among j j j Satisfy p − A p ≤ j < p p-A_{p} \leq j<p p−Ap≤j<p, then , Calculation j j j bring B j = 0 B_{j}=0 Bj=0.
A A A Element modification corresponding to B Interval addition query , If B B B The sum of the interval addition of becomes 0 0 0 The counting of elements can be completed at a high speed, which is better .
because B B B The element of is always 0 0 0 Or more , So just check min { B } = 0 \min\{B\}=0 min{ B}=0 And calculate m i n min min The elements of .
It can be done through the line segment tree , O ( log N ) O(\log N) O(logN) Updated once ,
The update is N N N Time , So overall O ( N log N ) O(N\log N) O(NlogN).
about 100 % 100\% 100% The data of
change A A A Query for ( But only the last element ), When it comes to vertices x x x when , It holds A ′ A' A′ From the top x x x Return to the parent node , Keep A A A.
There are some questions in the question : “ When you walk down , Add to A ′ A' A′ At the end is the maximum depth of the subtree of your brother who just dropped the subtree .”
Add to A ′ A' A′ The elements at the end increase monotonically , When moving to DFS When the brothers in the tree , see A A A The change of the last element , except p p p Other elements may come from S S S Disappear but not increase ,
When returning to the vertex of the parent node , If you look at A A A The change of the last element , S S S The elements of may disappear but will not increase .
It can be seen from the above that , S S S The number of times the element of is increased is N N N Time , therefore , Reduce the number of times by O ( N ) O(N) O(N) To achieve .
S S S Operations on can be represented by stacks , So every time O ( 1 ) O(1) O(1),
When S S S When the elements of increase or decrease , You can update the number of label types by changing the label set of the corresponding vertex .
Complexity O ( N ) O(N) O(N).
AC code:
#include <bits/stdc++.h>
using namespace std;
using ll=int64_t;
#define int ll
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) x.begin(),x.end()
auto& errStream=cerr;
#ifdef LOCAL
#define cerr (cerr<<"-- line "<<__LINE__<<" -- ")
#else
class CerrDummy {
} cerrDummy;
template<class T>
CerrDummy& operator<<(CerrDummy&cd,const T&) {
return cd;}
using charTDummy=char;
using traitsDummy=char_traits<charTDummy>;
CerrDummy& operator<<(CerrDummy&cd,basic_ostream<charTDummy,traitsDummy>&(basic_ostream<charTDummy,traitsDummy>&)) {
return cd;}
#define cerr cerrDummy
#endif
#define REACH cerr<<"reached"<<endl
#define DMP(x) cerr<<#x<<":"<<x<<endl
#define ZERO(x) memset(x,0,sizeof(x))
#define ONE(x) memset(x,-1,sizeof(x))
using pi=pair<int,int>;
using vi=vector<int>;
using ld=long double;
template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p) {
os<<"("<<p.first<<","<<p.second<<")";return os;}
template<class T>
ostream& operator <<(ostream& os,const vector<T>& v) {
os<<"{";REP(i,(int)v.size()) {
if(i)os<<",";os<<v[i];}os<<"}";return os;}
inline ll read() {
ll i;scanf("%" SCNd64,&i);return i;}
inline void printSpace() {
printf(" ");}
inline void printEoln() {
printf("\n");}
inline void print(ll x,int suc=1) {
printf("%" PRId64,x);if(suc==1)printEoln();if(suc==2)printSpace();}
inline string readString() {
static char buf[3341000];scanf("%s",buf);return string(buf);}
inline char* readCharArray() {
static char buf[3341000];
static int bufUsed=0;
char* ret=buf+bufUsed;
scanf("%s",ret);
bufUsed+=strlen(ret)+1;
return ret;
}
template<class T,class U>
inline void chmax(T& a,U b) {
if(a<b)a=b;}
template<class T,class U>
inline void chmin(T& a,U b) {
if(b<a)a=b;}
template<class T>
inline T Sq(const T& t) {
return t*t;}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
const int Nmax=200010;
vi tr[Nmax];
int ans[Nmax];
int col[Nmax],cnt[Nmax],curAns,stBuf[Nmax],stS;
inline void AddCol(int c) {
if(cnt[c]==0)curAns++;cnt[c]++;}
inline void DelCol(int c) {
cnt[c]--;if(cnt[c]==0)curAns--;}
inline void Push(int v) {
stBuf[stS++]=v;AddCol(col[v]);}
inline bool Empty() {
return stS==0;}
inline int Last() {
return stBuf[stS-1];}
inline void Pop() {
DelCol(col[Last()]);stS--;}
inline void dfs1(int v,int p,int d,vi&dist) {
dist[v]=d;
for(auto to:tr[v])if(to!=p)dfs1(to,v,d+1,dist);
}
int dep[Nmax];
inline int dfs2(int v,int p) {
int res=0;
for(auto to:tr[v])if(to!=p)chmax(res,dfs2(to,v));
return dep[v]=res+1;
}
inline void dfs3(int v,int p,const vi&dist) {
vector<pi> ch;
for(auto to:tr[v]) if(to!=p)ch.EB(dep[to],to);
if(!ch.empty()) {
swap(ch[0],*max_element(ALL(ch)));
int len=ch.size()>1?max_element(ch.begin()+1,ch.end())->first:0;
for(auto c:ch) {
int to=c.second;
while(!Empty() && dist[Last()]>=dist[v]-len) Pop();
Push(v);
dfs3(to,v,dist);
if(!Empty() && Last()==v) Pop();
chmax(len,c.first);
}
while(!Empty() && dist[Last()]>=dist[v]-len) Pop();
}
chmax(ans[v],curAns);
}
inline void Solve(int root,const vi&dist) {
assert(Empty());
dfs2(root,-1);
dfs3(root,-1,dist);
}
signed main() {
int n=read(),k=read();
int root;
REP(_,n-1) {
int a=read()-1,b=read()-1;
tr[a].PB(b);
tr[b].PB(a);
}
REP(i,n) col[i]=read()-1;
vi dist(n);
dfs1(0,-1,0,dist);
root= max_element(ALL(dist)) - dist.begin();
dfs1(root,-1,0,dist);
Solve(root,dist);
root= max_element(ALL(dist)) - dist.begin();
dfs1(root,-1,0,dist);
Solve(root,dist);
REP(i,n) print(ans[i]);
return 0;
}
边栏推荐
- Which users are suitable for applying for rapidssl certificate
- [in depth study of 4g/5g/6g topic -42]: urllc-14 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -8-low delay technology-2-slot based scheduling and
- Is it safe to buy funds on Alipay? I want to make a fixed investment in the fund
- Deep residual learning for image recognition shallow reading and Implementation
- [data processing] boxplot drawing
- [brother hero's July training] day 27: picture
- Introduction to the reduce() function in JS
- 入职华为od一个月的感受
- mysql: error while loading shared libraries: libtinfo.so. 5 solutions
- JS event loop synchronous task, asynchronous task (micro task, macro task) problem analysis
猜你喜欢

Redis AOF log persistence

IO流:节点流和处理流详细归纳。

TypeScript(零) —— 简介、环境搭建、第一个实例

Pycharm 快速给整页全部相同名称修改的快捷键

Is the interface that can be seen everywhere in the program really useful? Is it really right?

基于FPGA的64位8级流水线加法器

Find - block search

Canonical Address

How do you use the jar package sent by others (how to use the jar package sent by others)

初识C语言 -- 结构体,分支和循环语句
随机推荐
[brother hero's July training] day 26: check the collection
[data processing] boxplot drawing
Canonical Address
Use try-with-resources or close this
怎么简单实现菜单拖拽排序的功能
On the problem that sqli labs single quotation marks do not report errors
0动态规划中等 LeetCode873. 最长的斐波那契子序列的长度
Flutter神操作学习之(满级攻略)
Find - block search
Pytorch optimizer settings
Retainface use error: modulenotfounderror: no module named'rcnn.cyton.bbox'
[in depth study of 4g/5g/6g topic -42]: urllc-14 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -8-low delay technology-2-slot based scheduling and
CNN循环训练的解释 | PyTorch系列(二十二)
[wechat applet development (VI)] draw the circular progress bar of the music player
IO流:节点流和处理流详细归纳。
[wechat applet development (V)] the interface is intelligently configured according to the official version of the experience version of the development version
Canvas from getting started to persuading friends to give up (graphic version)
Typescript (zero) -- introduction, environment construction, first instance
特征值和特征向量
【TA-霜狼_may-《百人计划》】图形3.7 移动端TP(D)R架构