HDU 5923Prediction
来源:互联网 发布:windows10服务极限优化 编辑:程序博客网 时间:2024/05/16 19:48
There is a graph G=⟨VG,EG⟩ with |VG|=n and |EG|=m , and a magic tree T=⟨VT,ET⟩) rooted at 1, which contains m vertices.
Each vertex of the magic tree corresponds to an edge in the original graph G and each edge occurs in the magic tree exactly once.
Each query includes a setS(S⊆VT) , and you should tell Mr. Frog the number of components in the modified graph G‘=(VG,E‘G) , where E‘G is a set of edges in which every edge corresponds to a vertex v in magic tree T satisfying at least one of the following two conditions:
∙v∈S .
∙ v is an ancestor of some vertices in S.
Note that the queries are independent, and namely one query will not influence another.
Each vertex of the magic tree corresponds to an edge in the original graph G and each edge occurs in the magic tree exactly once.
Each query includes a set
Note that the queries are independent, and namely one query will not influence another.
For each test case, the first line contains two integers n and m(
The second line contains m - 1 integers describing the magic tree, i-th integer represents the parent of the (i + 1)-th vertex.
Then the following m lines describe the edges of the graph G. Each line contains two integers u and v indicating the two ends of the edge.
The next line contains only one integer q(
Then the following q lines represent queries, i-th line represents the i-th query, which contains an integer
It is guarenteed that
For each query, output a single line containing only one integer representing the answer, namely the number of components.
15 41 1 31 22 33 44 531 22 2 32 2 4
Case #1:321
magic tree and the original graph in the sample are: In the first query, S = {2} and the modified graph G' = {{1, 2, 3, 4}, {(1, 2), (2, 3)}}, thus the number of the components in the modified graph is 3.In the second query, S = {1, 2, 3}, where 1 is the ancestor of 2 (and 3) in the magic tree, and the modified graph G'' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3, 4)}}, therefore the number of the components in the modified graph is 2.In the third query, S = {1, 2, 3, 4}, where 1 is the ancestor of 2 (and 4), 3 is the ancestor of 4, and the modified graph G' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3,4), (4, 5)}}, therefore the answer equals to 1.
预处理树上节点的并查集,询问合并并查集即可
#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef pair<int,int> pii;const int N = 2e4 + 10;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int low(int x) { return x&-x; }int T, n, m, x, y,t;int ft[N], nt[N], u[N], sz;int a[N], b[N];int fa[10005][505];int get(int x,int y) {return y==fa[x][y]?y:fa[x][y] = get(x, fa[x][y]);}void dfs(int x,int y) {for (int i = 1;i<=n;i++) {fa[x][i] = fa[y][i];}int fx = get(x, a[x]);int fy = get(x, b[x]);if (fx != fy) {if (fx < fy) swap(fx,fy);fa[x][fx] = fy;}for (int i = ft[x];~i;i=nt[i]) {dfs(u[i], x);}}int main(){int cas = 1;for (scanf("%d",&T);T--;cas++) {scanf("%d%d",&n,&m);for (int i = 1;i<=m;i++) ft[i] = -1;sz = 0;for (int i = 2;i<=m;i++) {scanf("%d",&x);u[sz] = i; nt[sz] = ft[x]; ft[x] = sz++;}for (int i = 1;i<=m;i++) {scanf("%d%d",&a[i], &b[i]);}for (int i = 1;i<=n;i++) fa[0][i] = i;dfs(1,0);scanf("%d",&t);printf("Case #%d:\n",cas);while (t--) {scanf("%d",&x);for (int i = 1;i<=n;i++) fa[0][i]=i;int ans = n;while (x--) {scanf("%d",&y);for (int i = 1;i<=n;i++) {if (ans == 1) break;int fx = get(0,i);int fy = get(y,i);int fz = get(0,fy);if (fx == fz) continue;if (fx < fz) swap(fx,fz);fa[0][fx] = fz;ans--;}}printf("%d\n",ans);}}return 0;}
阅读全文
0 0
- HDU 5923Prediction
- HDU 5923 Prediction 并查集 合并
- HDU 5923 Prediction (并查集)
- [HDU 5923]Prediction:并查集
- hdu 1338Game Prediction
- hdu 1338 Game Prediction
- HDU 1338 Game Prediction
- HDU -- 1338 Game Prediction
- hdu 1338 Game Prediction
- hdu 1338 Game Prediction
- HDU 1338 Game Prediction
- hdu 1338Game Prediction
- hdu 1338 Game Prediction
- HDU 1338 Game Prediction
- HDU 5923 Prediction [可持久化并查集]【数据结构】
- HDU-5923 Prediction(并查集/暴力)
- 贪心 hdu-1338-Game Prediction
- HDU 1338 Game Prediction 贪心
- windows系统下派克斯(PacketiX)产品使用教程
- 抓包工具Charles
- Synopsys VCS vY-2006.06
- Class for the use of RegQueryInfoKey pinvoke interop in C#
- SpringMVC(十二)Restful架构风格
- HDU 5923Prediction
- linux mysql 重启
- 基于深度学习的目标检测技术演进:R-CNN、Fast R-CNN、Faster R-CNN【转】
- logger4j输出日志信息
- RSA初步记录
- Android 网络框架之Retrofit2使用详解及从源码中解析原理
- 动态存储空间管理示例——比较两个数大小
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- SQL Server 2017 数据库开发工具介绍