forked from volzkzg/sgu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
190.cpp
145 lines (139 loc) · 3.2 KB
/
190.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define maxn 41
using namespace std;
struct node{
int v;
node *next;
}*g[maxn*maxn];
int row[maxn*maxn],col[maxn*maxn];
struct pospoint{
int x,y;
}posPoint[maxn*maxn];
bool f[maxn][maxn];
bool visit[maxn][maxn];
int last[maxn*maxn];
bool use[maxn*maxn];
int point[maxn][maxn];
int left[maxn*maxn];
int right[maxn*maxn];
int leftNum,rightNum;
int rowNum,colNum;
int n,p,ans;
bool join[maxn*maxn];
void insert(int a,int b){
// printf("%d %d\n",a,b);
static node buf[maxn*maxn*4];
static int top = 0;
node *x = &buf[top++];
x->v = b;x->next =g[a];g[a] = x;
}
bool augment(int x){
node * t = g[x];
while (t!=0){
if (use[t->v]){
use[t->v] = false;
if (last[t->v] == 0 ||
augment(last[t->v])){
last[t->v] = x;
return true;
}
}
t = t->next;
}
return false;
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
//input
scanf("%d %d\n",&n,&p);
int black,white;
f[1][1] = false;
for (int i=2;i<=n;++i) f[1][i] = !f[1][i-1];
for (int i=2;i<=n;++i)
for (int j=1;j<=n;++j)
f[i][j] = !f[i-1][j];
black = white = 0;
for (int i=1,x,y;i<=p;++i){
scanf("%d %d\n",&x,&y);
visit[x][y] = true;
if (f[x][y]) ++white; else ++black;
}
if (white != black || black+white == n*n || (n % 2==1)){
printf("No\n");
return 0;
}
//makegraph
memset(g,0,sizeof(g));
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j){
point[i][j] = (i-1)*n+j;
posPoint[point[i][j]].x = i;
posPoint[point[i][j]].y = j;
}memset(join,false,sizeof(join));
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
if (!visit[i][j] && !f[i][j]){
left[++leftNum] = point[i][j];
if (i-1>0 && !visit[i-1][j]){
insert(point[i][j],point[i-1][j]);
if (join[point[i-1][j]] ==false){
right[++rightNum] = point[i-1][j];
join[point[i-1][j]] = true;
}
}
if (i+1<=n && !visit[i+1][j]){
insert(point[i][j],point[i+1][j]);
if (join[point[i+1][j]] == false){
right[++rightNum] = point[i+1][j];
join[point[i+1][j]] = true;
}
}
if (j-1>0 && !visit[i][j-1]){
insert(point[i][j],point[i][j-1]);
if (join[point[i][j-1]] == false){
right[++rightNum] = point[i][j-1];
join[point[i][j-1]] = true;
}
}
if (j+1<=n && !visit[i][j+1]){
insert(point[i][j],point[i][j+1]);
if (join[point[i][j+1]] == false){
right[++rightNum] = point[i][j+1];
join[point[i][j+1]] = true;
}
}
}
//solve
for (int i=1;i<=leftNum;++i){
memset(use,true,sizeof(use));
if (augment(left[i])) ++ans;
}
if (ans != (n*n-black*2)/2){
printf("No\n");
return 0;
}else{
for (int i=1;i<=rightNum;++i){
int p = last[right[i]];
int q = right[i];
if (abs(p-q) == 1)
row[++rowNum] = min(p,q);
else
col[++colNum] = min(p,q);
}
sort(row+1,row+rowNum+1);
sort(col+1,col+colNum+1);
//print
printf("Yes\n");
printf("%d\n",colNum);
for (int i=1;i<=colNum;++i)
printf("%d %d\n",posPoint[col[i]].x,posPoint[col[i]].y);
printf("%d\n",rowNum);
for (int i=1;i<=rowNum;++i)
printf("%d %d\n",posPoint[row[i]].x,posPoint[row[i]].y);
return 0;
}
}