QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#508715#7181. Graph CutsQingyyxWA 5ms12388kbC++205.2kb2024-08-07 19:31:432024-08-07 19:31:43

Judging History

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

  • [2024-08-07 19:31:43]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:12388kb
  • [2024-08-07 19:31:43]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define enl putchar('\n')
#define all(x) (x).begin(),(x).end()
#define debug(x) printf(" "#x":%d\n",x);
using namespace std;
const int MAXN = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
typedef pair<int, int> pii;
char buf[1 << 21], * p1 = buf, * p2 = buf, obuf[1 << 21], * o = obuf, of[35];
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline ll qpow(ll a, ll n) { ll res = 1; while (n) { if (n & 1)res = res * a % mod; n >>= 1; a = a * a % mod; }return res; }
template <class T = int>inline T read() { T s = 0, f = 1; char c = gc(); for (; !isdigit(c); c = gc())if (c == '-')f = -1; for (; isdigit(c); c = gc())s = s * 10 + c - '0'; return s * f; }
inline void read(int* a, int n) { for (int i = 1; i <= n; ++i)a[i] = read(); }
inline char getc() { char c; for (c = gc(); c == ' ' || c == '\n'; c = gc()); return c; }
inline int inal(char* s) { int n = 0; for (s[0] = gc(); !isalpha(s[0]); s[0] = gc()); for (; isalpha(s[n]); s[++n] = gc()); return s[n] = 0, n; }
inline void outd(auto* a, int n) { for (int i = 1; i <= n; ++i)printf("%d ", a[i]); enl; }
int n, m, q;
bitset<MAXN>t[305];
int idg[305][MAXN];
int hav[MAXN], tot, ri[MAXN];
pii g[MAXN];
int deg[MAXN], cnt[MAXN];
const int siz = 300;
bool del[MAXN], inS[MAXN];

struct EG {
    int to, nxt, id;
}e[MAXN << 1];
int head[MAXN], etot;
inline void add(int u, int v, int id) {
    e[etot] = EG {v, head[u], id};
    head[u] = etot++;
}
void clear(int n = MAXN - 1) { memset(head + 1, -1, sizeof(int) * n); etot = 0; }

struct QUE {
    queue<int>q;
    bool inq[MAXN];
    void set(int x) {
        if (inq[x])return;
        q.push(x);
        inq[x] = 1;
    }
    int ask() {
        while (!q.empty()) {
            int u = q.front(); q.pop();
            inq[u] = 0;
            if (cnt[u] == 1)return u;
        }
        return 0;
    }
}que;



void solve() {
    n = read(), m = read();
    for (int i = 1; i <= m; ++i) {
        int x = read(), y = read();
        g[i] = {x, y};
        deg[x]++, deg[y]++;
    }
    for (int i = 1; i <= n; ++i)
        if (deg[i] >= siz)
            ri[hav[i] = ++tot] = i;

    clear(n);
    for (int i = 1; i <= m; ++i) {
        auto& [u, v] = g[i];
        if (!hav[u] && !hav[v]) {
            add(u, v, i), add(v, u, i);
        } else {
            if (hav[u])idg[hav[u]][v] = i, t[hav[u]].set(v);
            if (hav[v])idg[hav[v]][u] = i, t[hav[v]].set(u);
        }
    }

    del[0] = 1;

    q = read();
    while (q--) {
        char op = getc();
        if (op == '+') {
            int x = read();
            inS[x] = 1;
            for (int i = 1; i <= tot; ++i)
                if (!del[idg[ri[i]][x]])
                    t[i].reset(x);
            if (!hav[x]) {
                while (~head[x] && del[head[x]])
                    head[x] = e[head[x]].nxt;
                for (int i = head[x], pre = i; ~i; pre = i, i = e[i].nxt) {
                    if (del[e[i].id]) {
                        e[pre].nxt = e[i].nxt;
                        continue;
                    }
                    cnt[e[i].id]++;
                    if (cnt[e[i].id] == 1)
                        que.set(e[i].id);
                }
            }
        } else if (op == '-') {
            int x = read();
            inS[x] = 0;
            for (int i = 1; i <= tot; ++i)
                if (!del[idg[ri[i]][x]])
                    t[i].set(x);
            if (!hav[x]) {
                while (~head[x] && del[head[x]])
                    head[x] = e[head[x]].nxt;
                for (int i = head[x], pre = i; ~i; pre = i, i = e[i].nxt) {
                    if (del[e[i].id]) {
                        e[pre].nxt = e[i].nxt;
                        continue;
                    }
                    cnt[e[i].id]--;
                    if (cnt[e[i].id] == 1)
                        que.set(e[i].id);
                }
            }
        } else {
            int ans = que.ask();
            if (!ans) {
                for (int i = 1; i <= tot; ++i) {
                    if (!inS[ri[i]])continue;
                    int pos = t[i]._Find_first();
                    if (pos != t[i].size()) {
                        t[i].reset(pos);
                        if (hav[pos])
                            t[hav[pos]].reset(ri[i]);
                        ans = idg[i][pos];
                        break;
                    }
                }
            }
            del[ans] = 1;
            printf("%d\n", ans);
        }
        // for (int i = 1; i <= tot; ++i)
        //     cout << t[i] << '\n';
        // cout << '\n';
    }
}
signed main(signed argc, char const* argv[]) {
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    //=============================================================
    int TxT = 1;
    // TxT = read();
    while (TxT--)
        solve();
    //=============================================================
#ifdef LOCAL
    end :
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 10212kb

input:

4 5
1 2
1 3
1 4
2 3
2 4
10
+ 1
+ 2
?
?
?
?
?
- 2
?
?

output:

3
2
5
4
0
1
0

result:

ok q=10

Test #2:

score: 0
Accepted
time: 0ms
memory: 9784kb

input:

0 0
0

output:


result:

ok q=0

Test #3:

score: 0
Accepted
time: 1ms
memory: 10204kb

input:

0 0
1
?

output:

0

result:

ok q=1

Test #4:

score: -100
Wrong Answer
time: 5ms
memory: 12388kb

input:

1000 2000
1 50
1 88
331 1
1 352
1 497
2 32
2 282
550 2
989 2
334 3
3 665
4 38
4 69
4 343
4 451
589 4
917 4
89 5
5 162
675 5
681 6
7 22
127 7
7 592
7 672
787 7
8 310
107 9
9 137
184 9
9 244
378 9
446 9
9 658
883 9
65 10
75 10
414 10
10 468
686 10
245 11
269 11
11 386
403 11
493 11
394 12
493 12
565 1...

output:

1546
1161
936
990
989
987
986
1911
1979
1828
1266
1176
867
512
653
651
405
1816
1542
847
1933
1589
1277
674
1567
1482
398
62
1521
1520
269
265
264
263
262
1951
1109
495
1191
334
1751
925
218
1581
768
555
1776
108
59
58
953
275
702
1461
1427
1240
1990
1958
779
1837
1128
935
1550
5
1
1989
591
590
589
...

result:

wrong answer Edge exists, but not found