QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#537847#6705. MedianXiaoretaWRE 0ms3548kbC++203.6kb2024-08-30 19:04:312024-08-30 19:04:31

Judging History

你现在查看的是最新测评结果

  • [2024-08-30 19:04:31]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3548kb
  • [2024-08-30 19:04:31]
  • 提交

answer

#include <bits/stdc++.h>

#define debug(...) 42;
#ifdef LOCAL
#include "debug.h"
#endif

using namespace std;
#define V vector
#define fi first
#define se second
#define mp make_pair
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define F(i, l, r) for (int i = l; i < r; ++i)
#define R(i, l, r) for (int i = r-1; i >= l; --i)
typedef long long LL;
typedef double DB;
typedef pair<int, int> PII;
template<typename T> bool setmin(T &a, T b) { return (a > b ? a = b, 1 : 0); }
template<typename T> bool setmax(T &a, T b) { return (a < b ? a = b, 1 : 0); }
// mt19937_64 rng {chrono::steady_clock::now().time_since_epoch().count()};

/* -------------Main code------------- */
struct DSU {
    int n;
    vector<int> p, sz;
    DSU(int _n) {
        n = _n;
        p.resize(n);
        sz.resize(n, 1);
        iota(all(p), 0);
    }
    int size(int u) {
        return sz[leader(u)];
    }
    int leader(int u) { return u == p[u] ? u : p[u] = leader(p[u]); }
    bool merge(int u, int v) {
        int a = leader(u), b = leader(v);
        if (a != b) {
            if (sz[a] < sz[b]) swap(a, b);
            p[b] = a;
            sz[a] += sz[b];
            return true;
        }
        return false;
    }
};

void solve() {
    int n, m; cin >> n >> m;
    V<V<int>> g(n + 1), g_t(n + 1);
    V<int> d(n + 1), d_t(n + 1);

    DSU dsu(n + 1);
    F(i, 0, m) {
        int u, v; cin >> u >> v;
        dsu.merge(u, v);
        d[v]++;
        d_t[u]++;
        g[u].emplace_back(v);
        g_t[v].emplace_back(u);
    }

    auto has = [&]() {
        V<int> q;
        F(i, 1, n + 1) if (d[i] == 0) q.push_back(i);
        for (int i = 0; i < sz(q); i++) {
            int u = q[i];
            for (int v : g[u]) if (--d[v] == 0) q.push_back(v);
        }
        for (int i = 1; i <= n; i++) if (d[i]) return true;
        return false;
    };

    if (has()) {
        cout << string(n, '0') << '\n';
        return;
    }

    V<int> comp1(n + 1, -1), comp2(n + 1, -1);
    auto dfs1 = [&](auto self, int u) -> int {
        if (comp1[u] != -1) return comp1[u];
        int res = 0;
        for (int v : g[u]) {
            res += self(self, v) + 1;
        }
        return comp1[u] = res;
    };
    auto dfs2 = [&](auto self, int u) -> int {
        if (comp2[u] != -1) return comp2[u];
        int res = 0;
        for (int v : g_t[u]) {
            res += self(self, v) + 1;
        }
        return comp2[u] = res;
    };
    for (int u = 1; u <= n; u++) {
        comp1[u] = dfs1(dfs1, u);
    }
    for (int u = 1; u <= n; u++) {
        comp2[u] = dfs2(dfs2, u);
    }
    // debug(comp1, comp2);
    V<int> w;
    for (int i = 1; i <= n; i++)
        if (i == dsu.leader(i))
            w.emplace_back(dsu.size(i));
    for (int i = 1; i <= n; i++) {
        if (comp1[i] <= (n / 2) and comp2[i] <= (n / 2)) {
            V<int> f(n + 1);
            f[0] = 1;
            int id = -1;
            for (int j = 0; j < sz(w); j++)
                if (w[j] == dsu.size(i)) { id = j; break; }
            for (int j = 0; j < sz(w); j++) {
                if (j == id) continue;
                for (int k = n; k >= w[j]; k--)
                    f[k] |= f[k - w[j]];
            }
            if (comp1[i] != -1 and f[(n / 2) - comp1[i]]) {
                cout << 1;
            } else cout << 0;
        } else cout << 0;
    }
    cout << '\n';
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int tt; cin >> tt;
    while (tt--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3548kb

input:

2
5 4
1 2
3 2
2 4
2 5
3 2
1 1
2 3

output:

01000
000

result:

ok 2 lines

Test #2:

score: -100
Runtime Error

input:

66
13 2
9 13
7 11
11 19
9 1
8 1
5 1
2 8
4 2
2 1
5 2
6 3
3 11
3 2
4 6
6 10
9 8
3 5
1 7
5 8
3 9
4 9
6 7
3 1
2 3
11 6
9 4
1 6
5 2
1 5
4 6
8 4
15 15
10 6
15 8
7 6
11 1
5 2
3 4
11 13
4 6
10 12
10 13
1 6
15 2
5 12
13 14
5 3
15 86
14 12
8 1
14 9
8 15
5 10
1 9
11 2
6 2
7 10
10 13
14 5
4 13
5 8
4 10
13 9
6 9...

output:


result: