Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- //матрица смежности
- class AdjacencyMatrix {
- private int Matrix[][];
- private int n;
- private int m;
- public AdjacencyMatrix(int n,int m)
- {
- Matrix=new int[n][m];
- this.n=n;
- this.m=m;
- }
- //считать матрицу с консоли
- public void consoleRead()
- {
- Scanner sc=new Scanner(System.in);
- for (int i=0;i<n;i++)
- for (int j=0;j<m;j++)
- Matrix[i][j]=sc.nextInt();
- }
- //вернуть элемент матрицы
- public int get(int i,int j)
- {
- return Matrix[i][j];
- }
- }
- //задание 3
- public class Ex_1_3 {
- public static void main(String args[])
- {
- int n,counter=0; // кол-во мостов, кол-во плохих мостов
- int [] colors; //цвета мостов
- Scanner sc=new Scanner(System.in);
- n=sc.nextInt();
- //иницилизировать и считать матрицу смежности
- AdjacencyMatrix matrix=new AdjacencyMatrix(n,n);
- matrix.consoleRead();
- //иницилизировать и считать цвета мостов
- colors=new int[n];
- for (int i=0;i<n;i++)
- {
- colors[i]=sc.nextInt();
- }
- //найти мосты , если найден проверить совпадают ли цвета островов
- for (int i=0;i<n;i++)
- {
- for (int j=0;j<i;j++)
- {
- if (matrix.get(i,j)==1)
- if (colors[i]!=colors[j])
- counter++;
- }
- }
- System.out.println(counter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement