Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)

Posted herlo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)相关的知识,希望对你有一定的参考价值。

?? ?? ??
题意:求点对中,满足要互达必须经过a,b两点的对数,图为无向连通图

若(x,y)要满足互达必须经过(a,b),反过来想,
a必须通过b点到达y点:满足a--->b--->y;
b必须通过a点到达x点:满足b--->a--->x,无向图:x--->a--->b;
连起来即为:x--->a--->b--->y;

int vis[MAXN];
vector<int>edge[MAXN];
void dfs(int x,int e)
{
    vis[x]=1;
    if(x==e) return ;
    for(auto v:edge[x])
    {
        if(vis[v]==0)
        {
            vis[v]=1;
            dfs(v,e);
        }
    }
}
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m,a,b;
        cin>>n>>m>>a>>b;//
        rpp(i,n) edge[i].clear(),vis[i]=0;
        rep(i,m)
        {
            int x,y;cin>>x>>y;
            edge[x].push_back(y);
            edge[y].push_back(x);
        }
        int num0=0,num1=0;
        dfs(a,b);
        rpp(i,n) if(!vis[i]) ++num0;
        rpp(i,n) vis[i]=0;
        dfs(b,a);
        rpp(i,n) if(!vis[i]) ++num1;
        ll ans=1ll*num0*num1;
        cout<<ans<<endl;
    }
    //stop;
    return 0;
}

以上是关于Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

cf比赛记录Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)

Codeforces Round #606 (Div. 2) D. Let's Play the Words?(贪心+map)

Codeforces Round #436 E. Fire(背包dp+输出路径)