QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#640761 | #6260. Historic Exhibition | BaBamBa# | WA | 1ms | 4104kb | C++17 | 4.0kb | 2024-10-14 15:50:19 | 2024-10-14 15:50:19 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct HopcroftKarp
{
int n, m;
vector<vector<int>> g;
vector<int> dst, le, ri;
vector<char> visit, track;
HopcroftKarp(int n, int m) : n(n), m(m) { clear(); }
void clear()
{
g = vector<vector<int>>(n);
dst = vector<int>(n, 0);
le = vector<int>(n, -1);
ri = vector<int>(m, -1);
visit = vector<char>(n, 0);
track = vector<char>(n + m, 0);
}
void add_edge(int s, int e)
{
g[s].push_back(e);
}
bool bfs()
{
bool res = false;
queue<int> que;
fill(dst.begin(), dst.end(), 0);
for (int i = 0; i < n; i++)
if (le[i] == -1)
que.push(i), dst[i] = 1;
while (!que.empty())
{
int v = que.front();
que.pop();
for (auto i : g[v])
{
if (ri[i] == -1)
res = true;
else if (!dst[ri[i]])
dst[ri[i]] = dst[v] + 1, que.push(ri[i]);
}
}
return res;
}
bool dfs(int v)
{
if (visit[v])
return false;
visit[v] = 1;
for (auto i : g[v])
{
if (ri[i] == -1 || !visit[ri[i]] && dst[ri[i]] == dst[v] + 1 && dfs(ri[i]))
{
le[v] = i;
ri[i] = v;
return true;
}
}
return false;
}
int maximum_matching()
{
int res = 0;
fill(le.begin(), le.end(), -1);
fill(ri.begin(), ri.end(), -1);
while (bfs())
{
fill(visit.begin(), visit.end(), 0);
for (int i = 0; i < n; i++)
if (le[i] == -1)
res += dfs(i);
}
return res;
}
vector<pair<int, int>> maximum_matching_edges()
{
int matching = maximum_matching();
vector<pair<int, int>> edges;
edges.reserve(matching);
for (int i = 0; i < n; i++)
if (le[i] != -1)
edges.emplace_back(i, le[i]);
return edges;
}
void dfs_track(int v)
{
if (track[v])
return;
track[v] = 1;
for (auto i : g[v])
track[n + i] = 1, dfs_track(ri[i]);
}
tuple<vector<int>, vector<int>, int> minimum_vertex_cover()
{
int matching = maximum_matching();
fill(track.begin(), track.end(), 0);
for (int i = 0; i < n; i++)
if (le[i] == -1)
dfs_track(i);
vector<int> lv, rv;
for (int i = 0; i < n; i++)
if (!track[i])
lv.push_back(i);
for (int i = 0; i < m; i++)
if (track[n + i])
rv.push_back(i);
assert(lv.size() + rv.size() == matching);
return {lv, rv, lv.size() + rv.size()};
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll N, M;
cin >> N >> M;
HopcroftKarp a(10100, 10100);
vector<pair<ll, ll>> inp(N);
for (int i = 0; i < N; i++)
{
ll x, y;
cin >> x >> y;
inp[i] = {x, y};
}
vector<ll> m;
for (int i = 0; i < M; i++)
{
ll a;
cin >> a;
m.push_back(a);
}
sort(m.begin(), m.end());
for (int i = 0; i < N; i++)
{
if (binary_search(m.begin(), m.end(), inp[i].first))
a.add_edge(inp[i].first - 1, i);
if (inp[i].first != inp[i].second)
{
if (binary_search(m.begin(), m.end(), inp[i].second))
a.add_edge(inp[i].second - 1, i);
}
}
vector<pair<int, int>> ans = a.maximum_matching_edges();
if (ans.size() < M)
{
cout << "impossible";
return 0;
}
vector<ll> t;
sort(ans.begin(), ans.end());
for (auto &x : ans)
cout << x.second + 1 << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 4056kb
input:
4 3 1 2 4 5 2 3 2 2 1 2 3
output:
1 4 3
result:
ok correct
Test #2:
score: 0
Accepted
time: 1ms
memory: 3772kb
input:
2 2 1 1 2 3 1 1
output:
impossible
result:
ok impossible
Test #3:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
2 3 9 8 4 5 4 9 5
output:
impossible
result:
ok impossible
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 4104kb
input:
1000 1000 141 140 239 240 380 380 114 115 345 345 60 60 341 340 224 223 400 399 125 124 163 162 53 53 62 62 326 326 36 36 91 92 187 186 48 49 123 123 232 233 275 274 374 373 321 322 251 250 347 348 221 222 64 65 143 144 65 65 135 136 209 208 336 336 118 117 189 190 87 86 58 58 66 67 185 185 289 288 ...
output:
impossible
result:
wrong output format Expected integer, but "impossible" found