[CF1276B] Two Fairs - DFS

Given \ (n-\) points, \ (m \) edges, and two points \ (S, T \) , to find the point \ ((a, b) \ ) number, satisfying any one \ (a \ to b \) paths have been \ (s, t \)

Solution

From \ (S \) start DFS, can without \ (T \) set point was reached in mind \ (S \)

From \ (T \) start DFS, can without \ (S \) reaches the set point referred to as \ (T \)

And the answer is \ (| S / T || T / S | \)

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;

int n,m,s,t,S[N],T[N],a1,a2;
vector <int> g[N];

void dfs1(int p) {
    S[p]=1;
    for(int q:g[p]) if(S[q]==0&&q!=t) dfs1(q);
}

void dfs2(int p) {
    T[p]=1;
    for(int q:g[p]) if(T[q]==0&&q!=s) dfs2(q);
}

signed main() {
    ios::sync_with_stdio(false);
    int Tx;
    cin>>Tx;
    while(Tx--) {
        cin>>n>>m>>s>>t;
        for(int i=1;i<=m;i++) {
            int t1,t2;
            cin>>t1>>t2;
            g[t1].push_back(t2);
            g[t2].push_back(t1);
        }
        dfs1(s);
        dfs2(t);
        S[s]=0; S[t]=0;
        T[t]=0; T[s]=0;
        for(int i=1;i<=n;i++) if(S[i]&&!T[i]) ++a1;
        for(int i=1;i<=n;i++) if(T[i]&&!S[i]) ++a2;
        cout<<a1*a2<<endl;
        for(int i=1;i<=n;i++) S[i]=T[i]=0;
        for(int i=1;i<=n;i++) g[i].clear();
        a1=a2=0;
    }
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12551854.html
dfs