题解:P8456 「SWTR-8」地地铁铁

· · 题解

好题。

首先看到简单路径(端点不交),考虑在圆方树上处理。

首先处理点对在一个点双内的贡献,假若点双内边颜色全部相同,那么显然不合法,否则就一定合法,吗?

很显然不是,如果 u \to v 所有路径是同色的,点双形如一个杏仁那就不合法,怎么统计这样的 u \to v 对呢?

实际上这样的 u,v 至多一对,画出这样的杏仁后,容易发现其他点对之间的路径都能构造出至少一条同时经过了 d,D 的。

那怎么确定到底是有一对还是没有呢?观察符合条件的杏仁,你发现有一对当且仅当存在恰好两个有不同颜色出边的点。

然后考虑统计跨过至少两个点双的点对的贡献,不难发现只需要路径上有一个非同色点双即可,因为出发或者结束的时候便可获得一种颜色,经过非同色点双时再把另一种颜色补上即可。

实际统计可以这么做,先把 n \choose 2 算进答案,然后不合法的首先有形如同色点双构成的极大同色连通块,可以通过圆方树上 dp 求解贡献,再对每个非同色点双看看是否要减去一对 u,v 的贡献即可。

判断一条边属于哪个点双可以对圆方树设置一个根然后对每个点求出父亲,就可以快速判断两个相邻圆点出边交。

时间复杂度 O(n+m)。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 4e5+114;
const int maxm = 1e6+114;
struct star{
    char ty;
    int nxt,to;
}e[maxm<<1];
int head[maxn];
int tot=1;
void add(int u,int v,char ty){
    tot++;
    e[tot].nxt=head[u];
    e[tot].to=v;
    e[tot].ty=ty;
    head[u]=tot;
    tot++;
    e[tot].nxt=head[v];
    e[tot].to=u;
    e[tot].ty=ty;
    head[v]=tot;
}
int dfn[maxn],low[maxn],dfncnt;
int stk[maxn],tp;
int n,m;
vector<int> G[maxn+maxm];
int cnt;
void tarjan(int u){
    dfn[u]=low[u]=++dfncnt;
    stk[++tp]=u;
    for(int i=head[u];i;i=e[i].nxt){
        int v=e[i].to;
        if(dfn[v]==0){  
            tarjan(v);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u]){
                cnt++;
                for(int x=0;x!=v;tp--){
                    x=stk[tp];
                    G[cnt].push_back(x);
                    G[x].push_back(cnt);
                }
                G[cnt].push_back(u);
                G[u].push_back(cnt);
            }
        }else low[u]=min(low[u],dfn[v]);
    }
}
int fa[maxn+maxm];
void dfs(int u){
    for(int v:G[u]){
        if(v!=fa[u]){
            fa[v]=u;
            dfs(v);
        }
    }
}
vector< pair<int,char> > P[maxn+maxm];
int ty[maxn+maxm];//0 - d 1 - D 2 - dD,same path 3 - dD diff path
int dp[maxn+maxm][2];//0 - d 1 - D
int ans;
void DP(int u){
    if(u<=n){
        dp[u][0]++;
        dp[u][1]++;
    }else{
        if(ty[u]==0) dp[u][1]=0;
        else if(ty[u]==1) dp[u][0]=0;
        else dp[u][0]=dp[u][1]=0;
    }
    for(int v:G[u]){
        if(v!=fa[u]){
            DP(v);
            if(u<=n){
                ans-=dp[u][0]*dp[v][0];
                ans-=dp[u][1]*dp[v][1];
                dp[u][0]+=dp[v][0];
                dp[u][1]+=dp[v][1];
            }else{
                if(ty[u]==0){
                    ans-=dp[u][0]*dp[v][0];
                    dp[u][0]+=dp[v][0];
                }else if(ty[u]==1){
                    ans-=dp[u][1]*dp[v][1];
                    dp[u][1]+=dp[v][1];
                }
            }
        }
    }  
}
int ct[maxn][2];
void init(){
    for(int u=1;u<=n;u++){
        for(int i=head[u];i;i=e[i].nxt){
            int v=e[i].to;
            if(v>u){
                int w;
                if(fa[u]==fa[v]){
                    w=fa[u];
                }else if(fa[fa[u]]==v){
                    w=fa[u];
                }else{
                    w=fa[v];
                }
                P[w].push_back(make_pair(u,e[i].ty));
                P[w].push_back(make_pair(v,e[i].ty));
            }
        }
    }
    for(int u=n+1;u<=cnt;u++){
        int cd=0,cD=0,cdD=0;
        for(pair<int,char> now:P[u]){
            if(now.second=='d') ct[now.first][0]++;
            else ct[now.first][1]++;
        }
        for(int v:G[u]){
            if(ct[v][0]*ct[v][1]>0) cdD++;
            else if(ct[v][0]>0) cd++;
            else cD++;
        }
        for(pair<int,char> now:P[u]){
            if(now.second=='d') ct[now.first][0]--;
            else ct[now.first][1]--;
        }
        if(cdD>2){
            ty[u]=3;
        }else if(cdD==2){
            ty[u]=2;
            ans--;
        }else if(cd>0){
            ty[u]=0;
        }else ty[u]=1;
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int id=0;
    cin>>id>>n>>m;
    cnt=n;
    for(int i=1;i<=m;i++){
        int u,v;
        char ty;
        cin>>u>>v>>ty;
        add(u,v,ty);
    }
    ans=n*(n-1)/2;
    tarjan(1);
    dfs(1);
    init();
    DP(1);
    cout<<ans<<'\n';
    return 0;
}