Codeforces Gym 100345H Settling the Universe Up Bitset+倒推

编程入门 行业动态 更新时间:2024-10-17 17:27:35

Codeforces <a href=https://www.elefans.com/category/jswz/34/1755862.html style=Gym 100345H Settling the Universe Up Bitset+倒推"/>

Codeforces Gym 100345H Settling the Universe Up Bitset+倒推

题目大意:

给一个只从标号小的点向标号大的点连边的有向图(n<=200)。有如下几种操作:
? u v(询问u能否到v)
+ u v (加一条u到v的边)
- u v (删除u到v的边)
以上操作保证u < v,且合法。在操作之前需要给出有多少对(u,v)能够到达,在每次+和-的操作后也要回答上面的问题。
操作数<=100 000,修改操作<=1000

做法:

  • 用bitset维护每个点能到的点
  • 对于每一次?操作可以 O(1) 回答
  • 对于每一次修改操作从后往前推暴力重构bitset并统计答案,时间复杂度 O((n+m)∗n)

代码:

4541485WHU_FFTHAccepted852746GNU G++ 4.9.2
#include <bits/stdc++.h>
using namespace std;
const int maxn=202;
set<int>g[maxn];
set<int>::iterator it;
bitset<maxn>b[maxn];
char op[10];
int n;
int getans()
{int ans=0;for(int i=n;i>0;i--){b[i].reset();for(it=g[i].begin();it!=g[i].end();it++){b[i].set(*it);b[i] |= b[*it];}ans+=b[i].count();}return ans;
}
//倒推暴力重构bitset 
int main()
{freopen("settling.in","r",stdin);freopen("settling.out","w",stdout);int m,t,u,v;scanf("%d%d",&n,&m);for(int i=0;i<m;i++){scanf("%d%d",&u,&v);g[u].insert(v);}scanf("%d",&t);printf("%d\n",getans());while(t--){scanf("%s %d %d",op,&u,&v);if(op[0]=='?')printf("%s\n",(b[u][v])?("YES"):("NO"));else{if(op[0]=='+')g[u].insert(v);elseif(op[0]=='-')g[u].erase(v);printf("%d\n",getans());}}return 0;
}

更多推荐

Codeforces Gym 100345H Settling the Universe Up Bitset+倒推

本文发布于:2024-02-06 09:39:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1748303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Gym   Codeforces   Bitset   Universe   Settling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!