01204212/friends

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
This part of 01204212

In this task you will work with graphs from real social networks.

  • facebook.in -- This is a graph from facebook. It is an undirected graph. The graph is from [1]
  • twitter.in -- This is a graph from twitter. It is a directed graph. This graph is from [2]

The graph format:

  • First line: two integers n and m, where n is the number of vertices and m is the number of edges
  • Next m lines: each line contains two integers u and v (ranging from 0 to n-1). In an undirected graph, this means that there is an edge between u and v. In a directed graph, this means that there is an edge from u to v.

Tasks

Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        
        main.process();
    }
    
    int n,m;
    List<Integer> [] adjList;
    
    void process() {
        readInput();
    }

    private void readInput() {
        BufferedReader reader = new BufferedReader(
                   new InputStreamReader(System.in) );

        try {
            String[] items = reader.readLine().split(" ");

            n = Integer.parseInt(items[0]);
            m = Integer.parseInt(items[1]);

            adjList = (List<Integer>[])(new List[n]);
            for(int i=0; i<n; i++) {
                adjList[i] = new ArrayList<Integer>();
            }
            
            for(int i=0; i<m; i++) {
                items = reader.readLine().split(" ");
                int u = Integer.parseInt(items[0]);
                int v = Integer.parseInt(items[1]);
                
                adjList[u].add(v);
                adjList[v].add(u);
            }
        } catch(Exception e) {
        }
    }
}