Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class E {
- final boolean ONLINE_JUDGE = true;
- BufferedReader in;
- PrintWriter out;
- StringTokenizer tok = new StringTokenizer("");
- class Graph {
- List<Integer>[] g;
- int n;
- boolean[] used;
- int[] component;
- int[] enter;
- int[] up;
- boolean[] isCutpoint;
- int time;
- int[] size;
- int[] res;
- public Graph(int n) {
- this.n = n;
- g = new List[n];
- for(int i = 0; i < n; i++) {
- g[i] = new ArrayList<Integer>();
- }
- }
- void add(int a, int b) {
- g[a].add(b);
- g[b].add(a);
- }
- int find() {
- used = new boolean[n];
- enter = new int[n];
- up = new int[n];
- isCutpoint = new boolean[n];
- component = new int[n];
- time = 0;
- dfsCutPoint(0, -1);
- Arrays.fill(used, false);
- time = -1;
- size = new int[n];
- res = new int[n];
- dfsComponent(0, -1, time);
- time++;
- int minIndex = -1;
- for(int i = 0; i < n; i++) {
- if(isCutpoint[i]) {
- if(minIndex == -1 || res[minIndex] > res[i]) {
- minIndex = i;
- }
- }
- }
- if(minIndex == -1) {
- minIndex = 0;
- }
- return minIndex;
- }
- void dfsCutPoint(int v, int p) {
- used[v] = true;
- enter[v] = up[v] = time++;
- int children = 0;
- for(int to: g[v]) {
- if(to == p) continue;
- if(used[to]) {
- up[v] = Math.min(up[v], enter[to]);
- } else {
- children++;
- dfsCutPoint(to, v);
- up[v] = Math.min(up[v], up[to]);
- if(up[to] >= enter[v]) {
- isCutpoint[v] = true;
- }
- }
- }
- if(v == 0) {
- isCutpoint[v] = children > 1;
- }
- }
- void dfsComponent(int v, int p, int color) {
- used[v] = true;
- size[v] = 1;
- res[v] = 0;
- int childrenSize = 0;
- for(int to: g[v]) {
- if(to == p) continue;
- if(!used[to]) {
- if(up[to] >= enter[v]) {
- int nextColor = ++time;
- dfsComponent(to, v, nextColor);
- if(res[v] < size[to]) {
- res[v] = size[to];
- }
- size[v] += size[to];
- childrenSize += size[to];
- } else {
- dfsComponent(to, v, color);
- size[v] += size[to];
- }
- }
- }
- if(res[v] < n - childrenSize - 1) {
- res[v] = n - childrenSize - 1;
- }
- }
- }
- void solve() throws IOException {
- int n = readInt();
- int m = readInt();
- Graph g = new Graph(n);
- for(int i = 0; i < m; i++) {
- int from = readInt() - 1;
- int to = readInt() - 1;
- g.add(from, to);
- }
- int res = g.find();
- out.println((res+1));
- }
- void init() throws FileNotFoundException {
- if (ONLINE_JUDGE) {
- in = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- } else {
- in = new BufferedReader(new FileReader("input.txt"));
- out = new PrintWriter("output.txt");
- }
- }
- String readString() throws IOException {
- while (!tok.hasMoreTokens()) {
- tok = new StringTokenizer(in.readLine());
- }
- return tok.nextToken();
- }
- int readInt() throws IOException {
- return Integer.parseInt(readString());
- }
- long readLong() throws IOException {
- return Long.parseLong(readString());
- }
- double readDouble() throws IOException {
- return Double.parseDouble(readString());
- }
- int[] readArr(int n) throws IOException {
- int[] res = new int[n];
- for (int i = 0; i < n; i++) {
- res[i] = readInt();
- }
- return res;
- }
- long[] readArrL(int n) throws IOException {
- long[] res = new long[n];
- for (int i = 0; i < n; i++) {
- res[i] = readLong();
- }
- return res;
- }
- public static void main(String[] args) {
- new E().run();
- }
- public void run() {
- try {
- long t1 = System.currentTimeMillis();
- init();
- solve();
- out.close();
- long t2 = System.currentTimeMillis();
- System.err.println("Time = " + (t2 - t1));
- } catch (Exception e) {
- e.printStackTrace(System.err);
- System.exit(-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement