这个程序为什么只能过20%。
查看原帖
这个程序为什么只能过20%。
994339
cyt_666楼主2024/12/8 23:34

我的思想是:根据y的值,来决定bfs向周围扩散的半径。已经卡了3个小时了,实在没办法了。求友友帮帮!

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define debug(a) cout << #a << " = " << a << "\n"
#define debuga(a, i) cout << #a << "[" << i << "]" << " = " << a[i] << "\n"
using namespace std;

typedef long long  LL;
typedef pair<int, int> PII;
const int N = 1e3 + 10;

vector<int> e[N];
int st[N];
int cal(int x, int y) {
    memset(st, 0, sizeof st);
    queue<int> q;
    st[x] = 1;
    q.push(x);
    int res = 0;
    while (q.size()) {
        if (!y) return res;
        int v = q.front();
        q.pop();

        for (const auto u : e[v]) {
            if (!st[u]) {
                st[u] = 1;
                q.push(u);
                res ++;
            }
        }
        y --;
    }
    return res;
}   

void solve() {
    int n, m, q;
    cin >> n >> m >> q;

    for (int i = 0; i < m; i ++ ) {
        int a, b;
        cin >> a >> b;
        e[a].push_back(b);
        e[b].push_back(a);
    }
    // for (auto x : e[2]) {
    //     debug(x);
    // }
    int sum = 0;
    for (int i = 0; i < q; i ++ ) {
        int x, y;
        cin >> x >> y;
        sum += cal(x, y) + 1;
    }
    // debug(sum);
    cout << fixed << setprecision(2) << sum * 1.0 / q << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = 1;
	// cin >> t;

	while (t --) {
		solve();
	}

	return 0;
}
2024/12/8 23:34
加载中...