求调wa#7#3
查看原帖
求调wa#7#3
1263684
Elysialr楼主2024/10/4 19:15
#include<bits/stdc++.h>
using namespace std;
struct node{
    int head=0;
    int dfn=0,low;
    bool cut=false;
};
struct road{
    int ver,Next;
};

node f[500001];
road r[4000001];
stack<int> s;
vector<vector<int>> ans;
int n,m,tot=1,dcc=0,num=0,root;
int st,to;

void vpush(int x){
    dcc++;
    ans.push_back(vector<int>());
    ans[dcc].push_back(x);
}
void rin(int x,int y){
    tot++;
    r[tot].ver=y;
    r[tot].Next=f[x].head;
    f[x].head=tot;    
}

void tarjan(int x){
    f[x].dfn=f[x].low=++num;
    s.push(x);
    if(x==root&&f[x].head==0){
        vpush(x);
        return;
    }
    int flag=0;
    for(int i=f[x].head;i!=0;i=r[i].Next){
        int y=r[i].ver;

        if(f[y].dfn==0){
            tarjan(y);
            f[x].low=min(f[x].low,f[y].low);

            if(x!=st&&x!=to)
            if(f[y].low>=f[x].dfn){
                flag++;
                if(x!=root||flag>=2)
                f[x].cut=true;
                vpush(x);
                int z;
                do{
                    z=s.top();s.pop();
                    ans[dcc].push_back(z);
                }while(z!=y);
            }
        }
        else f[x].low=min(f[x].low,f[y].dfn);
    }
}
bool vis[500001];int res=5000001;
bool dfs(int x){
    //cout<<x<<" ";
    vis[x]=false;
    if(x!=st&&x!=to) res=min(res,x);
    if(x==to) return true;
    for(int i=f[x].head;i!=0;i=r[i].Next){
        int y=r[i].ver;
        if(f[y].cut&&vis[y])
        //if((x-n)*(y-n)<0)
        if(dfs(y)) return true;
    }
    vis[x]=false;
    return false;
}
int main(){
    cin>>n;
    ans.push_back(vector<int>());
    int x,y;
    while(true){
        cin>>x>>y;
        if(x==0&&y==0) break;
        rin(x,y);rin(y,x);
    }
    cin>>st>>to;

    for(int i=1;i<=n;i++)
    if(f[i].dfn==0){
        root=i;
        tarjan(i);
    }

    for(int i=1;i<=dcc;i++){
        f[n+i].cut=true;
        for(int j=0;j<ans[i].size();j++){
            if(f[ans[i][j]].cut){
                rin(n+i,ans[i][j]);
                rin(ans[i][j],n+i);
            }
            if(ans[i][j]==st) x=i+n;
            if(ans[i][j]==to) y=i+n;
        }
    }
    
    if(!f[st].cut) st=x;
    if(!f[to].cut) to=y;

    memset(vis,true,sizeof(vis));
    if(dfs(st)&&res<=n) cout<<res;
    else cout<<"No solution";
    return 0;
}
2024/10/4 19:15
加载中...