Informatika gyűjtemény

Egy szinttel feljebb Varosok.java

2004050607080910

NézetNyomtat

Varosok.java (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 2 KB
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.krivan.szakkor.varos;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.HashSet;

/**
 *
 * @author Balint Krivan
 */
public class Varosok {

    private int n, m;

    // itt tároljuk, hogy az adott indexű város mely háromszögekben van benne.
    private int[] counties;
    private HashSet<Integer>[] neighbours;

    public Varosok() throws FileNotFoundException, IOException
    {
        BufferedReader in = new BufferedReader(new FileReader("varos.be"));
        PrintWriter out = new PrintWriter(new FileWriter("varos.ki"));
        String[] lines;
        String input;
        while( (input = in.readLine()) != null )
        {
            n = Integer.valueOf(input.split(" ")[0]);
            m = Integer.valueOf(input.split(" ")[1]);
            lines = new String[m];
            counties = new int[ n ];
            neighbours = new HashSet[ n ];
            for( int i=0; i<lines.length; i++ ) {
                lines[i] = in.readLine();
                int a = Integer.valueOf(lines[i].split(" ")[0])-1;
                int b = Integer.valueOf(lines[i].split(" ")[1])-1;
                int c = Integer.valueOf(lines[i].split(" ")[2])-1;
                if( neighbours[a] == null ) {
                    neighbours[a] = new HashSet<Integer>();
                }
                if( neighbours[b] == null ) {
                    neighbours[b] = new HashSet<Integer>();
                }
                if( neighbours[c] == null ) {
                    neighbours[c] = new HashSet<Integer>();
                }
                neighbours[a].add(b);
                neighbours[a].add(c);
                neighbours[b].add(a);
                neighbours[b].add(c);
                neighbours[c].add(a);
                neighbours[c].add(b);
                counties[a]++;
                counties[b]++;
                counties[c]++;
            }
        }
        int hatar = 0;
        StringBuffer sb = new StringBuffer();
        for( int i=0; i<n; i++ ) {
            if( neighbours[i].size() != counties[i] ) {
                hatar++;
            }
            if( i != 0 ) sb.append( " " );
            sb.append( neighbours[i].size() );

        }
        out.println( hatar );
        out.println( sb.toString() );
        out.close();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        new Varosok();
    }

}
(Vissza)