QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#592337#7181. Graph Cutsphuong2222TL 6ms10488kbC++145.6kb2024-09-26 21:57:522024-09-26 21:57:52

Judging History

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

  • [2024-09-26 21:57:52]
  • 评测
  • 测评结果:TL
  • 用时:6ms
  • 内存:10488kb
  • [2024-09-26 21:57:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using lli = long long;
const int maxN = 1e5 + 5;
const int maxB = 318;
const int maxB1 = 318;
vector<int> type1,type0,adj[maxN];
int type[maxN],deg[maxN],n,m,val[maxN],l[maxB1],r[maxB1],id[maxN];
int sum0[maxB1],val0[maxN],id0[maxN];
int val1[maxN],id1[maxN],id11[maxN];
int id01[maxB1][maxN],sum01[2][maxB1][maxB1],sumch[2][maxB1],val01[2][maxB1][maxN];
int del[maxN];
vector<int> type01[maxB1],vex1;
struct Tedge
{
    int x,y;
}
e[maxN];
void presolve()
{
    int cntb = 0;
    for (int i = 0;i < maxN;++i)
    {
        if (i % maxB == 0) l[++cntb] = i;
        if (i % maxB == (maxB - 1) || i == maxN - 1) r[cntb] = i;
        id[i] = cntb;
    }
}
void finddata()
{
    for (int i = 1;i <= n;++i)
    {
        type[i] = deg[i] > maxB;
        if (type[i])
        {
            id11[i] = vex1.size();
            vex1.push_back(i);
        }
    }
    for (int i = 1;i <= m;++i)
    {
        if (type[e[i].x] && type[e[i].y])
        {
            id1[i] = type1.size();
            type1.push_back(i);
        }
        if (type[e[i].x] == 0 && type[e[i].y] == 0)
        {
            id0[i] = type0.size();
            type0.push_back(i);
        }
    }
    for (int i = 1;i <= m;++i)
    {
        if (type[e[i].x] && !type[e[i].y])
        {
            id01[id11[e[i].x]][e[i].y] = type01[id11[e[i].x]].size();
            val01[1][id11[e[i].x]][type01[id11[e[i].x]].size()] = 1;
            sum01[1][id11[e[i].x]][id[type01[id11[e[i].x]].size()]]++;
            sumch[1][id11[e[i].x]]++;
            type01[id11[e[i].x]].push_back(i);
        }
        if (type[e[i].y] && !type[e[i].x])
        {
            id01[id11[e[i].y]][e[i].x] = type01[id11[e[i].y]].size();
            val01[1][id11[e[i].y]][type01[id11[e[i].y]].size()] = 1;
            sum01[1][id11[e[i].y]][id[type01[id11[e[i].y]].size()]]++;
            sumch[1][id11[e[i].y]]++;
            type01[id11[e[i].y]].push_back(i);
        }

    }
}
void input()
{
    cin >> n >> m;
    fill(deg + 1,deg + n + 1,0);
    fill(val + 1,val + n + 1,0);
    fill(id0 + 1,id0 + n + 1,0);
    for (int i = 1;i <= m;++i)
    {
        cin >> e[i].x >> e[i].y;
        deg[e[i].x]++;deg[e[i].y]++;
        adj[e[i].x].push_back(i);
        adj[e[i].y].push_back(i);
    }
    presolve();
    finddata();
}
void query()
{
    for (int i : type1)
        if (val1[id1[i]] == 1 && del[i] == 0)
        {
            del[i] = 1;
            cout << i << "\n";
            return;
        }
    int cntb = (type0.size() + maxB - 1) / maxB;
    for (int idb = 1;idb <= cntb;++idb)
    {
        if (sum0[idb] > 0)
        {
            for (int i = l[idb];i <= min(r[idb],int(type0.size()));++i)
                if (val0[id0[type0[i]]] == 1)
                {
                    del[type0[i]] = 1;
                    val0[i] = 0;
                    sum0[idb]--;
                    cout << type0[i] << "\n";
                    return;
                }
        }
    }
    for (int u : vex1)
    {
        if (sumch[val[u]][id11[u]] > 0)
        {
            int cntb = (type01[id11[u]].size() + maxB - 1) / maxB;
            for (int idb = 1;idb <= cntb;++idb)
            {
                if (sum01[val[u]][id11[u]][idb] > 0)
                {
                    for (int i = l[idb];i <= min(r[idb],int(type01[id11[u]].size()));++i)
                        if (val01[val[u]][id11[u]][i] == 1)
                        {
                            del[type01[id11[u]][i]] = 1;
                            val01[0][id11[u]][i] = 0;
                            val01[1][id11[u]][i] = 0;
                            cout << type01[id11[u]][i] << "\n";
                            return;
                        }
                }
            }
        }
    }
    cout << 0 << "\n";
}
void update(int u)
{
    val[u] ^= 1;
    if (type[u])
    {
        for (int i : type1)
            if ((e[i].x == u || e[i].y == u) && del[i] != 1) val1[id1[i]] ^= 1;
    }
    else
    {
        for (int i : adj[u])
        {
            if (del[i] == 1) continue;
            int v = e[i].x + e[i].y - u;
            if (!type[v])
            {
                val0[id0[i]] = (val0[id0[i]] ^ 1);
                if (val0[id0[i]] == 1) sum0[id[id0[i]]]++;
                else sum0[id[id0[i]]]--;
            }
            else
            {
                val01[1][id11[v]][id01[id11[v]][u]] ^= 1;
                val01[0][id11[v]][id01[id11[v]][u]] ^= 1;
                if (val01[1][id11[v]][id01[id11[v]][u]] == 1)
                {
                    sum01[1][id11[v]][id[id01[id11[v]][u]]]++;
                    sumch[1][id11[v]]++;
                }
                else
                {
                    sum01[1][id11[v]][id[id01[id11[v]][u]]]--;
                    sumch[1][id11[v]]--;
                }
                if (val01[0][id11[v]][id01[id11[v]][u]] == 1)
                {
                    sum01[0][id11[v]][id[id01[id11[v]][u]]]++;
                    sumch[0][id11[v]]++;
                }
                else
                {
                    sum01[0][id11[v]][id[id01[id11[v]][u]]]--;
                    sumch[0][id11[v]]--;
                }
            }
        }
    }
}
void solve()
{
    int q;
    cin >> q;
    while (q--)
    {
        char c;
        int u;
        cin >> c;
        if (c == '+' || c == '-') {cin >> u;update(u);}
        else query();
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    input();
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

2
3
4
5
0
1
0

result:

ok q=10

Test #2:

score: 0
Accepted
time: 2ms
memory: 8216kb

input:

0 0
0

output:


result:

ok q=0

Test #3:

score: 0
Accepted
time: 2ms
memory: 8516kb

input:

0 0
1
?

output:

0

result:

ok q=1

Test #4:

score: 0
Accepted
time: 6ms
memory: 10488kb

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:

1
4
5
8
9
10
12
14
16
18
19
25
27
29
33
38
39
40
42
47
48
49
50
56
58
59
62
63
67
69
70
71
73
75
79
81
82
83
84
87
89
91
94
97
101
103
104
106
107
108
109
110
113
114
115
118
120
121
122
125
126
129
130
131
132
133
134
135
137
145
147
148
34
149
152
153
154
155
156
157
159
160
163
167
171
105
173
17...

result:

ok q=100000

Test #5:

score: -100
Time Limit Exceeded

input:

447 99681
2 1
1 3
4 1
1 5
1 6
1 7
1 8
9 1
10 1
1 11
1 12
1 13
1 14
1 15
1 16
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
1 26
27 1
28 1
1 29
30 1
31 1
1 32
33 1
1 34
1 35
36 1
37 1
38 1
39 1
40 1
1 41
1 42
43 1
44 1
45 1
46 1
1 47
48 1
49 1
1 50
1 51
1 52
53 1
54 1
55 1
1 56
57 1
1 58
59 1
60 1
1 6...

output:


result: