QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#250814 | #1344. Rooks Game | IsaacMoris# | WA | 1ms | 3448kb | C++14 | 2.6kb | 2023-11-13 18:25:17 | 2023-11-13 18:25:17 |
Judging History
answer
#include<iostream>
#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 2e3 + 5;
int n, m;
int par[N];
int find_parent(int v) {
if (v == par[v])return v;
return par[v] = find_parent(par[v]);
}
int x[N], y[N];
struct graph {
int L, R;
vector<vector<int> > adj;
graph(int l, int r) : L(l), R(r), adj(l + 1) {}
void add_edge(int u, int v) {
adj[u].push_back(v + L);
}
int maximum_matching() {
vector<int> mate(L + R + 1, -1), level(L + 1);
function<bool(void)> levelize = [&]() {
queue<int> q;
for (int i = 1; i <= L; i++) {
level[i] = -1;
if (mate[i] < 0)
q.push(i), level[i] = 0;
}
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto i: adj[node]) {
int v = mate[i];
if (v < 0)
return true;
if (level[v] < 0) {
level[v] = level[node] + 1;
q.push(v);
}
}
}
return false;
};
function<bool(int)> augment = [&](int node) {
for (auto i: adj[node]) {
int v = mate[i];
if (v < 0 || (level[v] > level[node] && augment(v))) {
mate[node] = i;
mate[i] = node;
return true;
}
}
return false;
};
int match = 0;
while (levelize())
for (int i = 1; i <= L; i++)
if (mate[i] < 0 && augment(i))
match++;
return match;
}
};
void doWork() {
cin >> n >> m;
graph g(n, n);
for (int i = 1; i <= m; i++) {
cin >> x[i] >> y[i];
par[i] = i;
g.add_edge(x[i], y[i]);
for (int j = 1; j < i; j++) {
if (x[i] == x[j] || y[i] == y[j]) {
par[i] = find_parent(par[j]);
}
}
}
int ans2 = m;
for (int i = 1; i <= m; i++) {
if (find_parent(i) == i) {
ans2--;
}
}
cout << m - g.maximum_matching() << " " << ans2;
}
int main() {
IO
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ": ";
doWork();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3408kb
input:
8 3 1 1 1 8 8 8
output:
1 2
result:
ok 2 number(s): "1 2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3444kb
input:
8 4 1 1 1 8 8 8 8 1
output:
2 3
result:
ok 2 number(s): "2 3"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3444kb
input:
5 5 1 1 2 2 3 3 4 4 5 5
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3448kb
input:
100 1 100 100
output:
0 0
result:
ok 2 number(s): "0 0"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3384kb
input:
10 12 1 2 1 4 1 6 3 6 5 6 10 7 8 7 6 7 4 7 2 7 7 3 9 5
output:
7 8
result:
ok 2 number(s): "7 8"
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 3444kb
input:
9 12 2 3 5 8 9 7 9 9 1 2 7 8 9 1 3 3 2 9 4 4 4 6 1 6
output:
6 7
result:
wrong answer 2nd numbers differ - expected: '9', found: '7'