# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
95441 | choikiwon | 결혼 문제 (IZhO14_marriage) | C++17 | 1569 ms | 5864 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 100010;
int N, M, K;
vector<int> adj[MN], U, V;
int matched[MN], chk[MN], who[MN], vis[MN];
bool dfs(int u, int t, int lb, int ub) {
vis[u] = 1;
if(t && !chk[u]) {
chk[u] = 1;
return true;
}
for(int i = 0; i < adj[u].size(); i++) {
int e = adj[u][i];
int v = U[e] + V[e] - u;
if(v < N && (v < lb || ub < v)) continue;
if(vis[v]) continue;
if(matched[e] == t && dfs(v, t ^ 1, lb, ub)) {
if(!t) {
matched[e] = 1;
who[u] = e;
who[v] = e;
}
return true;
}
}
return false;
}
int main() {
scanf("%d %d %d", &N, &M, &K);
for(int i = 0; i < K; i++) {
int a, b; scanf("%d %d", &a, &b);
a--; b--;
adj[a].push_back(i);
adj[b + N].push_back(i);
U.push_back(a);
V.push_back(b + N);
}
ll ans = 0;
int matching = 0;
int j = 0;
for(int i = 0; i < N; i++) {
while(j < N && matching < M) {
for(int k = 0; k < N + M; k++) vis[k] = 0;
if(dfs(j, 0, i, j)) {
matching++;
chk[j] = 1;
}
j++;
}
if(matching < M) break;
ans += N - j + 1;
if(chk[i]) {
matching--;
int v = U[ who[i] ] + V[ who[i] ] - i;
matched[ who[i] ] = 0;
chk[v] = 0;
for(int k = 0; k < N + M; k++) vis[k] = 0;
if(dfs(v, 0, i + 1, j)) {
matching++;
chk[v] = 1;
}
}
}
printf("%lld", ans);
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |