백준5398 틀렸습니다

문제 링크

  • http://icpc.me/5398

문제 출처

  • 2010 BAPC J번

사용 알고리즘

  • 이분 매칭

풀이

각 단어들을 정점으로 잡고, 충돌하는 단어들을 간선으로 이어준 그래프를 생각해봅시다.
가로 단어들은 절대로 서로 충돌할 일이 없고, 세로 단어들도 마찬가지입니다.

이분 그래프가 만들어지게 되고, 최대 매칭 개수만큼만 제거해주면 크로스워드 퍼즐을 완성할 수 있습니다.

전체 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;

typedef pair<char, int> pci;

pci arr[2020][2020][2];
int n, m;

vector<int> g[1010];
const int bias = 500;

void init(){
	for(int i=0; i<2020; i++){
		for(int j=0; j<2020; j++){
			arr[i][j][0] = arr[i][j][1] = {'
#include <bits/stdc++.h>
using namespace std;

typedef pair<char, int> pci;

pci arr[2020][2020][2];
int n, m;

vector<int> g[1010];
const int bias = 500;

void init(){
	for(int i=0; i<2020; i++){
		for(int j=0; j<2020; j++){
			arr[i][j][0] = arr[i][j][1] = {'\0', 0};
		}
	}
	for(int i=0; i<1010; i++) g[i].clear();
}

int par[1010], chk[1010];

int dfs(int v){
	chk[v] = 1;
	for(auto i : g[v]){
		if(chk[i]) continue;
		chk[i] = 1;
		if(par[i] == -1 || dfs(par[i])){
			par[i] = v; return 1;
		}
	}
	return 0;
}

void solve(){
	init();
	cin >> n >> m;
	for(int i=1; i<=n; i++){
		int a, b; string s; cin >> a >> b >> s;
		swap(a, b);
		for(int j=0; j<s.size(); j++) arr[a][b+j][0] = {s[j], i};
	}
	for(int i=1; i<=m; i++){
		int a, b; string s; cin >> a >> b >> s; swap(a, b);
		for(int j=0; j<s.size(); j++){
			arr[a+j][b][1] = {s[j], i+bias};
			pci x = arr[a+j][b][0];
			pci y = arr[a+j][b][1];
			if(x.first != '\0' && y.first != '\0' && x.first != y.first){
				g[x.second].push_back(y.second);
				//cout << x.second << " " << y.second << "\n";
			}
		}
	}

	int ans = 0;
	memset(par, -1, sizeof par);
	for(int i=1; i<=n; i++){
		memset(chk, 0, sizeof chk);
		ans += dfs(i);
	}
	cout << n+m-ans << "\n";
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t; cin >> t;
	while(t--) solve();
}
'
, 0}; } } for(int i=0; i<1010; i++) g[i].clear(); } int par[1010], chk[1010]; int dfs(int v){ chk[v] = 1; for(auto i : g[v]){ if(chk[i]) continue; chk[i] = 1; if(par[i] == -1 || dfs(par[i])){ par[i] = v; return 1; } } return 0; } void solve(){ init(); cin >> n >> m; for(int i=1; i<=n; i++){ int a, b; string s; cin >> a >> b >> s; swap(a, b); for(int j=0; j<s.size(); j++) arr[a][b+j][0] = {s[j], i}; } for(int i=1; i<=m; i++){ int a, b; string s; cin >> a >> b >> s; swap(a, b); for(int j=0; j<s.size(); j++){ arr[a+j][b][1] = {s[j], i+bias}; pci x = arr[a+j][b][0]; pci y = arr[a+j][b][1]; if(x.first != '
#include <bits/stdc++.h>
using namespace std;

typedef pair<char, int> pci;

pci arr[2020][2020][2];
int n, m;

vector<int> g[1010];
const int bias = 500;

void init(){
	for(int i=0; i<2020; i++){
		for(int j=0; j<2020; j++){
			arr[i][j][0] = arr[i][j][1] = {'\0', 0};
		}
	}
	for(int i=0; i<1010; i++) g[i].clear();
}

int par[1010], chk[1010];

int dfs(int v){
	chk[v] = 1;
	for(auto i : g[v]){
		if(chk[i]) continue;
		chk[i] = 1;
		if(par[i] == -1 || dfs(par[i])){
			par[i] = v; return 1;
		}
	}
	return 0;
}

void solve(){
	init();
	cin >> n >> m;
	for(int i=1; i<=n; i++){
		int a, b; string s; cin >> a >> b >> s;
		swap(a, b);
		for(int j=0; j<s.size(); j++) arr[a][b+j][0] = {s[j], i};
	}
	for(int i=1; i<=m; i++){
		int a, b; string s; cin >> a >> b >> s; swap(a, b);
		for(int j=0; j<s.size(); j++){
			arr[a+j][b][1] = {s[j], i+bias};
			pci x = arr[a+j][b][0];
			pci y = arr[a+j][b][1];
			if(x.first != '\0' && y.first != '\0' && x.first != y.first){
				g[x.second].push_back(y.second);
				//cout << x.second << " " << y.second << "\n";
			}
		}
	}

	int ans = 0;
	memset(par, -1, sizeof par);
	for(int i=1; i<=n; i++){
		memset(chk, 0, sizeof chk);
		ans += dfs(i);
	}
	cout << n+m-ans << "\n";
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t; cin >> t;
	while(t--) solve();
}
'
&& y.first != '
#include <bits/stdc++.h>
using namespace std;

typedef pair<char, int> pci;

pci arr[2020][2020][2];
int n, m;

vector<int> g[1010];
const int bias = 500;

void init(){
	for(int i=0; i<2020; i++){
		for(int j=0; j<2020; j++){
			arr[i][j][0] = arr[i][j][1] = {'\0', 0};
		}
	}
	for(int i=0; i<1010; i++) g[i].clear();
}

int par[1010], chk[1010];

int dfs(int v){
	chk[v] = 1;
	for(auto i : g[v]){
		if(chk[i]) continue;
		chk[i] = 1;
		if(par[i] == -1 || dfs(par[i])){
			par[i] = v; return 1;
		}
	}
	return 0;
}

void solve(){
	init();
	cin >> n >> m;
	for(int i=1; i<=n; i++){
		int a, b; string s; cin >> a >> b >> s;
		swap(a, b);
		for(int j=0; j<s.size(); j++) arr[a][b+j][0] = {s[j], i};
	}
	for(int i=1; i<=m; i++){
		int a, b; string s; cin >> a >> b >> s; swap(a, b);
		for(int j=0; j<s.size(); j++){
			arr[a+j][b][1] = {s[j], i+bias};
			pci x = arr[a+j][b][0];
			pci y = arr[a+j][b][1];
			if(x.first != '\0' && y.first != '\0' && x.first != y.first){
				g[x.second].push_back(y.second);
				//cout << x.second << " " << y.second << "\n";
			}
		}
	}

	int ans = 0;
	memset(par, -1, sizeof par);
	for(int i=1; i<=n; i++){
		memset(chk, 0, sizeof chk);
		ans += dfs(i);
	}
	cout << n+m-ans << "\n";
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t; cin >> t;
	while(t--) solve();
}
'
&& x.first != y.first){ g[x.second].push_back(y.second); //cout << x.second << " " << y.second << "\n"; } } } int ans = 0; memset(par, -1, sizeof par); for(int i=1; i<=n; i++){ memset(chk, 0, sizeof chk); ans += dfs(i); } cout << n+m-ans << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) solve(); }