-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[이지민] - 구간 합 구하기 5, 작업, 도넛과 막대 그래프 #217
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import sys | ||
|
||
n, m = map(int, sys.stdin.readline().split()) | ||
numbers = [] | ||
sum_info = [[0 for _ in range(n + 1)]] | ||
|
||
for i in range(n): | ||
numbers.append(list(map(int, sys.stdin.readline().split()))) | ||
|
||
for i in range(1, n + 1): | ||
sub_info = [0] | ||
tmp = [0] | ||
for j in range(1, n + 1): | ||
tmp.append(tmp[-1] + numbers[i - 1][j - 1]) | ||
sub_info.append(sum_info[i - 1][j] + tmp[-1]) | ||
Comment on lines
+10
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+10
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요기가 진짜 대단하네요,,, |
||
|
||
sum_info.append(sub_info) | ||
|
||
for i in range(m): | ||
x1, y1, x2, y2 = map(int, sys.stdin.readline().split()) | ||
|
||
print(sum_info[x2][y2] - sum_info[x1 - 1][y2] - sum_info[x2][y1 - 1] + sum_info[x1 - 1][y1 - 1]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import itertools | ||
from collections import deque | ||
|
||
|
||
def solution(edges): | ||
edge_size = max(list(itertools.chain(*edges))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. edges의 타입이 정의되어있지 않아서 요런식으로 크기를 구해야하는 건가요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사실 문제에 순서대로 번호를 붙인다고 안쓰여져있어서 정석인 방식은 아닌 것 같아요..!! |
||
edge_info = [[] for _ in range(edge_size + 1)] | ||
edge_get = [0 for _ in range(edge_size + 1)] | ||
for a, b in edges: | ||
edge_info[a].append(b) | ||
edge_get[b] += 1 | ||
|
||
candidates = [[i, len(edge_info[i])] for i in range(1, edge_size + 1)] | ||
candidates.sort(key=lambda x: x[1], reverse=True) | ||
visited = [False for _ in range(edge_size + 1)] | ||
visited[0] = True | ||
point = -1 | ||
|
||
result = [0, 0, 0, 0] | ||
|
||
for candidate, _ in candidates: | ||
if edge_get[candidate] == 0: | ||
point = candidate | ||
break | ||
|
||
result[0] = point | ||
|
||
for edge in edge_info[point]: | ||
edge_get[edge] -= 1 | ||
|
||
for i in range(1, edge_size + 1): | ||
if i == point or visited[i] == True: | ||
continue | ||
|
||
type = check_graph(i, edge_info, visited, point) | ||
if type != 4: | ||
result[type] += 1 | ||
|
||
return result | ||
|
||
|
||
def check_graph(first, edge_info, visited, point): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 색다르게 푸셔서 좋았어요! 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 복잡한 구현을 깔끔하게 하신 거 같아요! |
||
type = -1 | ||
|
||
queue = deque([]) | ||
visited[first] = True | ||
if (len(edge_info[first]) == 2): | ||
type = 3 | ||
|
||
for e in edge_info[first]: | ||
if visited[e] == False: | ||
queue.append(e) | ||
else: | ||
if e == first: | ||
type = 1 | ||
else: | ||
type = 4 | ||
Comment on lines
+54
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 분기 처리 깔끔하다고 생각했습니다!!!👍 |
||
|
||
while queue: | ||
now = queue.popleft() | ||
visited[now] = True | ||
|
||
if (len(edge_info[now]) == 2): | ||
type = 3 | ||
|
||
if type != 3 and now == first: | ||
type = 1 | ||
|
||
for e in edge_info[now]: | ||
if visited[e] == False: | ||
queue.append(e) | ||
else: | ||
if type != 3: | ||
if e == first: | ||
type = 1 | ||
else: | ||
type = 4 | ||
|
||
if type == -1: | ||
type = 2 | ||
|
||
return type | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import sys | ||
from collections import deque | ||
|
||
n, m = map(int, sys.stdin.readline().split()) | ||
jobs = [[] for _ in range(n + 1)] | ||
|
||
for i in range(m): | ||
a, b = map(int, sys.stdin.readline().split()) | ||
jobs[b].append(a) | ||
|
||
x = int(sys.stdin.readline()) | ||
|
||
queue = deque(jobs[x]) | ||
visited = [False for _ in range(n + 1)] | ||
visited[x] = True | ||
|
||
num = 0 | ||
while queue: | ||
nxt = queue.popleft() | ||
if not visited[nxt]: | ||
num += 1 | ||
visited[nxt] = True | ||
|
||
for t in jobs[nxt]: | ||
if not visited[t]: | ||
queue.append(t) | ||
Comment on lines
+18
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bfs 보기가 깔끔합니다,,! |
||
|
||
print(num) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import sys | ||
from collections import deque | ||
|
||
|
||
def check_students(first, students, visited): | ||
queue = deque(students[first]) | ||
visited[first] = 1 | ||
|
||
while queue: | ||
now = queue.popleft() | ||
visited[now] = 1 | ||
|
||
for student in students[now]: | ||
queue.append(student) | ||
|
||
|
||
n, m = map(int, sys.stdin.readline().split()) | ||
students = [[] for _ in range(n + 1)] | ||
reverse_students = [[] for _ in range(n + 1)] | ||
result = 0 | ||
|
||
for _ in range(m): | ||
a, b = map(int, sys.stdin.readline().split()) | ||
students[a].append(b) | ||
reverse_students[b].append(a) | ||
|
||
for i in range(1, n + 1): | ||
visited = [0 for _ in range(n + 1)] | ||
visited[0] = 1 | ||
check_students(i, students, visited) | ||
check_students(i, reverse_students, visited) | ||
|
||
if all(visited): | ||
result += 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 부등호에 현혹돼서 너무 어렵게 생각했나봐요 bfs 좋네용 |
||
|
||
print(result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
애초부터 구간합도 이렇게 반복 한번으로 저장해둘 수 있었군요