为什么这种写法会超时啊
查看原帖
为什么这种写法会超时啊
486056
xuzishuai楼主2023/11/25 20:20
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
vector<int> mp[N];
int match[N];
bool be[N][N];
// 二分图这样更好理解?vis太抽象了
// 类似于当前弧优化,如果和之前配对的对象协商过了,那么不用再去协商了,直接从上次成功配对的下一个人开始尝试配对
int now[N];
bool path(int x)
{
    int len = mp[x].size();
    for (int i = now[x]; i < len; ++i)
    {
        now[x] = i + 1;
        int to = mp[x][i];
        if (match[to] == -1 || path(match[to]))
        {
            match[to] = x;
            return 1;
        }
    }
    return 0;
}
int main()
{

    memset(match, -1, sizeof(match));
    int n, m, e;
    cin >> n >> m >> e;
    for (int i = 1; i <= e; ++i)
    {
        int u, v;
        cin >> u >> v;
        if (be[u][v])
            continue;
        be[u][v] = 1;
        mp[u].push_back(v);
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
#if 0
        if (i == 3)
        {
            for (int k = N + 1; k <= N + m; ++k)
                cout << match[k] << "#\n";
        }
#endif
        memset(now, 0, sizeof(now));
        if (path(i))
            ++ans;
    }
    cout << ans;

    return 0;
}

为什么这种写法会超时啊,我觉的时间复杂度挺正确,每次调用path会使某一个now[x]增加1,而所有now[x]加起来最多是边数(n^2)所以总时间复杂度为n^3但是为什么超时了,只有70分

2023/11/25 20:20
加载中...