AtCoder Beginner Contest 264

比赛链接

AtCoder Beginner Contest 264

E.Blackout 2

给出很多点(n+m2×105),有发电站和城市,以及很多边(e5×105),有 q 次删边操作,求每次操作后有电的城市数量

解题思路

并查集

离线逆序处理,即只考虑建边操作,用并查集维护有电城市的信息即可

  • 时间复杂度:O(n+m+e+q)

代码

// Problem: E - Blackout 2 // Contest: AtCoder - freee Programming Contest 2022(AtCoder Beginner Contest 264) // URL: https://atcoder.jp/contests/abc264/tasks/abc264_e // Memory Limit: 1024 MB // Time Limit: 3000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> // #define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=5e5+5; int n,m,e,q,Q[N],fa[N],sz[N],res[N]; bool v[N],st[N]; PII a[N]; int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } int main() { cin>>n>>m>>e; for(int i=1;i<=n;i++)fa[i]=i; for(int i=1;i<=n;i++)sz[i]=1; for(int i=1;i<=e;i++) { cin>>a[i].fi>>a[i].se; if(a[i].fi>a[i].se)swap(a[i].fi,a[i].se); } cin>>q; for(int i=1;i<=q;i++)cin>>Q[i],st[Q[i]]=true; for(int i=1;i<=e;i++) { if(!st[i]) { if(a[i].fi<=n&&a[i].se>n) { int x=find(a[i].fi); if(!v[x]) { v[x]=true; res[q]+=sz[x]; } } else if(a[i].se<=n) { int x=find(a[i].fi),y=find(a[i].se); if(x!=y) { if(!v[x]&&v[y]) { res[q]+=sz[x]; fa[x]=y,sz[y]+=sz[x]; } else if(!v[y]&&v[x]) { res[q]+=sz[y]; fa[y]=x,sz[x]+=sz[y]; } else fa[y]=x,sz[x]+=sz[y]; } } } } for(int i=q;i;i--) { int id=Q[i]; int t=res[i]; if(a[id].fi<=n&&a[id].se>n) { int x=find(a[id].fi); if(!v[x]) { v[x]=true; t+=sz[x]; } } else if(a[id].se<=n) { int x=find(a[id].fi),y=find(a[id].se); if(x!=y) { if(!v[x]&&v[y]) { t+=sz[x]; fa[x]=y,sz[y]+=sz[x]; } else if(!v[y]&&v[x]) { t+=sz[y]; fa[y]=x,sz[x]+=sz[y]; } else fa[y]=x,sz[x]+=sz[y]; } } res[i-1]=t; } for(int i=1;i<=q;i++)cout<<res[i]<<'\n'; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16585066.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示