题解

圆方树建好之后点是原来的两倍,而st表求lca也要开到点的两倍,所以是四倍

我并没有开小,然而= =,我的预处理log2,写成了200000,而不是400000

我是不是折翼啊= =

很可写,我们对于割点考虑一下圆方树,发现答案就是圆方树上两个圆点之间经过了多少圆点,把圆点拿出来然后建立虚树,统计一下这个点和父亲之间有多少个圆点,建立虚树之后统计一下加入的lca是不是圆点,如果是的话也要把答案+1

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
//#define ivorysi
#define pb push_back
#define MAXN 200005
#define eps 1e-12
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define fi first
#define se second
#define mo 974711
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 - '0' + c;
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) putchar('-'),x = -x;
while(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int T;
int N,M,Q,sumE,head[MAXN],Cnt;
struct node {
int to,next;
}E[MAXN * 4];
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
int dfn[MAXN],low[MAXN],idx,sta[MAXN],top,st[MAXN * 2][20],dep[MAXN],cir[MAXN],len[MAXN * 2];
vector<int> ver[MAXN];
int MK[MAXN],C,ans;
void Tarjan(int u,int fa) {
dfn[u] = low[u] = ++idx;
sta[++top] = u;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(dfn[v] && v != fa) {
low[u] = min(dfn[v],low[u]);
}
else if(v != fa){
Tarjan(v,u);
if(low[v] >= dfn[u]) {
++Cnt;
ver[u].pb(Cnt);
while(1) {
int x = sta[top--];
ver[Cnt].pb(x);
if(x == v) break;
}
}
else low[u] = min(low[u],low[v]);
}
}
}
int min_dep(int a,int b) {
return dep[a] < dep[b] ? a : b;
}
int lca(int u,int v) {
u = dfn[u],v = dfn[v];
if(u > v) swap(u,v);
int l = len[v - u + 1];
return min_dep(st[u][l],st[v - (1 << l) + 1][l]);
}
void dfs(int u,int fa) {
dfn[u] = ++idx;
st[idx][0] = u;
cir[u] = cir[fa];
dep[u] = dep[fa] + 1;
if(u <= N) ++cir[u]; for(auto k : ver[u]) {
dfs(k,u);
st[++idx][0] = u;
}
}
bool cmp(int a,int b) {
return dfn[a] < dfn[b];
}
int calc(int a,int b) {
if(dep[a] < dep[b]) swap(a,b);
return cir[a] - cir[b] - (a <= N);
}
void Build_aux() {
sort(MK + 1,MK + C + 1,cmp);
top = 0;
for(int i = 1 ; i <= C ; ++i) {
if(!top) sta[++top] = MK[i];
else {
int l = lca(sta[top],MK[i]);
while(top && dep[sta[top]] > dep[l]) {
if(top == 1 || dep[sta[top - 1]] <= dep[l]) {
ans += calc(sta[top],l);
}
else {
ans += calc(sta[top],sta[top - 1]);
}
--top;
}
if(sta[top] != l) {
if(l <= N) ++ans;
sta[++top] = l;
}
sta[++top] = MK[i];
}
}
for(int i = top ; i >= 2 ; --i) ans += calc(sta[i],sta[i - 1]);
}
void Init() {
read(N);read(M);
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(head,0,sizeof(head));
idx = 0;top = 0;sumE = 0; for(int i = 1 ; i <= 2 * N ; ++i) ver[i].clear();
Cnt = N;
int u,v;
for(int i = 1 ; i <= M ; ++i) {
read(u);read(v);add(u,v);add(v,u);
}
Tarjan(1,0);
idx = 0;
dfs(1,0);
for(int j = 1 ; j <= 19 ; ++j) {
for(int i = 1 ; i <= idx; ++i) {
if(i + (1 << j) - 1 > idx) break;
st[i][j] = min_dep(st[i][j - 1],st[i + (1 << j - 1)][j - 1]);
}
}
}
void Solve() {
read(Q);
while(Q--) {
read(C);
for(int i = 1 ; i <= C ; ++i) read(MK[i]);
ans = 0;
Build_aux();
printf("%d\n",ans);
}
}
int main() {
#ifdef ivorysi
freopen("01.in","r",stdin);
#endif
for(int i = 2 ; i <= 400000 ; ++i) len[i] = len[i / 2] + 1;
read(T);
while(T--) {
Init();
Solve();
}
return 0;
}

【LOJ】#2562. 「SDOI2018」战略游戏的更多相关文章

  1. @loj - 2004@ 「SDOI2017」硬币游戏

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 周末同学们非常无聊,有人提议,咱们扔硬币玩吧,谁扔的硬币正面次数 ...

  2. loj#2565. 「SDOI2018」旧试题(反演 三元环计数)

    题意 题目链接 Sol 神仙反演题.在洛谷上疯狂被卡常 Orz shadowice #include<bits/stdc++.h> #define Pair pair<int, in ...

  3. LOJ #6436. 「PKUSC2018」神仙的游戏(字符串+NTT)

    题面 LOJ #6436. 「PKUSC2018」神仙的游戏 题解 参考 yyb 的口中的长郡最强选手 租酥雨大佬的博客 ... 一开始以为 通配符匹配 就是类似于 BZOJ 4259: 残缺的字符串 ...

  4. Loj #3056. 「HNOI2019」多边形

    Loj #3056. 「HNOI2019」多边形 小 R 与小 W 在玩游戏. 他们有一个边数为 \(n\) 的凸多边形,其顶点沿逆时针方向标号依次为 \(1,2,3, \ldots , n\).最开 ...

  5. Loj #3044. 「ZJOI2019」Minimax 搜索

    Loj #3044. 「ZJOI2019」Minimax 搜索 题目描述 九条可怜是一个喜欢玩游戏的女孩子.为了增强自己的游戏水平,她想要用理论的武器武装自己.这道题和著名的 Minimax 搜索有关 ...

  6. Loj #2192. 「SHOI2014」概率充电器

    Loj #2192. 「SHOI2014」概率充电器 题目描述 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品--概率充电器: 「采用全新纳米级加工技术,实现元件与导线能否通电完 ...

  7. Loj #3096. 「SNOI2019」数论

    Loj #3096. 「SNOI2019」数论 题目描述 给出正整数 \(P, Q, T\),大小为 \(n\) 的整数集 \(A\) 和大小为 \(m\) 的整数集 \(B\),请你求出: \[ \ ...

  8. Loj #3093. 「BJOI2019」光线

    Loj #3093. 「BJOI2019」光线 题目描述 当一束光打到一层玻璃上时,有一定比例的光会穿过这层玻璃,一定比例的光会被反射回去,剩下的光被玻璃吸收. 设对于任意 \(x\),有 \(x\t ...

  9. Loj #3089. 「BJOI2019」奥术神杖

    Loj #3089. 「BJOI2019」奥术神杖 题目描述 Bezorath 大陆抵抗地灾军团入侵的战争进入了僵持的阶段,世世代代生活在 Bezorath 这片大陆的精灵们开始寻找远古时代诸神遗留的 ...

随机推荐

  1. Hadoop生态圈-Hive函数

    Hadoop生态圈-Hive函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  2. JVM体系结构和工作方式

            JVM能够跨计算机体系结构来执行Java字节码,主要是由于JVM屏蔽了与各个计算机平台相关的软件或者是硬件之间的差异,使得与平台相关的耦合统一由JVM提供者来实现.   何为JVM   ...

  3. 数据分析与展示---Matplotlib基本绘图函数

    一:基本绘图函数(这里介绍16个,还有许多其他的) 二:pyplot饼图plt.pie的绘制 三:pyplot直方图plt.hist的绘制 (一)修改第二个参数bins:代表直方图的个数,均分为多段, ...

  4. python---基础知识回顾(十)进程和线程(进程)

    前戏:进程和线程的概念 若是学过linux下的进程,线程,信号...会有更加深刻的了解.所以推荐去学习下,包括网络编程都可以去了解,尤其是对select,poll,epoll都会有更多的认识. 进程就 ...

  5. 判断android是否是debug

    1.使用BuildConfig.DEBUG,这个在住modul里面是有效的,但是在有依赖库里面使用就会一直返回false,可以通过下面的方法解决:在library的build.gradle中添加以下代 ...

  6. mysql 允许远程登录

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;flush privileges;

  7. sql server 查询本周、本月所有天数的数据

    查询本月所有的天数: --本月所有的天数 ),) day from (),,)+'-01' day) t1, ( ) t2 ),) ),,)+'%' 查询本周所有的天数: ),,),) ),,),) ...

  8. sklearn_SVC_支持向量机

    # coding:utf-8 import numpy as np from sklearn.svm import SVC import matplotlib.pyplot as plt #生成数据 ...

  9. Mysql注入root权限直接写一句话马

    首先我们的找到一个有注入的站:这里我用自己搭建的环境表示:大家不要乱来 http://localhost/pentest/sql/sql_injection_get.php?id=1 发现是root权 ...

  10. Ping程序的实现

    Ping程序的实现 在windows系统下进行cmd可以进行ping操作. ping命令是用来确定本地主机与网络中其他主机的网络通信情况,或者查看是否是为效IP. ping的工作原理:网络另一主机发送 ...