Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Solution implements Runnable {
  5.  
  6.  
  7.     private StringTokenizer st;
  8.     private BufferedReader in;
  9.     private PrintWriter out;
  10.    
  11.     final String file = "C-large";
  12.  
  13.     public void solve(int test) throws IOException {
  14.         out.print("Case #" + (test + 1) + ": ");
  15.         int n = nextInt();
  16.         int m = nextInt();
  17.         ArrayList<Integer>[] edges = new ArrayList[n];
  18.         for (int i = 0; i < n; ++i) {
  19.             edges[i] = new ArrayList<Integer>();
  20.         }
  21.         for (int i = 0; i < n; ++i) {
  22.             edges[i].add((i + 1) % n);
  23.             edges[(i + 1) % n].add(i);
  24.         }
  25.         int[] a = new int[m];
  26.         int[] b = new int[m];
  27.         for (int i = 0; i < m; ++i) {
  28.             a[i] = nextInt() - 1;
  29.         }
  30.         for (int i = 0; i < m; ++i) {
  31.             b[i] = nextInt() - 1;
  32.         }
  33.         for (int i = 0; i < m; ++i) {
  34.             edges[a[i]].add(b[i]);
  35.             edges[b[i]].add(a[i]);
  36.         }
  37.         int[][] col = new int[n][];
  38.         for (int i = 0; i < n; ++i) {
  39.             Collections.sort(edges[i]);
  40.             col[i] = new int[edges[i].size()];
  41.             Arrays.fill(col[i], -1);
  42.         }
  43.         ArrayList<ArrayList<Integer>> comps = new ArrayList<ArrayList<Integer>>();
  44.         for (int i = 0; i < n; ++i) {
  45.             for (int j = 0; j < edges[i].size(); ++j) {
  46.                 if (col[i][j] != -1) {
  47.                     continue;
  48.                 }
  49.                 ArrayList<Integer> c = new ArrayList<Integer>();
  50.                 int u = i, e = j;
  51.                 while (col[u][e] == -1) {
  52.                     c.add(u);
  53.                     col[u][e] = comps.size();
  54.                     int nu = edges[u].get(e);
  55.                     int ne = (Collections.binarySearch(edges[nu], u) + 1) % edges[nu].size();
  56.                     u = nu;
  57.                     e = ne;
  58.                 }
  59.                 comps.add(c);
  60.             }
  61.         }
  62.         int minComp = 1;
  63.         for (int i = 2; i < comps.size(); ++i) {
  64.             if (comps.get(i).size() < comps.get(minComp).size()) {
  65.                 minComp = i;
  66.             }
  67.         }
  68.         int ans = comps.get(minComp).size();
  69.         int[] ansc = new int[n];
  70.         Arrays.fill(ansc, -1);
  71.         dfs(minComp, comps, ansc, ans, col, edges);
  72.         System.err.println(ans);
  73.         System.err.println(Arrays.toString(ansc));
  74.         for (int i = 1; i < comps.size(); ++i) {
  75.             boolean[] used = new boolean[ans];
  76.             for (int j : comps.get(i)) {
  77.                 used[ansc[j]] = true;
  78.             }
  79.             for (boolean f : used) {
  80.                 if (!f) {
  81.                     throw new AssertionError();
  82.                 }
  83.             }
  84.         }
  85.         out.println(ans);
  86.         for (int i = 0; i < n; ++i) {
  87.             out.print((ansc[i] + 1) + " ");
  88.         }
  89.         out.println();
  90.     }
  91.  
  92.     private void dfs(int c, ArrayList<ArrayList<Integer>> comps,
  93.             int[] ansc, int ans, int[][] col, ArrayList<Integer>[] edges) {
  94.         if (c == 0) {
  95.             return;
  96.         }
  97.         ArrayList<Integer> comp = comps.get(c);
  98.         boolean[] used = new boolean[ans];
  99.         int from = -1;
  100.         int count = 0;
  101.         for (int i = 0; i < comp.size(); ++i) {
  102.             if (ansc[comp.get(i)] != -1) {
  103.                 used[ansc[comp.get(i)]] = true;
  104.                 count++;
  105.                 if (ansc[comp.get((i + 1) % comp.size())] != -1) {
  106.                     if (from != -1) {
  107.                         throw new AssertionError();
  108.                     }
  109.                     from = i;
  110.                 }
  111.             }
  112.         }
  113.         for (int i = 0; i < comp.size(); ++i) {
  114.             if (ansc[comp.get(i)] != -1) {
  115.                 continue;
  116.             }
  117.             ansc[comp.get(i)] = 0;
  118.             while (ansc[comp.get(i)] < ans && used[ansc[comp.get(i)]]) {
  119.                 ++ansc[comp.get(i)];
  120.             }
  121.             if (ansc[comp.get(i)] == ans) {
  122.                 ansc[comp.get(i)] = 0;
  123.             }
  124.             while (ansc[comp.get(i)] == ansc[comp.get((i + comp.size() - 1) % comp.size())] ||
  125.                    ansc[comp.get(i)] == ansc[comp.get((i + 1) % comp.size())]) {
  126.                 ansc[comp.get(i)]  = (ansc[comp.get(i)] + 1) % ans;
  127.             }
  128.             used[ansc[comp.get(i)]] = true;
  129.         }
  130.         for (int i = 0; i < comp.size(); ++i) {
  131.             if (i == from) {
  132.                 continue;
  133.             }
  134.             int u = comp.get(i);
  135.             int v = comp.get((i + 1) % comp.size());
  136.             dfs(col[v][Collections.binarySearch(edges[v], u)], comps, ansc, ans, col, edges);
  137.         }
  138.     }
  139.  
  140.     public void run() {
  141.         try {
  142.             in = new BufferedReader(new FileReader(file + ".in"));
  143.             out = new PrintWriter(file + ".out");
  144.             eat("");
  145.            
  146.             int tests = nextInt();
  147.             for (int test = 0; test < tests; ++test) {
  148.                 solve(test);
  149.             }
  150.            
  151.             out.close();
  152.         } catch (Exception e) {
  153.             e.printStackTrace();
  154.             failed = true;
  155.         }
  156.     }
  157.    
  158.     void eat(String s) {
  159.         st = new StringTokenizer(s);
  160.     }
  161.    
  162.     String next() throws IOException {
  163.         while (!st.hasMoreTokens()) {
  164.             String line = in.readLine();
  165.             if (line == null) {
  166.                 return null;
  167.             }
  168.             eat(line);
  169.         }
  170.         return st.nextToken();
  171.     }
  172.    
  173.     int nextInt() throws IOException {
  174.         return Integer.parseInt(next());
  175.     }
  176.    
  177.     long nextLong() throws IOException {
  178.         return Long.parseLong(next());
  179.     }
  180.    
  181.     double nextDouble() throws IOException {
  182.         return Double.parseDouble(next());
  183.     }
  184.    
  185.     static boolean failed = false;
  186.    
  187.     public static void main(String[] args) {
  188.         Locale.setDefault(Locale.US);
  189.         Thread th = new Thread(new Solution());
  190.         th.start();
  191.         try {
  192.             th.join();
  193.         } catch (InterruptedException iex) {
  194.         }
  195.         if (failed) {
  196.             throw new RuntimeException();
  197.         }
  198.     }
  199.    
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement