zoukankan      html  css  js  c++  java
  • Java集合之HashMap

    HashMap

    • 定义
    C++:map
    Java:HashMap
    
    • 创建与其基本操作
    创建:
    HashMap<Integer,Integer>=new HashMap<Integer,Integer>();
    基本操作:
    void clear( )
    Object clone( )
    boolean contains(Object value)
    boolean containsKey(Object key)
    boolean containsValue(Object value)
    Object get(Object key)
    Object put(Object key, Object value)
    

    例题:D. Andrew and Chemistry

    链接:http://codeforces.com/contest/718/problem/D

    Description


    During the chemistry lesson Andrew learned that the saturated hydrocarbons (alkanes) enter into radical chlorination reaction. Andrew is a very curious boy, so he wondered how many different products of the reaction may be forms for a given alkane. He managed to solve the task for small molecules, but for large ones he faced some difficulties and asks you to help.
    Formally, you are given a tree consisting of n vertices, such that the degree of each vertex doesn't exceed 4. You have to count the number of distinct non-isomorphic trees that can be obtained by adding to this tree one new vertex and one new edge, such that the graph is still the tree and the degree of each vertex doesn't exceed 4.
    Two trees are isomorphic if there exists a bijection f(v) such that vertices u and v are connected by an edge if and only if vertices f(v) and f(u) are connected by an edge.

    Input


    The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of vertices in the tree.
    Then follow n - 1 lines with edges descriptions. Each edge is given by two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices connected by an edge. It's guaranteed that the given graph is a tree and the degree of each vertex doesn't exceed 4.

    Output


    Print one integer — the answer to the question.
    Examples

    input

    4
    1 2
    2 3
    2 4
    

    output

    2
    

    主要在于将树哈希,对于每个分支不超过4的节点,用最小表示法进行哈希,其中需要用到HashMap

    描述

    import java.io.*;
    import java.util.*;
    public class Main {
    	static final int N=100005;
    	static int a[]=new int[N];
    	static ArrayList<Integer>G[]=new ArrayList[N];
    	static HashMap<ArrayList<Integer>,Integer>vis=new HashMap<ArrayList<Integer>,Integer>();
    	static HashMap<String,Integer>book=new HashMap<String,Integer>();
    	static int sz;
    	static class cmp implements Comparator<Object>{
    		@Override 
    		public int compare(Object o1,Object o2) {
    			Integer p1=(Integer)o1;
    			Integer p2=(Integer)o2;
    			return p1>p2?1:-1;
    		}
    	}
    	static int dfs(int u,int fa) {
    		String st=u+"#"+fa;
    		if(book.containsKey(st)) {
    			return book.get(st);
    		}
    		ArrayList<Integer>temp=new ArrayList<Integer>();
    		for(int i=0;i<G[u].size();i++) if(G[u].get(i)!=fa) {
    			temp.add(dfs(G[u].get(i),u));
    		}
    		temp.sort(new cmp());
    		if(!vis.containsKey(temp)) {
    			vis.put(temp, ++sz);
    		}
    		book.put(st,vis.get(temp));
    		return vis.get(temp);
    	}
    	public static void main(String[] args){
    		InputReader in = new InputReader(System.in);
    	    PrintWriter out = new PrintWriter(System.out);
    	    int n=in.nextInt();sz=0;
    	    for(int i=0;i<n-1;i++) {
    	    	int u=in.nextInt(),v=in.nextInt();
    	    	if(G[u]==null) G[u]=new ArrayList<Integer>();
    	    	if(G[v]==null) G[v]=new ArrayList<Integer>();
    	    	G[u].add(v);G[v].add(u);
    	    }
    	    int ans=0;
    	    HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();
    	    for(int i=1;i<=n;i++) {
    	    	if(G[i]==null) continue;
    	    	if(G[i].size()==4) continue;
    	    	int id=dfs(i,-1);
    	    	if(!map.containsKey(id)) {
    	    		ans++;
    	    		map.put(id, 1);
    	    	}
    	    }
    	    if(n==1) ans=1;
    	    out.println(ans);
    	    out.flush();
    	}
    	static class InputReader {
            public BufferedReader reader;
            public StringTokenizer tokenizer;
            public InputReader(InputStream stream) {
                reader = new BufferedReader(new InputStreamReader(stream), 32768);
                tokenizer = null;
            }
            public String next() {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    try {
                        tokenizer = new StringTokenizer(reader.readLine());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                return tokenizer.nextToken();
            }
            public int nextInt() {
                return Integer.parseInt(next());
            }
        }
    }
    
  • 相关阅读:
    My Tornado Particle Effect
    [zz] 海洋环境的光能传递
    一道算法题
    Alembic
    一些莫名其妙的东东
    Python Q&A
    <<Exceptional C++>> notes
    CG Rendering v.s. Browser Rendering
    Modo
    Katana
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/7657691.html
Copyright © 2011-2022 走看看