結果

問題 No.416 旅行会社
ユーザー Tawara
提出日時 2016-08-27 11:00:43
言語 Python2
(2.7.18)
結果
AC  
実行時間 1,701 ms / 4,000 ms
コード長 875 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 118,536 KB
最終ジャッジ日時 2024-12-14 20:02:10
合計ジャッジ時間 16,679 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
def dfs(h,E,ans,reach,turn):
	q = [[-1,h]]
	while q:
		b,h = q.pop()
		if reach[h]:continue
		reach[h] = True
		ans[h] = turn
		for nxt in E[h]:
			if b == nxt:continue
			q.append([h,nxt])
def solve():
	ofs = 10**6
	N,M,Q = map(int,raw_input().split())
	reach = [False]*N
	EL = [[] for i in xrange(N)]
	ans = [0]*N
	Break = set()
	Edges = map(lambda x: map(lambda x:int(x)-1,x.split()),sys.stdin.readlines())
	for i in xrange(Q):
		Break.add(Edges[M+i][0]+Edges[M+i][1]*ofs)
		Break.add(Edges[M+i][0]*ofs+Edges[M+i][1])
	for i in xrange(M):
		f,t = Edges[i]
		if (f+t*ofs) in Break:
			continue
		EL[f].append(t); EL[t].append(f)
	dfs(0,EL,ans,reach,-1)
	for i in xrange(Q,0,-1):
		f,t = Edges[M+i-1]
		EL[f].append(t); EL[t].append(f)
		if reach[f]:
			dfs(t,EL,ans,reach,i)
		elif reach[t]:
			dfs(f,EL,ans,reach,i)
	for i in xrange(1,N):
		print ans[i]
solve()
0