萌新圆方树 RE 求调
查看原帖
萌新圆方树 RE 求调
610557
shinzanmonoszm 妹妹楼主2023/6/9 23:17

rt,圆方树+虚树,若 98 行注释掉就成了 WA

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
const int sz=1e5+10;
const int msz=4e5+10;
const int lgsz=std::__lg(sz)+2;
struct edge{
    int nxt,to;
}graph[msz<<1];
int hpp,head[sz];
void addEdge(int from,int to){
    graph[++hpp]=edge{head[from],to};
    head[from]=hpp;
}
int dfn[sz<<1],low[sz],dpp,vtx,n,m,q;
std::vector<int>tree[sz<<1],vt[sz<<1];
std::stack<int>s;
void tarjan(int u){
    dfn[u]=low[u]=++dpp;
    s.push(u);
    for(int p=head[u];p;p=graph[p].nxt){
        int v=graph[p].to;
        if(dfn[v]==0){
            tarjan(v);
            low[u]=std::min(low[u],low[v]);
            if(low[v]>=dfn[u]){
                vtx++;
                int p;
                do{
                    p=s.top();
                    tree[vtx].push_back(p);
                    tree[p].push_back(vtx);
                    s.pop();
                }while(p!=v);
                tree[vtx].push_back(u);
                tree[u].push_back(vtx);
            }
        }else low[u]=std::min(low[u],dfn[v]);
    }
}
int key[sz],kpp,f[lgsz][sz<<1],dep[sz<<1];
void dfs(int u,int fau){
    dfn[u]=++dpp,f[0][dpp]=fau,dep[u]=dep[fau]+1;
    for(int v:tree[u]){
        if(v==fau)continue;
        dfs(v,u);
    }
}
int depmin(int u,int v){
    return dep[u]<dep[v]?u:v;
}
int lca(int u,int v){
    if(u==v)return u;
    int du=dfn[u],dv=dfn[v];
    if(du>dv)std::swap(du,dv);
    int lg=std::__lg(dv-du);
    return depmin(f[lg][du+1],f[lg][dv-(1<<lg)+1]);
}
void buildVT(){
    std::sort(key+1,key+kpp+1,[](int x,int y)->bool{return dfn[x]<dfn[y];});
    s.push(1);
    for(int i=1;i<=kpp;i++){
        int u=key[i],l=lca(u,s.top());
        while(s.top()!=l){
            int top=s.top();
            s.pop();
            if(dfn[s.top()]<dfn[l])s.push(l);
            vt[s.top()].push_back(top);
        }
        s.push(u);
    }
    while(s.top()!=1){
        int top=s.top();
        s.pop();
        vt[s.top()].push_back(top);
    }
}
int ans=0;
void vtDFS(int u){
    if(u<=n)ans++;
    for(int v:vt[u])vtDFS(v);
    vt[u].clear();
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin>>t;
    while(t--){
        hpp=dpp=0;
        std::cin>>n>>m;
        std::fill(dfn+1,dfn+n+1,0);
        std::fill(head+1,head+n+1,0);
        vtx=n;
        for(int i=1,u,v;i<=m;i++)
            std::cin>>u>>v,addEdge(u,v),addEdge(v,u);
        while(!s.empty())s.pop();
        tarjan(1);
        dpp=0;
        std::fill(dfn+1,dfn+vtx+1,0);
        dfs(1,0);
        for(int i=1;i<=std::__lg(vtx);i++)
            for(int j=1;j+(1<<i)-1<=vtx;j++)
                f[i][j]=depmin(f[i-1][j],f[i-1][j+(1<<i-1)]);
        std::cin>>q;
        while(q--){
            std::cin>>kpp;
            for(int i=1;i<=kpp;i++)std::cin>>key[i];
            while(!s.empty())s.pop();
            buildVT();
            ans=0;
            vtDFS(1);
            std::cout<<ans-kpp<<"\n";
        }
    }
    return 0;
}
2023/6/9 23:17
加载中...