forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3646 - Matrix Transformer.cpp
70 lines (52 loc) · 1.29 KB
/
3646 - Matrix Transformer.cpp
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
70
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector<int> L[200];
int l[200],r[200];
bool visited[200];
bool dfs(int cur){
if(visited[cur]) return false;
visited[cur] = true;
for(int i = L[cur].size() - 1,to;i >= 0;--i){
to = L[cur][i];
if(l[to] == -1 || dfs(l[to])){
r[cur] = to;
l[to] = cur;
return true;
}
}
return false;
}
bool matching(int V){
memset(l,-1,sizeof l);
memset(r,-1,sizeof r);
bool found = true;
while(found){
memset(visited,false,sizeof visited);
found = false;
for(int i = 0;i < V;++i)
if(r[i] == -1)
found |= dfs(i);
}
int cont = 0;
for(int i = 0;i < V;++i)
if(r[i] != -1) ++cont;
return cont == V;
}
int main(){
int N;
char M[200][201];
while(scanf("%d",&N) == 1){
for(int i = 0;i < N;++i)
scanf("%s",M[i]);
for(int i = 0;i < N;++i)
L[i].clear();
for(int i = 0;i < N;++i)
for(int j = 0;j < N;++j)
if(M[i][j] == 'U')
L[i].push_back(j);
puts(matching(N)? "YES" : "NO");
}
return 0;
}