Luogu4606 SDOI2018 战略游戏 圆方树、虚树、链并
传送门
弱化版
考虑到去掉一个点使得存在两个点不连通的形式类似割点,不难想到建立圆方树。那么在圆方树上对于给出的关键点建立虚树之后,我们需要求的就是虚树路径上所有圆点的数量减去关键点的数量。
因为没有DP,所以其实没有必要将虚树建立起来,只需要维护一个链并就可以了。
#include<bits/stdc++.h>
//This code is written by Itst
using namespace std;inline int read(){int a = 0;char c = getchar();bool f = 0;while(!isdigit(c) && c != EOF){if(c == '-')f = 1;c = getchar();}if(c == EOF)exit(0);while(isdigit(c)){a = a * 10 + c - 48;c = getchar();}return f ? -a : a;
}const int MAXN = 2e5 + 7;
int head[MAXN] , N , M , cnt , cntEd , ans;
int dfn[MAXN] , low[MAXN] , st[MAXN] , ts , top;
int ind[MAXN] , dep[MAXN] , TS;
vector < int > ch[MAXN];
int num[MAXN] , jump[MAXN][19];
struct Edge{int end , upEd;
}Ed[MAXN << 1];
struct cmp{bool operator ()(int a , int b){return ind[a] < ind[b];}
};
set < int , cmp > s;
set < int , cmp > :: iterator it1 , it2;inline void addEd(int a , int b){Ed[++cntEd].end = b;Ed[cntEd].upEd = head[a];head[a] = cntEd;
}void pop(int x , int bot){++cnt;ch[cnt].clear();ch[x].push_back(cnt);do{ch[cnt].push_back(st[top]);}while(st[top--] != bot);
}void tarjan(int x , int p){dfn[x] = low[x] = ++ts;st[++top] = x;ch[x].clear();for(int i = head[x] ; i ; i = Ed[i].upEd)if(Ed[i].end != p)if(!dfn[Ed[i].end]){tarjan(Ed[i].end , x);low[x] = min(low[x] , low[Ed[i].end]);if(low[Ed[i].end] >= dfn[x])pop(x , Ed[i].end);}elselow[x] = min(low[x] , dfn[Ed[i].end]);
}void dfs(int x , int p){ind[x] = ++TS;dep[x] = dep[p] + 1;jump[x][0] = p;for(int i = 1 ; i <= 18 ; ++i)jump[x][i] = jump[jump[x][i - 1]][i - 1];num[x] = num[p] + (x <= N);for(int i = 0 ; i < ch[x].size() ; ++i)dfs(ch[x][i] , x);
}inline int LCA(int x , int y){if(dep[x] < dep[y])swap(x , y);for(int i = 18 ; i >= 0 ; --i)if(dep[x] - (1 << i) >= dep[y])x = jump[x][i];if(x == y)return x;for(int i = 18 ; i >= 0 ; --i)if(jump[x][i] != jump[y][i]){x = jump[x][i];y = jump[y][i];}return jump[x][0];
}inline void insert(int x){s.insert(x);it1 = it2 = s.find(x);int p = 0 , q = 0;if(it1 != s.begin())p = LCA(*(--it1) , x);if(++it2 != s.end())q = LCA(*it2 , x);p = dep[p] < dep[q] ? q : p;ans += num[x] - num[p];
}int main(){
#ifndef ONLINE_JUDGEfreopen("in","r",stdin);freopen("out","w",stdout);
#endiffor(int T = read() ; T ; --T){cnt = N = read();M = read();memset(head , 0 , sizeof(head));memset(dfn , 0 , sizeof(dfn));cntEd = ts = TS = top = 0;for(int i = 1 ; i <= M ; ++i){int a = read() , b = read();addEd(a , b);addEd(b , a);}tarjan(1 , 0);dfs(1 , 0);for(int Q = read() ; Q ; --Q){s.clear();ans = 0;int S = read() , t;for(int i = 1 ; i <= S ; ++i){int a = read();if(i == 1)t = a;elseif(t != 1)t = LCA(t , a);insert(a);}printf("%d\n" , ans - S - num[jump[t][0]]);}}return 0;
}
转载于:https://www.cnblogs.com/Itst/p/10290442.html
Luogu4606 SDOI2018 战略游戏 圆方树、虚树、链并相关推荐
- [BZOJ5329][Sdoi2018]战略游戏 圆方树+虚树
5329: [Sdoi2018]战略游戏 Time Limit: 30 Sec Memory Limit: 512 MB Submit: 174 Solved: 109 [Submit][Stat ...
- BZOJ5329:[SDOI2018]战略游戏(圆方树,虚树)
Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任意一个城市出发总能沿着 ...
- [SDOI2018]战略游戏 圆方树+虚树
Description 给T组数据. 每组数据给你一个n个点的无向图,保证图联通,给q个询问. 每个询问给k个节点,问每一次询问中,求有多少个点在断掉他之后可以使图中两个点不连通. Sample In ...
- LuoguP4606 [SDOI2018]战略游戏
LuoguP4606 [SDOI2018]战略游戏 题目描述 题目描述 省选临近,放飞自我的小 QQ 无心刷题,于是怂恿小 CC 和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由 nn 个城市 ...
- [BZOJ5329][SDOI2018]战略游戏
bzoj luogu Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任 ...
- [bzoj5329][圆方树][虚树]战略游戏
Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任意一个城市出发总能沿着 ...
- BZOJ5329: [SDOI2018]战略游戏——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5329 https://www.luogu.org/problemnew/show/P4606 省选 ...
- [SDOI2018]战略游戏
题目描述 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战略游戏的地图由n个城市以及m条连接这些城市的双向道路构成,并且从任意一个城市出发总能沿着道路走到 任意 ...
- 【题解】SDOI2018战略游戏
被CNST的大小卡了好久.一定要开到18呀-- 首先,遇到这种带各种各样环的图先考虑是不是可以建立圆方树,在圆方树上求出答案.然后转化为圆方树之后,我们就将图转化到了树上.答案非常的明显:只要一个圆点 ...
最新文章
- 【Python 第8课】while
- mysql命令集合,MySql常用命令集合
- 命题公式的主合取范式C语言,命题公式主范式的自动生成与形式输出.pdf
- c++ 函数返回空_Python all() 函数
- Eclipse里不能调试J2ME程序的解决办法
- 解读SQL Server 2012中的最新BI功能
- 数据链路层解决的三个问题
- SQL Server 中系统表的作用
- python机器学习库教程——结巴中文分词
- 笔记:MapReduce Design Patterns
- windows环境安装elasticsearch
- talib python文档_TALib中文文档代码实现
- 计算机游戏软件视频,录屏软件具体怎么使用?记录电脑游戏视频
- MCS-51单片机汇编语言程序设计
- 《世界顶级思维》读后感 —— 上篇
- 2021年西式面点师(中级)报名考试及西式面点师(中级)证考试
- 阿里P9大牛徒手编写的这份十亿级并发手册,教你彻底玩懂高并发,赶紧收藏
- vue 项目中 zip 压缩包文件下载
- Vue.js尤雨溪 30分钟纪录片(中文字幕)
- 高仿知乎android客户端,仿知乎分享界面