-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogle_2013_practice_bad_horse.py
152 lines (130 loc) · 4.14 KB
/
Google_2013_practice_bad_horse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding:utf-8 -*-
""" 方法一:查找所有可能的组合方式 """
# # 文件版本
# in_path = "data/A-small-practice-1.in"
# out_path = "result/A-small-practice-1_first.out"
#
# f_in = open(in_path, "r")
# f_out = open(out_path, "w")
#
# T = int(f_in.readline().strip())
#
# # 读取样例
# for t in range(T):
# M = int(f_in.readline().strip())
# pre_result = [([], [])]
# flag = "Yes"
#
# # 读取内容
# for m in range(M):
# # 读取新的一行
# line = f_in.readline().strip().split()
# update_result = []
#
# # 是否不再需要继续深入分析
# if flag == "No":
# continue
#
# # 更新组合
# for result in pre_result:
# # 如果同时出现在一个集合,跳过
# if (line[0] in result[0] and line[1] in result[0]) or (line[0] in result[1] and line[1] in result[1]):
# continue
#
# # 如果出现在两边,保留
# elif (line[0] in result[0] and line[1] in result[1]) or (line[1] in result[0] and line[0] in result[1]):
# update_result.append((result[0], result[1]))
#
# # 如果只有一个出现过
# elif line[0] in result[0]:
# result[1].append(line[1])
# update_result.append((result[0], result[1]))
# elif line[0] in result[1]:
# result[0].append(line[1])
# update_result.append((result[0], result[1]))
# elif line[1] in result[0]:
# result[1].append(line[0])
# update_result.append((result[0], result[1]))
# elif line[1] in result[1]:
# result[0].append(line[0])
# update_result.append((result[0], result[1]))
#
# # 如果都没有出现
# else:
# update_result.append((result[0] + [line[0]], result[1] + [line[1]]))
# update_result.append((result[0] + [line[1]], result[1] + [line[0]]))
#
# if len(update_result) == 0:
# flag = "No"
# else:
# # 更新
# pre_result = update_result
#
# # 输出结果
# f_out.write("Case #%d: %s"%(t+1, flag))
# f_out.write("\n")
"""
只要是连通的,有状态便可以接上去
"""
# 文件版本
in_path = "data/A-small-practice-2.in"
out_path = "result/A-small-practice-2.out"
f_in = open(in_path, "r")
f_out = open(out_path, "w")
T = int(f_in.readline().strip())
# 读取样例
for t in range(T):
M = int(f_in.readline().strip())
d = {}
visited = {}
YES = True
# 读取内容
for m in range(M):
line = f_in.readline().strip().split()
d.setdefault(line[0], []).append(line[1])
d.setdefault(line[1], []).append(line[0])
visited[line[0]] = -1
visited[line[1]] = -1
if t == 0:
print(d)
print(visited)
# 遍历
for key in d.keys():
# 是否访问过
if visited[key] == -1:
visited[key] = 0
# else:
# continue
# 广度搜索
result = [key]
while len(result) > 0:
if t == 0:
print("-----------------")
print(result)
print(visited)
print("-----------------")
# 选取初始结点,寻找子节点
init_key = result.pop(0)
# 判断子节点是否满足条件,不冲突
for next_key in d[init_key]:
# 如果没有遍历过
if visited[next_key] == -1:
visited[next_key] = int(1 - visited[init_key])
result.append(next_key)
# 如果已经设置过且出现冲突
elif visited[next_key] == visited[init_key]:
YES = False
break
else:
continue
if not YES:
break
if not YES:
break
# print(YES)
# print(t)
if YES:
f_out.write("Case #%d: Yes" % (t + 1))
else:
f_out.write("Case #%d: No" % (t + 1))
f_out.write("\n")