Description
给T组数据。
每组数据给你一个n个点的无向图,保证图联通,给q个询问。
每个询问给k个节点,问每一次询问中,求有多少个点在断掉他之后可以使图中两个点不连通。


Sample Input
2
7 6
1 2
1 3
2 4
2 5
3 6
3 7
3
2 1 2
3 2 3 4
4 4 5 6 7
6 6
1 2
1 3
2 3
1 4
2 5
3 6
4
3 1 2 3
3 1 2 6
3 1 5 6
3 4 5 6


Sample Ouput
0
1
3
0
1
2
3


首先点双缩一下,建个圆方树。
答案其实就是路径中圆点的数量
然后你就按照虚树的做法。
按dfs序排一下序,算一下相邻两个点的的答案,除一下2,差不多就这样吧。


#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;
int _min(int x, int y) {return x < y ? x : y;}
int read() {int s = 0, f = 1; char ch = getchar();while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();return s * f;
}struct edge {int x, y, next;
} e[410000], e2[410000]; int len, len2, last[110000], last2[210000];
int belong[110000], ll[210000], dep[210000], fa[210000][20], dis[210000];
int id, cnt, tp, sta[110000], low[110000], dfn[110000];
vector<int> q[110000];
int n, m, a[110000];
bool cut[110000];bool cmp(int x, int y) {return ll[x] < ll[y];}void ins(int x, int y) {e[++len].x = x; e[len].y = y;e[len].next = last[x]; last[x] = len;
}void ins2(int x, int y) {e2[++len2].x = x; e2[len2].y = y;e2[len2].next = last2[x]; last2[x] = len2;
}void tarjan(int x) {dfn[x] = low[x] = ++id; sta[++tp] = x;for(int k = last[x]; k; k = e[k].next) {int y = e[k].y;if(!dfn[y]) {tarjan(y);low[x] = _min(low[x], low[y]);if(low[y] >= dfn[x]) {int i; cnt++;do {i = sta[tp--];q[cnt].push_back(i);} while(i != y); q[cnt].push_back(x);}} else low[x] = _min(low[x], dfn[y]);}
}void dfs(int x) {ll[x] = ++id;dis[x] = dis[fa[x][0]] + (x <= n);for(int i = 1; (1 << i) <= dep[x]; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];for(int k = last2[x]; k; k = e2[k].next) {int y = e2[k].y;if(y != fa[x][0]) fa[y][0] = x, dep[y] = dep[x] + 1, dfs(y);}
}int LCA(int x, int y) {if(dep[x] > dep[y]) swap(x, y);for(int i = 18; i >= 0; i--) if(dep[y] - dep[x] >= (1 << i)){y = fa[y][i];} if(x == y) return x;for(int i = 18; i >= 0; i--) if(fa[x][i] != fa[y][i]){x = fa[x][i], y = fa[y][i];} return fa[x][0];
}int main() {int tt = read();while(tt--) {n = read(), m = read();for(int i = 1; i <= cnt; i++) q[i].clear();len = 0; memset(last, 0, sizeof(last));memset(cut, 0, sizeof(cut));memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));id = tp = cnt = 0;for(int i = 1; i <= m; i++) {int x = read(), y = read();ins(x, y), ins(y, x);} tarjan(1);len2 = 0; memset(last2, 0, sizeof(last2));for(int i = 1; i <= cnt; i++) {int o = i + n;for(int j = 0; j < q[i].size(); j++) {int x = q[i][j];ins2(o, x), ins2(x, o);}} memset(fa, 0, sizeof(fa));id = 0; dep[1] = dis[1] = 0; dfs(1);int q = read();for(int i = 1; i <= q; i++) {int k = read(), ans = -2 * k, lca;for(int j = 1; j <= k; j++) a[j] = read();sort(a + 1, a + k + 1, cmp);for(int j = 2; j <= k; j++) {int x = a[j], y = a[j - 1];lca = LCA(x, y);ans += dis[x] + dis[y] - dis[lca] * 2;} lca = LCA(a[1], a[k]);ans += dis[a[1]] + dis[a[k]] - dis[lca] * 2;ans /= 2;if(dis[lca] != dis[fa[lca][0]]) ans++;printf("%d\n", ans);}}return 0;
}

[SDOI2018]战略游戏 圆方树+虚树相关推荐

  1. [BZOJ5329][Sdoi2018]战略游戏 圆方树+虚树

    5329: [Sdoi2018]战略游戏 Time Limit: 30 Sec  Memory Limit: 512 MB Submit: 174  Solved: 109 [Submit][Stat ...

  2. BZOJ5329:[SDOI2018]战略游戏(圆方树,虚树)

    Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任意一个城市出发总能沿着 ...

  3. Luogu4606 SDOI2018 战略游戏 圆方树、虚树、链并

    传送门 弱化版 考虑到去掉一个点使得存在两个点不连通的形式类似割点,不难想到建立圆方树.那么在圆方树上对于给出的关键点建立虚树之后,我们需要求的就是虚树路径上所有圆点的数量减去关键点的数量. 因为没有 ...

  4. LuoguP4606 [SDOI2018]战略游戏

    LuoguP4606 [SDOI2018]战略游戏 题目描述 题目描述 省选临近,放飞自我的小 QQ 无心刷题,于是怂恿小 CC 和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由 nn 个城市 ...

  5. [BZOJ5329][SDOI2018]战略游戏

    bzoj luogu Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任 ...

  6. [bzoj5329][圆方树][虚树]战略游戏

    Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任意一个城市出发总能沿着 ...

  7. BZOJ5329: [SDOI2018]战略游戏——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5329 https://www.luogu.org/problemnew/show/P4606 省选 ...

  8. [SDOI2018]战略游戏

    题目描述 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任意一个城市出发总能沿着道路走到 任意 ...

  9. 【题解】SDOI2018战略游戏

    被CNST的大小卡了好久.一定要开到18呀-- 首先,遇到这种带各种各样环的图先考虑是不是可以建立圆方树,在圆方树上求出答案.然后转化为圆方树之后,我们就将图转化到了树上.答案非常的明显:只要一个圆点 ...

最新文章

  1. js-----第四篇
  2. 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
  3. Linux服务器集群系统(三)--转
  4. ASIO协程彻底转变你的思维
  5. spring mvc-使用Servlet原生API作为参数
  6. 沟通模型包含5个状态
  7. alxc tool 报错数组超出了界限_代码审计之报错信息泄露与字符串截断
  8. Oracle笔记(一) Oracle简介及安装
  9. Linux 服务器为什么被黑
  10. android自动化(appium)
  11. 批量删除.svn文件的方法
  12. spring boot first
  13. 音乐直链php,【原创】百度音乐直链 + 实现方法
  14. 什么是单片机,什么是51单片机【51单片机介绍】
  15. c#将byte转为int_C# int转byte[],byte[]转int
  16. c++ 11 中显式默认设置的函数和已删除的函数 总结
  17. 事件图谱的构建、推理与应用
  18. 计算机协会活动策划,计算机协会活动策划书
  19. Scala——面向对象和函数式编程语言
  20. 径向基函数神经网络_神经网络

热门文章

  1. Go设置、获取和删除Cookie
  2. pygame飞机大战用精灵组(sprite)的层(layer)编写(二)BOSS登场了
  3. 3G技术的宠儿CDMA2000 1X
  4. 网络安全 - Harden CISCO Devices
  5. matlab 分水岭函数,【填空题】用来实现分水岭算法的MATLAB函数是 ____________
  6. jQuery超酷苹果3D音乐专辑封面CoverFlow特效
  7. 游戏盾怎么样,什么是游戏盾,必看
  8. 名画09 韩干《十六神骏图卷》
  9. Linux:搭建深度学习环境配置教程
  10. 找工作:社招你会遇到的坑!校招你了解多少?(全面干货)