From 614f9a295dfd9adc82d7177c915e31f684e5df93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9C=A0=ED=99=98?= Date: Thu, 25 Sep 2025 09:03:13 +0900 Subject: [PATCH] =?UTF-8?q?3665=EB=B2=88=20=ED=92=80=EC=9D=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Appendix E/solutions/3665.cpp | 70 +++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/Appendix E/solutions/3665.cpp b/Appendix E/solutions/3665.cpp index 6c991660..02170375 100644 --- a/Appendix E/solutions/3665.cpp +++ b/Appendix E/solutions/3665.cpp @@ -1,11 +1,73 @@ -// Authored by : BaaaaaaaaaaarkingDog +// Authored by : uhwan0723 // Co-authored by : - -// http://boj.kr/**************** +// http://boj.kr/a4497307382e4a2dad1cc8cf86eafcdc #include using namespace std; +const int mn = 505; +int t, n, m, indeg[mn], pre[mn]; +bool adj[mn][mn]; +queue q; + int main(void){ ios::sync_with_stdio(0); cin.tie(0); - -} \ No newline at end of file + cin >> t; + while(t--){ + cin >> n; + for(int i = 0; i < n; ++i) + cin >> pre[i]; + for(int i = 0; i < n; ++i){ + for(int j = i+1; j < n; ++j){ + adj[pre[i]][pre[j]] = 1; + ++indeg[pre[j]]; + } + } + cin >> m; + while(m--){ + int u, v; + cin >> u >> v; + adj[u][v] = !adj[u][v]; + adj[v][u] = !adj[v][u]; + if(adj[u][v]){ + ++indeg[v]; + --indeg[u]; + } + else{ + ++indeg[u]; + --indeg[v]; + } + } + + for(int i = 1; i <= n; ++i) + if(indeg[i] == 0) q.push(i); + + vector res; + while(!q.empty()){ + int cur = q.front(); q.pop(); + res.push_back(cur); + for(int nxt = 1; nxt <= n; ++nxt){ + if(adj[cur][nxt] == 0) continue; + if(--indeg[nxt] == 0) q.push(nxt); + } + } + + fill(indeg, indeg+n+1, 0); + for(int i = 1; i <= n; ++i) fill(adj[i], adj[i]+n+1, 0); + + if(res.size() != n){ + cout << "IMPOSSIBLE" << '\n'; + continue; + } + for(int u : res) + cout << u << ' '; + cout << '\n'; + } +} + +/* +?를 출력해야하는 상황이라는 것은 곧 두 팀간의 선후관계를 확정할 수 없다는 것인데, +그런 상황이 존재하려면 일단 어떤 두 팀 사이에 간선이 없어야합니다. +하지만 이 문제에서는, 작년 순위에서는 임의의 두 팀 사이에 관계가 존재하고, 상대 순위가 바뀐다고 하더라도 관계가 사라지는 것은 아닙니다. +따라서 ?를 출력하는 상황은 존재하지 않습니다. +*/