正文
增加 153 行删除 0 行
71 行未改动727374757677787922 行未改动10210310410510610710810917 行未改动71 行未改动时间复杂度 $O(n \log n)$(若认为 $n, k$ 同阶),可以通过全部测试数据。对于部分分:度数不超过 $5$ 则菊花数量与 $n$ 同阶,可以暴力计算;整个树是个菊花则无需全局堆。# T3要想最小化代价,就是要尽量把 `+` 边的端点放入同一个集合,`-` 边的端点放入不同的集合。22 行未改动3. 树上的 `-` 边。使用并查集模拟这个过程即可得到各个 $k$ 的答案。但是实际上我们计算贡献不必须用并查集——凡是合并 `+` 均 $-1$,凡是合并 `-` 均 $+1$。但在合并环上的第 $s-1$ 条边时需要额外注意一下(假设环的大小为 $s$),如果整个环全是 `+` 那么代价额外 $-1$,否则代价额外 $+1$。找出环上的所有边并记录有多少 `-` 这件事可以用 DFS 实现,因此放到普及组也是不超纲的(。# T4题目已经对所有 $S_i$ 建出 Trie,则 LCP 长度就是两个点在 Trie 上的 LCA 深度,于是两点之间的边权仅与两点本身以及 LCA 有关。17 行未改动71 行未改动7273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214322 行未改动16616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126217 行未改动71 行未改动时间复杂度 $O(n \log n)$(若认为 $n, k$ 同阶),可以通过全部测试数据。对于部分分:度数不超过 $5$ 则菊花数量与 $n$ 同阶,可以暴力计算;整个树是个菊花则无需全局堆。## Code```cpp#include<bits/stdc++.h>#define int long long#define f(i,j,k) for(int i=j;i<=k;i++)#define F(i,j,k) for(int i=j;i>=k;i--)char Buf[1<<23],*P1=Buf,*P2=Buf;#define getchar() (P1==P2&&(P2=(P1=Buf)+fread(Buf,1,1<<23,stdin),P1==P2)?EOF:*P1++)const int maxn=3e5+10;inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}inline double readDouble(){double x=0; int f=1,cnt=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();if(ch=='.'){ch=getchar();while (ch>='0'&&ch<='9') x+=(ch-48)/std::pow(10,cnt),ch=getchar(),cnt++;}return x*f;}inline void write(int x){if(x<0) {x=~(x-1); putchar('-');}if(x>9) write(x/10);putchar(x%10+'0');}int n,k,a[maxn],u,v,ans;std::vector<int> vec[maxn];inline bool cmp(int x,int y){return a[x]>a[y];}struct node{int id,pointerL,pointerR,Rlimit,val;inline bool operator <(const node &x) const{return val<x.val;}};std::priority_queue<node> q[maxn];inline void work(){freopen("b.in","r",stdin);freopen("b.out","w",stdout);n=read(); k=read();f(i,1,n) a[i]=read();f(i,1,n-1) u=read(),v=read(),vec[u].push_back(v),vec[v].push_back(u);f(i,1,n){std::sort(vec[i].begin(),vec[i].end(),cmp);if(vec[i].size()<3) continue;int sum=a[i]+a[vec[i][0]]+a[vec[i][1]];f(j,2,(int)vec[i].size()-1) sum+=a[vec[i][j]],q[i].push({i,j-1,j,(int)vec[i].size(),sum});q[0].push(q[i].top());}f(i,1,k){auto p=q[0].top(); q[0].pop(); ans^=std::abs(p.val); q[p.id].pop();if(p.pointerR+1<p.Rlimit) q[p.id].push({p.id,p.pointerL,p.pointerR+1,p.Rlimit,p.val-a[vec[p.id][p.pointerR]]+a[vec[p.id][p.pointerR+1]]});if(p.pointerL>=0&&p.pointerL+1<p.pointerR) q[p.id].push({p.id,p.pointerL-1,p.pointerL+1,p.pointerR,p.val-a[vec[p.id][p.pointerL]]+a[vec[p.id][p.pointerL+1]]});if(!q[p.id].empty()) q[0].push(q[p.id].top());}write(ans);}signed main(){work();return !!!!!("YZren");}```# T3要想最小化代价,就是要尽量把 `+` 边的端点放入同一个集合,`-` 边的端点放入不同的集合。22 行未改动3. 树上的 `-` 边。使用并查集模拟这个过程即可得到各个 $k$ 的答案。但是实际上我们计算贡献不必须用并查集——凡是合并 `+` 均 $-1$,凡是合并 `-` 均 $+1$。但在合并环上的第 $s-1$ 条边时需要额外注意一下(假设环的大小为 $s$),如果整个环全是 `+` 那么代价额外 $-1$,否则代价额外 $+1$。找出环上的所有边并记录有多少 `-` 这件事可以用 DFS 实现,因此放到普及组也是不超纲的(。## Code```cpp#include<bits/stdc++.h>#define int long long#define f(i,j,k) for(int i=j;i<=k;i++)#define F(i,j,k) for(int i=j;i>=k;i--)char Buf[1<<23],*P1=Buf,*P2=Buf;#define getchar() (P1==P2&&(P2=(P1=Buf)+fread(Buf,1,1<<23,stdin),P1==P2)?EOF:*P1++)const int maxn=3e5+10;inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}inline double readDouble(){double x=0; int f=1,cnt=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();if(ch=='.'){ch=getchar();while (ch>='0'&&ch<='9') x+=(ch-48)/std::pow(10,cnt),ch=getchar(),cnt++;}return x*f;}inline void write(int x){if(x<0) {x=~(x-1); putchar('-');}if(x>9) write(x/10);putchar(x%10+'0');}int n,ans,u[maxn],v[maxn],pos[maxn],cntAll,CyclicTree,sumAdd,in,deg[maxn],add,del;char ch;std::bitset<maxn> vis;struct node{int to,val;};std::vector<node> vec[maxn];std::queue<int> q;inline void topu(){while(!q.empty()){int p=q.front(); q.pop(); vis[p]=1; CyclicTree--;for(auto y:vec[p]){deg[y.to]--;if(deg[y.to]==1) q.push(y.to);}}}inline void dfs(int x){vis[x]=1;for(auto y:vec[x]) if(!vis[y.to]&&y.val) dfs(y.to);}inline void solve1(){f(i,1,cntAll) write(del--),puts("");f(i,cntAll+1,n) write(i-cntAll),puts("");}inline void solve2(){f(i,1,cntAll) write(cntAll-i),puts("");f(i,1,add-sumAdd) write(i),puts("");f(i,add-sumAdd+cntAll+2,n+1) write(i-cntAll),puts("");}inline void solve3(){write(del),puts(""),write(del-2),puts(""),del-=2;f(i,3,cntAll) write(--del),puts("");f(i,cntAll+1,n) write(++del),puts("");}inline void work(){freopen("c.in","r",stdin);freopen("c.out","w",stdout);n=read(); CyclicTree=n;f(i,1,n){u[i]=read(); v[i]=read();ch=getchar(); while(ch!='+'&&ch!='-' ) ch=getchar();vec[u[i]].push_back({v[i],ch=='+'}),vec[v[i]].push_back({u[i],ch=='+'});deg[u[i]]++; deg[v[i]]++; ch=='+'?add++:del++;}f(i,1,n) if(deg[i]==1) q.push(i);topu();f(i,1,n) if(!vis[i]) for(auto y:vec[i]) if(!vis[y.to]&&y.val) sumAdd++;sumAdd>>=1; vis=0;f(i,1,n) if(!vis[i]) dfs(i),cntAll++;if(CyclicTree-1==sumAdd) solve1();else if(CyclicTree==sumAdd) solve2();else solve3();}signed main(){work();return !!!!!("YZren");}```# T4题目已经对所有 $S_i$ 建出 Trie,则 LCP 长度就是两个点在 Trie 上的 LCA 深度,于是两点之间的边权仅与两点本身以及 LCA 有关。17 行未改动