Submission #3620305


Source Code Expand

import java.util.Scanner;

class Main{
	static boolean[] visited;
	static int N;
	static int M;
	static boolean[][] graph;
	static boolean[][] test;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		N=sc.nextInt();
		M=sc.nextInt();
		visited=new boolean[N+1];
		graph=new boolean[N+1][N+1];	//隣接行列
		test=new boolean[N+1][N+1];
		int[][] path=new int[M][2];

		for(int i=0; i<=N; i++) {		//グラフの初期化
			for(int j=0; j<=N; j++) {
				graph[i][j]=false;
			}
		}

		for(int i=0; i<M; i++) {		//入力
			int from=sc.nextInt();
			int to=sc.nextInt();
			graph[from][to]=true;
			graph[to][from]=true;
			path[i][0]=from;
			path[i][1]=to;
		}

		boolean connected=true;		//連結であるという過程で解く
		int count=0;		//橋の数

		visited[0]=true;	//ぬさもとりあへず
		for(int h=0; h<M; h++) {
			for(int i=0; i<=N; i++) {
				for(int j=0; j<=N; j++) {
					test[i][j]=graph[i][j];		//データをコピー
				}
			}
			connected=true;
			test[path[h][0]][path[h][1]]=false;		//パスを引っこ抜く
			test[path[h][1]][path[h][0]]=false;

			for(int i=1; i<=N; i++) {		//訪問初期化
				visited[i]=false;
			}
			dfs(1);	//1から始める(実際問題どこでもいい)

			for(int i=1; i<=N; i++) {		//連結判定
				if(!visited[i]) {
					connected=false;
					break;
				}
			}
			//System.out.println(connected);	//for debug
			if(connected==false) {
				count++;
			}
		}
		System.out.println(count);

	}
	static void dfs(int x) {		//訪問していくDFS
		if(visited[x]) {
			return;		//すでに訪問している時はみない
		}
		visited[x]=true;		//まだ訪れてないなら 今訪れたのでtrueにする
		for(int i=1; i<=N; i++) {
			if(test[x][i]) {		//訪問できるところを訪問する
				dfs(i);
			}
		}
	}

}

Submission Info

Submission Time
Task C - Bridge
User Digaus
Language Java8 (OpenJDK 1.8.0)
Score 300
Code Size 1907 Byte
Status AC
Exec Time 126 ms
Memory 23764 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 20
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
Case Name Status Exec Time Memory
sample_01.txt AC 99 ms 19796 KB
sample_02.txt AC 98 ms 21204 KB
sample_03.txt AC 96 ms 23764 KB
subtask_1_1.txt AC 113 ms 20948 KB
subtask_1_10.txt AC 98 ms 19796 KB
subtask_1_11.txt AC 111 ms 20692 KB
subtask_1_12.txt AC 119 ms 21204 KB
subtask_1_13.txt AC 121 ms 21332 KB
subtask_1_14.txt AC 122 ms 21844 KB
subtask_1_15.txt AC 126 ms 19156 KB
subtask_1_16.txt AC 111 ms 17236 KB
subtask_1_17.txt AC 120 ms 19924 KB
subtask_1_2.txt AC 122 ms 19924 KB
subtask_1_3.txt AC 104 ms 21844 KB
subtask_1_4.txt AC 109 ms 20052 KB
subtask_1_5.txt AC 115 ms 22996 KB
subtask_1_6.txt AC 118 ms 21460 KB
subtask_1_7.txt AC 109 ms 21844 KB
subtask_1_8.txt AC 106 ms 21332 KB
subtask_1_9.txt AC 101 ms 21332 KB