moidamcuoi

 avatar
duyvan
plain_text
a year ago
895 B
6
Indexable
Never
#include <cstdio>
#include <iostream>
#include <vector>
#include <bits/stdc++.h>


using namespace std;

int a[105][105], n, m, s, t, ans;
bool visited[105];

void init() {
    scanf("%d%d%d%d", &n, &m, &s, &t);
    memset(a, 0, sizeof(a));
    for (int i = 0; i < m; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        a[u][v] = 1;
    }
}

void dfs(int u) {
    visited[u] = true;
    for (int v = 1; v <= n; ++v)
        if (!visited[v] && a[u][v] == 1) dfs(v);
}

void solve() {
    ans = 0;
    for (int u = 1; u <=n; ++u) {
        for (int v = 1; v <= n; ++v)  visited[v] = false;
        visited[u] = true;
        dfs(s);
        if (!visited[t]) ++ans;
    }
}

int main()  {
    int nTest;
    scanf("%d", &nTest);
    while (nTest--) {
        init();
        solve();
        printf("%d\n", ans);
    }

    return 0;
}

Leave a Comment