QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#623635#9424. Stop the Castle 2IllusionaryDominanceRE 0ms26960kbC++204.3kb2024-10-09 13:31:562024-10-09 13:31:56

Judging History

This is the latest submission verdict.

  • [2024-10-09 13:31:56]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 26960kb
  • [2024-10-09 13:31:56]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;

const int MAX_N = 200000 + 5;
const int MAX_M = 500000 + 5;

int N, M, K, tot;
struct Node{
    int x, y, id;
}node[MAX_N];
unordered_set <int> edge[MAX_N], redge[MAX_N];

void Build(int x, int y) {
    edge[x].insert(y);
    redge[y].insert(x);
}

void solve() {
    for (int i = 1; i <= M; i ++) {
        edge[i].clear();
    }
    for (int i = 1; i <= tot; i ++) {
        redge[i].clear();
    }
    tot = 0;
    
    cin >> N >> M >> K;
    for (int i = 1; i <= N; i ++) {
        cin >> node[i].x >> node[i].y;
        node[i].id = -i;
    }
    for (int i = 1; i <= M; i ++) {
        cin >> node[i + N].x >> node[i + N].y;
        node[i + N].id = i;
    }
    auto cmp_by_x = [&](const Node &i, const Node &j) -> bool {
        return i.x < j.x || (i.x == j.x && i.y < j.y);
    };
    auto cmp_by_y = [&](const Node &i, const Node &j) -> bool {
        return i.y < j.y || (i.y == j.y && i.x < j.x);
    };
    sort(node + 1, node + N + M + 1, cmp_by_x);
    for (int l = 1, r; l <= N + M; l = r) {
        for (r = l; r <= N + M && node[l].x == node[r].x; r ++);
        for (int i = l, j; i < r; i = j) {
            for ( ; i < r && node[i].id > 0; i ++);
            for (j = i + 1; j < r && node[j].id > 0; j ++);
            if (i < r && j < r) {
                tot ++;
                for (int k = i + 1; k < j; k ++) {
                    Build(node[k].id, tot);
                }
            }
        }
    }
    sort(node + 1, node + N + M + 1, cmp_by_y);
    for (int l = 1, r; l <= N + M; l = r) {
        for (r = l; r <= N + M && node[l].y == node[r].y; r ++);
        for (int i = l, j; i < r; i = j) {
            for ( ; i < r && node[i].id > 0; i ++);
            for (j = i + 1; j < r && node[j].id > 0; j ++);
            if (i < r && j < r) {
                tot ++;
                for (int k = i + 1; k < j; k ++) {
                    Build(node[k].id, tot);
                }
            }
        }
    }
    
    auto Priority = [&](int u) -> int {
        if ((int)edge[u].size() < 2) return -(int)edge[u].size();
        for (auto v : edge[u]) {
            int cnt = 0;
            for (auto w : redge[v]) {
                if ((int)edge[w].size() == 2) cnt ++;
            }
            if (cnt == 1) return -3;
        }
        return -2;
    };
    
    set <pii> s;
    for (int i = 1; i <= M; i ++) {
        s.insert(pair(Priority(i), i));
    }
    
    int ans = tot;
    while ((int)s.size() > K) {
        auto it = s.begin();
        auto [priority, u] = *it;
        s.erase(it);
        assert(Priority(u) == priority);
        static pii q[MAX_N];
        int hd = 0, tl = -1;
        for (auto v : edge[u]) {
            for (auto w : redge[v]) {
                if (w != u) {
                    q[++ tl] = pair(v, w);
                    s.erase(pair(Priority(w), w));
                }
            }
        }
        while (hd <= tl) {
            auto [v, w] = q[hd ++];
            int z = -1;
            if ((int)edge[w].size() == 2) {
                for (auto x : edge[w]) {
                    if (x != v)  {
                        int cnt = 0;
                        for (auto y : edge[x]) {
                            if (y != w && (int)edge[y].size() == 2) {
                                cnt ++; z = y;
                            }
                        }
                        if (cnt > 1) z = -1;
                    }
                }
            }
            if (z != -1) s.erase(pair(Priority(z), z));
            edge[w].erase(v);
            if (z != -1) s.insert(pair(Priority(z), z));
        }
        for (auto v : edge[u]) {
            for (auto w : redge[v]) {
                if (w != u) {
                    s.insert(pair(Priority(w), w));
                }
            }
            redge[v].clear();
        }
        edge[u].clear();
        if (priority < -1) {
            ans -= 2;
        }else if (priority) {
            ans --;
        }
    }
    cout << ans << '\n';
    for (auto [priority, u] : s) cout << u << ' ';
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
	int T; cin >> T;
	while (T --) solve();
    
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
8 6 4
1 3
2 1
2 6
4 1
4 7
6 1
6 3
6 6
2 3
3 1
4 3
4 6
5 2
6 4
3 2 1
10 12
10 10
10 11
1 4
1 5
1 3 2
1 1
2 1
2 2
2 3

output:

4
2 6 3 5 
2
2 
0
2 3 

result:

ok ok 3 cases (3 test cases)

Test #2:

score: -100
Runtime Error

input:

1224
11 17 14
7 3
4 2
8 13
3 15
3 4
5 11
10 2
3 3
8 6
7 11
2 3
10 4
1 3
12 1
2 5
11 9
11 6
11 10
8 15
1 5
9 14
4 11
1 6
10 7
7 6
11 4
8 4
1 11
18 3 2
14 8
2 14
13 13
9 12
14 12
5 6
8 1
10 5
8 6
8 9
6 6
7 5
12 11
6 11
13 5
1 10
7 6
14 5
6 15
2 4
11 1
1 6 4
14 14
13 9
9 3
10 12
7 5
8 13
9 14
1 9 8
4 9...

output:


result: