Guest User

Untitled

a guest
Oct 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. public class LOJ_Tshirt{
  6.  
  7.  
  8. public static void main(String[] args) {
  9. InputStream inputStream = System.in;
  10. OutputStream outputStream = System.out;
  11. InputReader in = new InputReader(inputStream);
  12. PrintWriter out = new PrintWriter(outputStream);
  13. CustomType customType=new CustomType();
  14. Task solver = new Task();
  15. solver.solve(1, in, out,customType);
  16. out.close();
  17. }
  18.  
  19.  
  20. static class Task{
  21.  
  22.  
  23. TreeMap<String,Integer> map;
  24.  
  25. String[] str=new String[]{"XXL","XL", "L", "M", "S","XS"};
  26.  
  27.  
  28. Task()
  29. {
  30. map=new TreeMap<>();
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. public void solve(int testNumber, InputReader in, PrintWriter out,CustomType m) {
  40.  
  41. for (int i = 0; i <str.length ; i++) {
  42. map.put(str[i],i+1);
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. int t=in.nextInt(),cs=1;
  52. while(t--!=0){
  53.  
  54. int n=in.nextInt(), e=in.nextInt();
  55.  
  56. EdmondsKarp ff=new EdmondsKarp(e+12);
  57.  
  58. for (int i = 0; i <str.length ; i++) {
  59. ff.addEdge(0,i+1,n);
  60. }
  61.  
  62.  
  63. for (int i = 0; i <e ; i++) {
  64.  
  65. String buff1=in.next(),buff2=in.next();
  66. ff.addEdge(map.get(buff1),i+8,1);
  67. ff.addEdge(map.get(buff2),i+8,1);
  68. ff.addEdge(i+8,e+10,1);
  69. }
  70.  
  71. long mf=ff.getMaxFlow(0,e+10);
  72. out.printf("Case %d: %s\n",cs++,(mf==e)?"YES":"NO");
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79. }
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. static class EdmondsKarp {
  91.  
  92. private long[][] flow;
  93. private long[][] capacity;
  94. private int[] parent;
  95. private boolean[] visited;
  96. private int n;
  97. EdmondsKarp(int numOfVerticles) {
  98. this.n = numOfVerticles;
  99. this.flow = new long[n][n];
  100. this.capacity = new long[n][n];
  101. this.parent = new int[n];
  102. this.visited = new boolean[n];
  103. }
  104.  
  105. public void addEdge(int from, int to, long capacity) {
  106. assert capacity >= 0;
  107. this.capacity[from][to] += capacity;
  108. }
  109.  
  110. public long getMaxFlow(int s, int t) {
  111. while (true) {
  112. final Queue<Integer> Q = new LinkedList<>();
  113. Arrays.fill(visited,false);
  114. Q.add(s);
  115. visited[s] = true;
  116.  
  117. boolean check = false;
  118. int current;
  119. while (!Q.isEmpty()) {
  120. current = Q.poll();
  121. if (current == t) {
  122. check = true;
  123. break;
  124. }
  125. for (int i = 0; i < n; ++i) {
  126. if (!visited[i] && capacity[current][i] > flow[current][i]) {
  127. visited[i] = true;
  128. Q.add(i);
  129. parent[i] = current;
  130. }
  131. }
  132. }
  133. if (check == false)
  134. break;
  135.  
  136. long temp = capacity[parent[t]][t] - flow[parent[t]][t];
  137. for (int i = t; i != s; i = parent[i])
  138. temp = Math.min(temp, (capacity[parent[i]][i] - flow[parent[i]][i]));
  139.  
  140. for (int i = t; i != s; i = parent[i]) {
  141. flow[parent[i]][i] += temp;
  142. flow[i][parent[i]] -= temp;
  143. }
  144. }
  145.  
  146. long result = 0;
  147. for (int i = 0; i < n; ++i)
  148. result += flow[s][i];
  149. return result;
  150. }
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. static class CustomType{
  163. int gcd(int a, int b)
  164. {
  165. if(a == 0 || b == 0) return a+b; // base case
  166. return gcd(b,a%b);
  167. }
  168.  
  169. int mod (int a, int b){
  170. return a %b;
  171. }
  172.  
  173. int[] extendedEuclid(int a,int b) {
  174. int[] dxy = new int[3];
  175. if (b ==0){
  176. dxy[0] =a; dxy[1] =1; dxy[2] =0;
  177.  
  178. return dxy;
  179. }
  180. else{
  181. int t, t2;
  182. dxy = extendedEuclid(b, mod(a, b));
  183. t =dxy[1];
  184. t2 =dxy[2];
  185. dxy[1] =dxy[2];
  186. dxy[2] = t - a/b *t2;
  187.  
  188. return dxy;
  189. }
  190. }
  191.  
  192.  
  193.  
  194. int invMod(int num,int mod)
  195. {
  196. int ptr[] =extendedEuclid (num, mod);
  197. return ptr[1]+mod;
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204. }
  205.  
  206.  
  207. static class InputReader {
  208. public BufferedReader reader;
  209. public StringTokenizer tokenizer;
  210.  
  211. public InputReader(InputStream stream) {
  212. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  213. tokenizer = null;
  214. }
  215.  
  216. public String next() {
  217. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  218. try {
  219. tokenizer = new StringTokenizer(reader.readLine());
  220. } catch (IOException e) {
  221. throw new RuntimeException(e);
  222. }
  223. }
  224. return tokenizer.nextToken();
  225. }
  226.  
  227. public int nextInt() {
  228. return Integer.parseInt(next());
  229. }
  230.  
  231. public long nextLong() {
  232. return Long.parseLong(next());
  233. }
  234.  
  235. }
  236. }
Add Comment
Please, Sign In to add comment