Merged
Conversation
Contributor
|
잘 좀 하자..... 너 가서 알고리즘 만들어야 하는데 이렇게 할꺼야??... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/33988
🧭 풀이 시간
180분
👀 체감 난이도
✏️ 문제 설명
정점 N개 간선 M개인 무향 연결 그래프 G가 주어진다.
집합 S는 1 <= i < j <= N, 0 <= w <= 10^9를 만족하는 모든 간선 쌍 (i,j,w)을 원소로 가진다.
S에서 하나를 뽑아서 G에 추가한 새 그래프 G'의 가중치 합의 기댓값을 구해보자.
🔍 풀이 방법
사건 i가 일어날 확률 P[i], 그 떄의 값이 E[i]일 때,
기댓값은 sum(P[i] * E[i])
X = G의 MST 가중치
어떤 정점 쌍 (u,v)에 대해,
d[u][v] = MST에서 u와 v를 잇는 경로 상의 최대 가중치로 정의
모든 정점 쌍 (u,v)에 대해,
S 내에서 둘을 잇는 간선의 개수는 10^9 + 1개
이 중에서 MST를
X-d[u][v]로 만드는 경우 1개
X-d[u][v]+1로 만드는 경우 1개
...
X-1로 만드는 경우 1개
X로 만드는 경우 10^9 + 1 - d[u][v]개
전체 사건의 개수는 N*(N-1)/2개
-> O(N^2)임. 시간 초과
전체 경우의 수 P = N*(N-1)/2 * (10^9 + 1)
하지만 d[u][v]의 총 가짓수는 N-1개임.
MST에 쓰인 간선들 중, 가중치 최소 간선부터 분리 집합으로 합쳐주며 컴포넌트의 개수를 관리
현재 추가한 간선 (u,v,w)에 대해,
f(a) = MST를 X-a로 만드는 경우의 수 라고 하면,
f(1)부터 f(w)까지 c[u] * c[v]만큼이 추가되고, f(0)에는 (10^9 + 1 - w)*c[u]*c[v]만큼 추가됨.
답은 sum(f(a)*(X-a)/P)
근데 이거도 가중치 범위가 10^9라 시간 초과임
어케 더 줄이지?
위에서 각 작업을 튜플 (w, c[u]*c[v])로 배열 E에 관리하자.
w에대한 내림차순으로 정렬하면,
E[i]와 E[i+1] 사이에 대한 구간을 효율적으로 처리할 방법을 생각해보자
경우의 수에 대한 누적 합 C를 관리.
E[i+1].w + 1 <= e <= E[i].w 인 e에 대해,
cnt = E[i].w - E[i+1].w라고 하자.
g(n) = n*(n+1)/2라고 하면, s = g(E[i].w) - g(E[i+1].w)라고 두자.
((X-E[i].w * (C + E[i].c)) + (X-E[i].w-1 * (C + 2E[i].c)) + ... ) / P
이걸 구한 뒤, 마지막에 f(0)만 따로 처리.
⏳ 회고
맞는 거 같은데 왜 답이 안나오지?