跳到正文

题解

题解:P8426 [JOI Open 2022] 放学路 / School Road

_Ch1F4N_约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年1月4日
更新
2026年1月4日
篇幅
约 7 分钟
题目
P8426

版本

  1. v2当前
  2. v1

首先图上有很多点都是干扰的,所有满足不存在 1→u→n1 \to u \to n 的简单路径的 uu 都可以从图中删掉。

考虑咋做这个,建圆方树保留所有属于 1→n1 \to n 路径上经过的方点的边即可。

然后考察图会长什么样,不难发现此时对于图中任意一点 uu 都存在 1→u→n1 \to u \to n 的简单路径,而我们要判断的是所有 1→n1 \to n 的简单路径长度是否都相等。

如果你做过 P8331 就可以立马反应过来这个图应该形如一个纺锤体,更具体地,定义一个纺锤体 (s,t)(s,t) 是由若干条两两不交的 s→ts \to t 的简单路径构成的图,我们声称此时图一定形如纺锤体 (s,p1),(p1,p2),…,(pk−1,pk),(pk,t)(s,p_1),(p_1,p_2),\dots,(p_{k-1},p_k),(p_k,t) 依次拼接。

考虑证明,假设图中所有 s→ts \to t 的简单路径长度相等,显然可以从任意一个点双连通分量中找到一个边的子集使得其构成了纺锤体,考虑在此基础上加入其他边,考虑我们加入边 (u,v,c)(u,v,c),而在纺锤体上存在路径 s→u,u→t,s→v,v→ts \to u,u \to t,s \to v,v \to t,注意到 ws→u+c+wv→t=ws→u+wu→tw_{s \to u}+c+w_{v \to t}=w_{s \to u}+w_{u \to t} 说明 c+wv→t=wu→tc+w_{v \to t}=w_{u \to t},但是交换这个式子中 u,vu,v 的位置可以推出 c+wu→t=wv→tc+w_{u \to t}=w_{v \to t},而 c>0c>0,所以与假设矛盾,故该点双联通分量只能是一个纺锤体。而圆方树上一条链上的点双放到一起就是若干纺锤体首尾拼接。

考虑怎么判定,由于纺锤体的结构很好,直接用缩二度点和叠合重边的办法判断即可,具体而言就是叠重边的时候边权一致并且最后可以收缩成一条 s→ts \to t 的边。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 3e5+114;
int n,m;
vector<int> V[maxn];
vector<int> G[maxn];
int A[maxn],B[maxn],C[maxn];
int stk[maxn],tp,low[maxn],dfn[maxn],dfc;
int cnt;
void Tarjan(int u){
    low[u]=dfn[u]=++dfc;
    stk[++tp]=u;
    for(int v:V[u]){
        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 vis[maxn];
int fa[maxn];
void dfs(int u){
    for(int v:G[u]){
        if(v!=fa[u]){
            fa[v]=u;
            dfs(v);
        }
    }
}
set<int> E[maxn];
map<int,int> e[maxn],f[maxn];
int ecnt;
bool add(int u,int v,int c){
    if(e[min(u,v)][max(u,v)]==0){
        ecnt++;
        e[min(u,v)][max(u,v)]=1;
        E[u].insert(v);
        E[v].insert(u);
        f[u][v]=f[v][u]=c;
    }else{
        if(f[u][v]!=c) return false;
    }
    return true;
}
queue<int> q;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>A[i]>>B[i]>>C[i];
        V[A[i]].push_back(B[i]);
        V[B[i]].push_back(A[i]);
    }
    cnt=n;
    Tarjan(1);
    dfs(1);
    int u=n;
    while(u!=1){
        if(u>n) vis[u]=1;
        u=fa[u];
    }
    for(int i=1;i<=m;i++){
        if(fa[A[i]]==fa[B[i]]){
            if(vis[fa[A[i]]]==1){
                if(add(A[i],B[i],C[i])==false){
                    cout<<"1\n";
                    return 0;
                }
            }
        }else{
            if(fa[fa[A[i]]]==B[i]){
                if(vis[fa[A[i]]]==1&&add(A[i],B[i],C[i])==false){
                    cout<<"1\n";
                    return 0;
                }
            }else{
                if(vis[fa[B[i]]]==1&&add(A[i],B[i],C[i])==false){
                    cout<<"1\n";
                    return 0;
                }
            }
        }
    }
    for(int i=1;i<=n;i++) if(E[i].size()==2&&i>1&&i<n) q.push(i);
    while(q.size()>0){
        int u=q.front();
        q.pop();
        if(E[u].size()==2){
            int x=(*E[u].begin());
            E[u].erase(x);
            E[x].erase(u);
            int y=(*E[u].begin());
            E[u].erase(y);
            E[y].erase(u);
            ecnt-=2;
            if(add(x,y,f[x][u]+f[u][y])==false){
                cout<<"1\n";
                return 0;
            }
            if(E[x].size()==2&&x>1&&x<n) q.push(x);
            if(E[y].size()==2&&y>1&&y<n) q.push(y);
        }
    }
    if(ecnt!=1){
        cout<<"1\n";
    }else cout<<"0\n";
    return 0;
}
原文