Advertisement
Guest User

E - Drogons in sleeping

a guest
Jun 23rd, 2015
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class E {
  5.  
  6.     final boolean ONLINE_JUDGE = true;
  7.     BufferedReader in;
  8.     PrintWriter out;
  9.     StringTokenizer tok = new StringTokenizer("");
  10.  
  11.     class Graph {
  12.         List<Integer>[] g;
  13.         int n;
  14.         boolean[] used;
  15.         int[] component;
  16.         int[] enter;
  17.         int[] up;
  18.         boolean[] isCutpoint;
  19.         int time;
  20.         int[] size;
  21.         int[] res;
  22.  
  23.         public Graph(int n) {
  24.             this.n = n;
  25.             g = new List[n];
  26.             for(int i = 0; i < n; i++) {
  27.                 g[i] = new ArrayList<Integer>();
  28.             }
  29.         }
  30.  
  31.         void add(int a, int b) {
  32.             g[a].add(b);
  33.             g[b].add(a);
  34.         }
  35.  
  36.  
  37.         int find() {
  38.             used = new boolean[n];
  39.             enter = new int[n];
  40.             up = new int[n];
  41.             isCutpoint = new boolean[n];
  42.             component = new int[n];
  43.             time = 0;
  44.             dfsCutPoint(0, -1);
  45.  
  46.  
  47.             Arrays.fill(used, false);
  48.             time = -1;
  49.             size = new int[n];
  50.             res = new int[n];
  51.             dfsComponent(0, -1, time);
  52.             time++;
  53.  
  54.             int minIndex = -1;
  55.             for(int i = 0; i < n; i++) {
  56.                 if(isCutpoint[i]) {
  57.                     if(minIndex == -1 || res[minIndex] > res[i]) {
  58.                         minIndex = i;
  59.                     }
  60.                 }
  61.             }
  62.             if(minIndex == -1) {
  63.                 minIndex = 0;
  64.             }
  65.             return minIndex;
  66.         }
  67.  
  68.         void dfsCutPoint(int v, int p) {
  69.             used[v] = true;
  70.             enter[v] = up[v] = time++;
  71.             int children = 0;
  72.             for(int to: g[v]) {
  73.                 if(to == p) continue;
  74.                 if(used[to]) {
  75.                     up[v] = Math.min(up[v], enter[to]);
  76.                 } else {
  77.                     children++;
  78.                     dfsCutPoint(to, v);
  79.                     up[v] = Math.min(up[v], up[to]);
  80.                     if(up[to] >= enter[v]) {
  81.                         isCutpoint[v] = true;
  82.                     }
  83.                 }
  84.             }
  85.             if(v == 0) {
  86.                 isCutpoint[v] = children > 1;
  87.             }
  88.         }
  89.  
  90.         void dfsComponent(int v, int p, int color) {
  91.             used[v] = true;
  92.             size[v] = 1;
  93.             res[v] = 0;
  94.             int childrenSize = 0;
  95.             for(int to: g[v]) {
  96.                 if(to == p) continue;
  97.                 if(!used[to]) {
  98.                     if(up[to] >= enter[v]) {
  99.                         int nextColor = ++time;
  100.                         dfsComponent(to, v, nextColor);
  101.                         if(res[v] < size[to]) {
  102.                             res[v] = size[to];
  103.                         }
  104.                         size[v] += size[to];
  105.                         childrenSize += size[to];
  106.                     } else {
  107.                         dfsComponent(to, v, color);
  108.                         size[v] += size[to];
  109.                     }
  110.                 }
  111.             }
  112.             if(res[v]  < n - childrenSize - 1) {
  113.                 res[v] = n - childrenSize - 1;
  114.             }
  115.         }
  116.  
  117.     }
  118.  
  119.     void solve() throws IOException {
  120.         int n = readInt();
  121.         int m = readInt();
  122.         Graph g = new Graph(n);
  123.         for(int i = 0; i < m; i++) {
  124.             int from = readInt() - 1;
  125.             int to = readInt() - 1;
  126.             g.add(from, to);
  127.         }
  128.         int res = g.find();
  129.         out.println((res+1));
  130.     }
  131.  
  132.  
  133.     void init() throws FileNotFoundException {
  134.         if (ONLINE_JUDGE) {
  135.             in = new BufferedReader(new InputStreamReader(System.in));
  136.             out = new PrintWriter(System.out);
  137.         } else {
  138.             in = new BufferedReader(new FileReader("input.txt"));
  139.             out = new PrintWriter("output.txt");
  140.         }
  141.     }
  142.  
  143.     String readString() throws IOException {
  144.         while (!tok.hasMoreTokens()) {
  145.             tok = new StringTokenizer(in.readLine());
  146.         }
  147.         return tok.nextToken();
  148.     }
  149.  
  150.     int readInt() throws IOException {
  151.         return Integer.parseInt(readString());
  152.     }
  153.  
  154.     long readLong() throws IOException {
  155.         return Long.parseLong(readString());
  156.     }
  157.  
  158.     double readDouble() throws IOException {
  159.         return Double.parseDouble(readString());
  160.     }
  161.  
  162.     int[] readArr(int n) throws IOException {
  163.         int[] res = new int[n];
  164.         for (int i = 0; i < n; i++) {
  165.             res[i] = readInt();
  166.         }
  167.         return res;
  168.     }
  169.  
  170.     long[] readArrL(int n) throws IOException {
  171.         long[] res = new long[n];
  172.         for (int i = 0; i < n; i++) {
  173.             res[i] = readLong();
  174.         }
  175.         return res;
  176.     }
  177.  
  178.     public static void main(String[] args) {
  179.         new E().run();
  180.     }
  181.  
  182.     public void run() {
  183.         try {
  184.             long t1 = System.currentTimeMillis();
  185.             init();
  186.             solve();
  187.             out.close();
  188.             long t2 = System.currentTimeMillis();
  189.             System.err.println("Time = " + (t2 - t1));
  190.         } catch (Exception e) {
  191.             e.printStackTrace(System.err);
  192.             System.exit(-1);
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement