Ural 1671. Anansi's Cobweb(并查集)
来源:互联网 发布:精通d3.js pdf百度云 编辑:程序博客网 时间:2024/05/20 05:31
题目链接:点击打开链接
思路:
由于我们对删除边之后的状态很难处理, 我们可以考虑对询问倒着处理, 转化成对集合的合并问题。
细节参见代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <stack>#include <ctime>#include <bitset>#include <cstdlib>#include <cmath>#include <set>#include <list>#include <deque>#include <map>#include <queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const double eps = 1e-6;const double PI = acos(-1);const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;const int maxn = 1e5 + 10;int T,n,m,q,p[maxn],vis[maxn],u[maxn],v[maxn],op[maxn];int _find(int x) { return p[x] == x ? x : p[x] = _find(p[x]); }int main() { while(~scanf("%d%d",&n, &m)) { for(int i = 1; i <= m; i++) { scanf("%d%d", &u[i], &v[i]); vis[i] = 0; } scanf("%d", &q); for(int i = 1; i <= q; i++) { scanf("%d", &op[i]); vis[op[i]] = 1; } for(int i = 1; i <= n; i++) p[i] = i; int ans = n; for(int i = 1; i <= m; i++) { if(vis[i]) continue; int x = _find(u[i]); int y = _find(v[i]); if(x != y) p[x] = y, --ans; } vector<int> res; for(int i = q; i >= 1; i--) { res.push_back(ans); int id = op[i]; int x = _find(u[id]); int y = _find(v[id]); if(x != y) { p[x] = y; --ans; } } for(int i = q-1; i >= 0; i--) { printf("%d%c", res[i], i == 0 ? '\n' : ' '); } } return 0;}
0 0
- Ural 1671. Anansi's Cobweb(并查集)
- Ural 1671. Anansi's Cobweb(并查集)
- URAL 1671 Anansi's Cobweb (并查集)
- ural 1671 Anansi's Cobweb-并查集
- Ural 1671. Anansi's Cobweb
- Ural 1671 - Anansi's Cobweb 倒过来做...并查集维护..
- URAL1671 Anansi's Cobweb(离线做 + 并查集)
- Ural 1671 Anansi's Cobweb
- Ural 1671 Anansi's Cobweb
- Ural 1003 Parity(并查集)
- poj1733 & Ural 1003 Parity Game (hash+并查集 )
- URAL 1982. Electrification Plan(并查集)
- Pilot Work Experience (URAL 1888 并查集+floyd)
- ural 1982. Electrification Plan 并查集
- ural 1003 Parity 并查集
- URAL 1003 Parity(并查集)
- Ural 1682 Crazy Professor (并查集)
- ural 1003. Parity(并查集)
- UVaOJ 10048 Audiophobia(最小化最短路的最大权值边)
- 【Qt开发】Qt5.7串口开发
- 哈希技术
- Android 事务 SQLite 数据(参考《第一行代码》)
- cocos2dx学习之路--拼图游戏
- Ural 1671. Anansi's Cobweb(并查集)
- STAR法则的简历应用
- 关于eclipse环境开发软件的使用
- ubuntu下vgg16预训练模型转换为mxnet格式
- Xcode打包ipa的基本步骤
- 基于索引的SQL语句优化之降龙十八掌(转载)!
- maven工程添加本地工程库
- java策略模式练习
- 左老师容器接水