QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#611147 | #9424. Stop the Castle 2 | Macesuted | RE | 136ms | 8876kb | C++23 | 4.5kb | 2024-10-04 19:37:07 | 2024-10-04 19:37:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define maxn 100005
typedef pair<int, int> pii;
class Dinic {
private:
struct Edge {
int to, cap, rev, id;
};
vector<vector<Edge>> graph;
vector<vector<Edge>::iterator> cur;
vector<int> dist;
queue<int> que;
int n, S, T;
bool bfs(void) {
for (int i = 1; i <= n; i++) dist[i] = INT_MAX, cur[i] = graph[i].begin();
que.push(S), dist[S] = 0;
while (!que.empty()) {
int p = que.front();
que.pop();
for (auto i : graph[p])
if (dist[i.to] > dist[p] + 1 && i.cap) dist[i.to] = dist[p] + 1, que.push(i.to);
}
return dist[T] != INT_MAX;
}
int dfs(int p, int rest) {
if (p == T) return rest;
int used = 0, c;
for (auto i = cur[p]; i != graph[p].end() && rest; i++) {
cur[p] = i;
if (!i->cap || dist[i->to] != dist[p] + 1) continue;
if (!(c = dfs(i->to, min(rest, i->cap)))) dist[i->to] = -1;
used += c, rest -= c, i->cap -= c, graph[i->to][i->rev].cap += c;
}
return used;
}
public:
vector<bool> avai;
set<int> edges[2];
void resize(int _n) { return n = _n, graph.resize(n + 1), cur.resize(n + 1), dist.resize(n + 1), avai.resize(n + 1); }
void addEdge(int from, int to, int cap, int id) {
return graph[from].push_back(Edge{to, cap, (int)graph[to].size(), id}),
graph[to].push_back(Edge{from, 0, (int)graph[from].size() - 1, 0});
}
int solve(int _S, int _T) {
S = _S, T = _T;
int flow = 0;
while (bfs()) flow += dfs(S, INT_MAX);
for (int i = 1; i <= n; i++)
for (auto j : graph[i])
if (j.id) edges[!j.cap].insert(j.id);
return flow;
}
};
map<int, set<pii>> R, C;
int nx[maxn], ny[maxn];
bool vis[maxn];
vector<int> rec[maxn];
void solve(void) {
R.clear(), C.clear();
int n, m, k;
cin >> n >> m >> k;
Dinic dnc;
int S = 2 * n + 1, T = S + 1;
dnc.resize(T);
for (int i = 1, x, y; i <= n; i++)
cin >> x >> y, R[x].emplace(y, i), C[y].emplace(x, i), dnc.addEdge(S, i, 1, 0), dnc.addEdge(n + i, T, 1, 0);
for (int i = 1; i <= 2 * n; i++) rec[i].clear(), vis[i] = false;
int ans = 0;
for (auto &i : R) ans += (int)i.second.size() - 1;
for (auto &i : C) ans += (int)i.second.size() - 1;
for (int i = 1, x, y; i <= m; i++) {
cin >> x >> y, nx[i] = -1, ny[i] = -1;
if (R.count(x)) {
auto p = R[x].lower_bound({y, 0});
if (p != R[x].begin() && p != R[x].end()) dnc.avai[nx[i] = p->second] = true;
}
if (C.count(y)) {
auto p = C[y].lower_bound({x, 0});
if (p != C[y].begin() && p != C[y].end()) dnc.avai[ny[i] = n + p->second] = true;
}
if (nx[i] != -1 && ny[i] != -1) dnc.addEdge(nx[i], ny[i], 1, i);
}
// cerr << "! " << ans << endl;
int cnt = m - k, flow = dnc.solve(S, T);
// cerr << "### " << flow << endl;
if (flow >= cnt) {
cout << ans - cnt * 2 << endl;
for (int i = 1; i <= m; i++)
if (!dnc.edges[1].count(i) || !cnt)
cout << i << ' ';
else
cnt--;
cout << endl;
return;
}
cnt -= flow, ans -= flow * 2;
for (int i = 1; i <= m; i++)
if (!dnc.edges[1].count(i)) {
if (nx[i] != -1) rec[nx[i]].push_back(i);
if (ny[i] != -1) rec[ny[i]].push_back(i);
} else
vis[nx[i]] = vis[ny[i]] = true;
// cerr << "# " << cnt << endl;
for (int i = 1; i <= m; i++) {
if (dnc.edges[1].count(i)) continue;
if (nx[i] != -1 && !vis[nx[i]] && cnt) vis[nx[i]] = true, dnc.edges[1].insert(i), ans--, cnt--;
if (ny[i] != -1 && !vis[ny[i]] && cnt) vis[ny[i]] = true, dnc.edges[1].insert(i), ans--, cnt--;
}
// cerr << "# " << cnt << endl;
for (int i = 1; i <= m; i++)
if (!dnc.edges[1].count(i) && cnt) dnc.edges[1].insert(i), cnt--;
// cerr << "###" << endl;
cout << ans << endl;
for (int i = 1; i <= m; i++)
if (!dnc.edges[1].count(i)) cout << i << ' ';
cout << endl;
return;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int _ = 1;
cin >> _;
while (_--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3564kb
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 3 5 6 2 2 0 2 3
result:
ok ok 3 cases (3 test cases)
Test #2:
score: 0
Accepted
time: 136ms
memory: 8876kb
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:
7 3 4 5 6 7 8 9 10 11 12 13 15 16 17 15 2 3 0 3 4 5 6 0 2 3 4 5 6 7 8 9 11 1 3 8 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11 12 1 5 6 7 9 10 11 12 8 17 18 19 1 1 2 3 4 5 6 7 8 7 6 8 10 13 14 15 1 10 11 12 13 14 15 16 17 18 19 20 0 1 1 2 3 0 5 6 7 7 8 12 13 14 15 2 10 11 12 13 14 4 3 4 5 6 7 8 ...
result:
ok ok 1224 cases (1224 test cases)
Test #3:
score: -100
Runtime Error
input:
1 86289 95092 40401 911 152 1 270 135 731 347 451 283 224 338 348 166 346 12 385 590 763 939 176 232 405 122 946 397 576 795 823 546 392 33 718 444 598 954 852 185 662 732 539 172 681 386 148 76 495 163 323 711 201 278 363 531 275 66 122 823 983 234 792 102 188 985 423 804 712 419 636 318 331 693 68...