80分第一个样例WA玄关求调!!!
查看原帖
80分第一个样例WA玄关求调!!!
1494691
Theonlybreeze楼主2025/7/25 17:24
#include<bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
int n, m, u, v, sx, sy, ans, f;
vector<int> edge[N];
bool vis[N], block[N];
bool bfs() {
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    q.push(sx);
    vis[sx] = true;
    if(block[sx] == true) return false;
    while(!q.empty()) {
        int fr = q.front();
        q.pop();
        if(fr == sy) return true;
        for(auto p : edge[fr]) {
            if(vis[p] == 0 && block[p] == false) {
                vis[p] = 1;
                q.push(p);
            }
        }
    }
    return false;
}
int main() {
    cin >> n >> m;
    for(int i = 1; i <= m; i ++) {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    cin >> sx >> sy;
    if(sx == sy) {
        cout << 0;
        return 0;
    }
    for(int i = 1; i <= n; i ++) {
        if(i == sx || i == sy) continue;
        else {
            block[i] = true;
            if(bfs() == false) ans ++;
            else f = 1;
            block[i] = false;
        }
    }
    if(f == 1) cout << ans;
    else cout << -1;
    return 0;
}
2025/7/25 17:24
加载中...