forked from AhmadElsagheer/UVa-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheDogTask_UVa670.java
More file actions
138 lines (116 loc) · 3.13 KB
/
TheDogTask_UVa670.java
File metadata and controls
138 lines (116 loc) · 3.13 KB
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
package v006;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class TheDogTask_UVa670 {
static final double EPS = 1e-9;
static int n, m, match[];
static ArrayList<Integer>[] adjList;
static boolean[] vis;
static int maxMatches()
{
match = new int[m];
Arrays.fill(match, -1);
int matches = 0;
for(int i = 0; i < n; ++i)
{
vis = new boolean[n];
matches += aug(i);
}
return matches;
}
static int aug(int u)
{
vis[u] = true;
for(int v: adjList[u])
if(match[v] == -1 || !vis[match[v]] && aug(match[v]) == 1)
{
match[v] = u;
return 1;
}
return 0;
}
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0)
{
int N = sc.nextInt(), M = sc.nextInt();
Point[] meet = new Point[N], inter = new Point[M];
for(int i = 0; i < N; ++i)
meet[i] = new Point(sc.nextInt(), sc.nextInt());
for(int i = 0; i < M; ++i)
inter[i] = new Point(sc.nextInt(), sc.nextInt());
n = N - 1;
m = M;
adjList = new ArrayList[n];
for(int i = 0; i < n; ++i)
adjList[i] = new ArrayList<Integer>();
for(int i = 0; i < n; ++i)
{
Point p = meet[i], q = meet[i+1];
for(int j = 0; j < m; ++j)
if(inter[j].canReach(p, q))
adjList[i].add(j);
}
int matches = maxMatches();
int[] interAfter = new int[n];
Arrays.fill(interAfter, -1);
for(int i = 0; i < m; ++i)
if(match[i] != -1)
interAfter[match[i]] = i;
sb.append(N + matches + "\n");
for(int i = 0; i < n; ++i)
{
if(i != 0)
sb.append(" ");
Point p = meet[i];
sb.append(p.x + " " + p.y);
if(interAfter[i] != -1)
{
p = inter[interAfter[i]];
sb.append(" " + p.x + " " + p.y);
}
}
sb.append(" " + meet[N-1].x + " " + meet[N-1].y + "\n");
if(tc != 0)
sb.append("\n");
}
out.print(sb);
out.flush();
out.close();
}
static class Point
{
int x, y;
Point(int a, int b) { x = a; y = b; }
boolean canReach(Point p, Point q) { return p.dist(q) * 2 + EPS > dist(p) + dist(q); }
double dist(Point p) { return Math.sqrt(sq(x - p.x) + sq(y - p.y)); }
int sq(int x) { return x * x; }
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException { return Double.parseDouble(next()); }
public boolean ready() throws IOException {return br.ready();}
}
}