Skip to content
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

12-g0rnn #50

Merged
merged 2 commits into from
Jan 8, 2025
Merged

12-g0rnn #50

merged 2 commits into from
Jan 8, 2025

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Jan 2, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํšŒ์žฅ๋ฝ‘๊ธฐ
์˜๊ด‘์˜ 2025 ์ฒซ & 50๋ฒˆ์งธ pr์€ ์ œ๊ฐ€ ๊ฐ€์ ธ๊ฐ‘๋‹ˆ๋‹ค^^

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30m

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ๋ฅผ ๊ฐ„๋‹จํžˆ ์š”์•ฝํ•˜๋ฉด ๋™์•„๋ฆฌ์›์˜ ๊ด€๊ณ„๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜ํƒ€๋‚ด๊ณ  ๊ฐ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ˆœํšŒํ•˜๋ฉฐ ์–ผ๋งˆ๋‚˜ ๊นŠ์€ ์—ฐ๊ฒฐ์„ ๊ฐ€์ง€๋Š”์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

  1. ๋จผ์ € ๋™์•„๋ฆฌ์›์„ ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ €๋Š” 2์ฐจ์› ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

  2. bfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋…ธ๋“œ(๋ชจ๋“  ๋™์•„๋ฆฌ์›)๋ฅผ root๋กœ ์„ค์ •ํ•˜์—ฌ ์–ผ๋งˆ๋‚˜ ๊นŠ์€ ์—ฐ๊ฒฐ์„ ๊ฐ€์ง€๋Š”์ง€ ํ™•์ธํ•˜๊ณ  level์„ return ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„๋„ ์–ด๋ ต์ง€ ์•Š์„ ๊ฒ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์–ด๋ ต๋‹ค๋ฉด ์˜ˆ์ „ ํ† ๋งˆํ†  ๋ฌธ์ œ๋ฅผ ํ™•์ธํ•˜๊ธธ...

// ์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
for (int i = 1; i <= n; i++) {
    int score = bfs(i);
    ...
}
  1. ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ฒƒ๋“ค๋งŒ ์ถ”๋ฆฌ๊ณ  ์ •๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ bfs๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ธ์ง€ํ•˜๊ณ  ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€๋งŒ 2~3๋ฒˆ์œผ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๋‹จ๊ณ„๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ œ์ผ ๋‚œ๊ด€์ด๋ผ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค.

์ €๋Š” ์ฒ˜์Œ์— ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ bfs๋กœ ์ˆœํ™˜ํ•˜๊ณ  return ๊ฐ’์„ priority_queue(์ตœ์†Œ ์šฐ์„ ์ˆœ์œ„ ํ)์— ์‚ฝ์ž…ํ•˜์—ฌ ์ž๋™์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  front์™€ ๊ฐ™์€ point๋ฅผ ๊ฐ€์ง„ ์š”์†Œ๋“ค์„ ๋นผ๋‚ด์–ด ์ •๋‹ต์„ ์ถœ๋ ฅํ•˜๋ ค ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทผ๋ฐ ๋ฌด์Šจ ์ด์œ ์—์„ ์ง€ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋œจ๋”๊ตฐ์š”.. gpt๋ฅผ ํ†ตํ•ด ์•Œ์•„๋‚ธ ๊ฒฐ๊ณผ priority_queue์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ํž™์˜ ์„ฑ์งˆ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์š”์†Œ๋“ค์ด ์žฌ๋ฐฐ์น˜ ๋˜๊ณ  ๋‚ด๋ถ€์ ์œผ๋กœ size๊ฐ€ ๋Š˜์–ด๋‚  ๋•Œ๋งˆ๋‹ค 2๋ฐฐ๋กœ ๋Š˜์–ด๋‚˜๋‹ˆ ์ด ๋•Œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ดˆ๊ณผ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜๋”๊ตฐ์š”.

cpp์—์„œ ๋™์  ๋ฐฐ์—ด์ด๋ž€ ๋ฐฐ์—ด์˜ size๊ฐ€ ๋‚ด๊ฐ€ ๋„ฃ์€ ์›์†Œ๋งŒํผ ๋Š˜์–ด๋‚ฌ๋‹ค ์ค„์–ด๋“œ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์‹ค์ œ ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ์ •ํ•ด์ง„ size๊ฐ€ ์žˆ๊ณ  ์ด๋ฅผ ์ดˆ๊ณผํ•  ๋•Œ๋งˆ๋‹ค new๋ฅผ ํ†ตํ•ด ๋” ํฐ size์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ๊ธฐ์กด ๋ฐฐ์—ด์„ ์‚ฝ์ž…ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ ์ž ๊น์ด๋‚˜๋งˆ ๋ฐฐ์—ด์ด ๋‘๊ฐœ๋‚˜ ์ƒ๊ธฐ๊ฒŒ ๋˜๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ดˆ๊ณผ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜๋”๊ตฐ์š”. (๊ทผ๋ฐ ์•„๋ฌด๋ฆฌ ๊ทธ๋ž˜๋„ n์ด 50์ธ๋ฐ 128MB ๋ณด๋‹ค ์ปค์งˆ ์ˆ˜ ์žˆ์„๊นŒ์š”?)
image

๊ทธ๋ž˜์„œ ๋ฐฉ๋ฒ•์„ ๋ฐ”๊พธ์–ด bfs๋ฅผ ํ†ตํ•ด return ๋ฐ›์€ ๊ฐ’ ์ค‘ ์ž‘์€ ๊ฐ’์„ ๊ธฐ๋กํ•œ ํ›„ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์˜๋ฏธ์žˆ๋Š” ๊ฐ’๋“ค๋งŒ ์ถ”๋ ค๋‚ธ ํ›„ ์ •๋‹ต์„ ์ถœ๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ฃผ์„ ๋ถ€๋ถ„์€ ์ œ๊ฐ€ ์ฒ˜์Œ ์ƒ๊ฐํ•ด๋‚ธ ์ฝ”๋“œ์ธ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๊ฐ€ ์™„๋ฒฝํžˆ ๋‚ฉ๋“๋˜์ง€ ์•Š์•„ ์˜๊ฒฌ์„ ๋“ฃ๊ณ  ์‹ถ์–ด ๋‚จ๊ฒจ ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  auto [point, who]๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋Š” cpp์—์„œ๋„ ํŒŒ์ด์ฌ์ฒ˜๋Ÿผ ๋‘๊ฐ€์ง€ ๊ฐ’์„ ๋™์‹œ์— ๋ฐ›์„ ์ˆ˜ ์žˆ๋”๊ตฐ์š”. cpp17๋ถ€ํ„ฐ ์ƒ๊ฒผ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. std::tuple, std::pair๊ณผ ๊ฐ™์€ ๊ฒƒ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Œ€์š”!!

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ์•„์ด๋””์–ด๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†”๋ฃจ์…˜์ด ๋– ์˜ฌ๋ž์Šต๋‹ˆ๋‹ค.

1. ๋ชจ๋“  ์‚ฌ๋žŒ์„ ์‹œ์ž‘์ ์œผ๋กœํ•˜๋Š” bfs ์ง„ํ–‰.
2. 1๋ฒˆ ์ง„ํ–‰์ค‘ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ๋˜์—ˆ๋‹ค๋ฉด ๊ฐ€์žฅ ๋ฉ€๋ฆฌ์žˆ๋Š” ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ์ €์žฅ
3. ๊ฐ€์žฅ ๋ฉ€๋ฆฌ์žˆ๋Š” ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋“ค ์ค‘ ๊ฐ€์žฅ ์งง์€ ๊ฒƒ ์ฐพ๊ธฐ

์ฒ˜์Œ์—๋Š” ๊ท ํ˜ธ๋‹˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ 1์”ฉ ๊ฑฐ๋ฆฌ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ bfs๋ฅผ ํ–ˆ๋Š”๋ฐ ๊ณ„์† 4%์—์„œ ํ‹€๋ ธ๋‹ค๊ณ  ๋œจ๋”๊ตฐ์š”,,
๊ทธ๋ž˜์„œ gpt์„ ์ƒ๋‹˜์˜ ๋„์›€์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

return *max_element(distances.begin(), distances.end());

์ด๋ ‡๊ฒŒ distance๋ฐฐ์—ด์— ๊ฑฐ๋ฆฌ๋“ค์„ ์ €์žฅํ•˜๊ณ  ๊ฐ€์žฅ ํฐ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋„๋ก ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

์›๋ž˜ ์ฝ”๋“œ
int bfs(vector<list<int>>& graph, int member) {
    vector<bool> visited(N + 1, false);
    queue<int> q;
    q.push(member);
    visited[member] = true;

    int distance = 0;
    while (!q.empty()) {
        int size = q.size();
        for (int i = 0; i < size; i++) {
            int cur = q.front();
            q.pop();
            for (int curFriend : graph[cur]) {
                if (!visited[curFriend]) {
                    q.push(curFriend);
                    visited[curFriend] = true;
                }
            }
        }
        distance++;
    }

    if (isConnected(visited)) return distance;
    return -1;
}
(์ €๋„ ์™œ ๊ทธ๋ž˜์•ผํ–ˆ๋Š”์ง€ ์ •ํ™•ํ•œ ์ด์œ ๋Š”... ๋ชจ๋ฅด๊ฒ ๋”๋ผ๊ณ ์š”.. ๊ณ„์† ์•Œ์•„๋ณด๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.)

๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ๊ท ํ˜ธ๋‹˜๊ณผ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค.
์ตœ์ข… ๋ฐฐ์—ด์— ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ฐ’๋“ค๋งŒ ๋‚จ๊ธฐ๊ณ  ๊ทธ๊ฒƒ๋“ค์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

CPP CODE
#include <iostream>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int N;

void printResult(vector<pair<int, int>>& result) {
    cout << result[0].first << " " << result.size() << '\n';
    for (auto& p : result) cout << p.second << " ";
}

int bfs(vector<list<int>>& graph, int member) {
    vector<bool> visited(N + 1, false);
    vector<int> distances(N + 1, 0);
    queue<int> q;
    q.push(member);
    visited[member] = true;

    while (!q.empty()) {
        int cur = q.front();
        q.pop();

        for (int curFriend : graph[cur]) {
            if (!visited[curFriend]) {
                q.push(curFriend);
                visited[curFriend] = true;
                distances[curFriend] = distances[cur] + 1;
            }
        }
    }

    if (find(visited.begin() + 1, visited.end(), false) != visited.end()) return -1;
    return *max_element(distances.begin(), distances.end());
}

int main() {
    cin >> N;

    vector<list<int>> graph(N + 1);
    while (true) {
        int a, b;
        cin >> a >> b;
        if (a == -1 && b == -1) break;

        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    vector<pair<int, int>> result;
    int minDistance = INT_MAX;
    for (int i = 1; i <= N; i++) {
        int distance = bfs(graph, i);
        if (distance == -1) continue;
        if (distance < minDistance) {
            result.clear();
            minDistance = distance;
        }
        if (distance == minDistance) result.emplace_back(minDistance, i);
    }

    if (!result.empty()) printResult(result);

    return 0;
}

cpp ์“ฐ์‹œ๋Š” ๋ถ„์€ ์ฝ”๋“œ ํ•œ ๋ฒˆ ์ž˜ ์ฝ์–ด๋ณด์‹œ๋ฉด ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ง€๊ธˆ๊นŒ์ง€ ์ €๋Š” ์ž˜ ์•ˆ์จ์™”๋˜ find ์™€ max_element ๋“ฑ์„ ํ™œ์šฉํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!!
(๊ทธ๋ฆฌ๊ณ  emplace_back๋„ ์ฒ˜์Œ ํ™œ์šฉํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.)


๊ทธ๋ฆฌ๊ณ  ๊ท ํ˜ธ๋‹˜์ด ์ž‘์„ฑํ•˜์‹  priority_queue๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹๋„ ๋งŽ์ด ๊ณ ๋ฏผํ•ด๋ดค์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ.. ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ์ด์œ ๋Š” ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค...

์ €๋„ ๊ท ํ˜ธ๋‹˜ ์„ค๋ช…์ฒ˜๋Ÿผ ๋™์  ๋ฐฐ์—ด์ธ ๋ฒกํ„ฐ๊ฐ€ ๋Š˜์–ด๋‚˜๋„ 128MB๋ฅผ ์ดˆ๊ณผํ•  ์ •๋„์ธ์ง€ ์ž˜ ๊ฐ€๋Š ์ด ์•ˆ๋˜๋„ค์š”..
์กฐ๊ธˆ ๋” ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ pr์€ ์ƒ๊ฐํ•ด๋ณผ ๊ฑฐ๋ฆฌ๊ฐ€ ๋งŽ์€ ๊ฒƒ ๊ฐ™๋„ค์š”
์ข‹์€ ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ! !

Comment on lines +51 to +64
/*
for (int i = 1; i <= n; i++) {
int score = bfs(i);
candidate.emplace(score,i);
}

auto [point, who] = candidate.top();
candidate.pop();
president.push_back(who);

while (candidate.top().first == point) {
president.push_back(candidate.top().second);
candidate.pop();
}*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์™œ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š”์ง€ ์•„์ง ์ž˜ ๋ชจ๋ฅด๊ฒ ๋„ค์š”...
์ข€ ์˜ค๋ž˜ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ๋„ ๋‹ต์ด ์—†๋„ค์š”

๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ข‹์€ ๋กœ์ง์ธ ๊ฒƒ ๊ฐ™์•„์š”!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ˜น์‹œ ์ฒ˜์Œ ์ฝ”๋“œ์—์„œ๋„ mainํ•จ์ˆ˜๋Š” ๋ณ€ํ•จ์ด ์—†๋‚˜์š”? ์ €๋„ ์™œ ์•ˆ๋˜๋Š”์ง€ ์ด์œ ๋ฅผ ์ƒ๊ฐํ•ด๋ดค๋Š”๋ฐ ํ•จ์ˆ˜๋Š” ์ž˜์ž‘๋™ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ณ  ์ œ ์ฝ”๋“œ๋ž‘ ๊ฑฐ์˜ ๊ฐ™์€๊ฑฐ ๊ฐ™์•„์„œ main์—์„œ์˜ ์˜ค๋ฅ˜๊ฐ€ ์•„๋‹Œ๊ฐ€ ์ƒ๊ฐ๋˜๋„ค์š”

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„... ์ถœ๋ ฅ์„ ์ž˜๋ชปํ–ˆ๋„ค์š”...
์ œ์ผ ์ฒซ ์ถœ๋ ฅ์ด ํšŒ์žฅ ํ›„๋ณด ์ ์ˆ˜์ธ๋ฐ ์ €๋Š” ํšŒ์žฅ ํ›„๋ณด ์ค‘ ๊ฐ€์žฅ ์ฒซ ๋…€์„์„ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์—ˆ๋„ค์š”.
( ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ๋Š” ๋‘˜๋‹ค 2, 2 ๋ผ์„œ ๋ฐœ๊ฒฌ์ด ์•ˆ๋๋„ค์š”)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค ๊ทธ๋ฆฌ๊ณ  ๊ท ํ˜ธ๋‹˜ ์šฐ์„ ์ˆœ์œ„ ํ ๋ฒ„์ „์˜ ์†”๋ฃจ์…˜๋„ ์ด์œ ๋ฅผ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค!!

๋‹ค๋ฅธ ๊ณณ ํ•˜๋‚˜๋„ ์•ˆ๊ฑด๋“ค์˜€๊ณ , ๋ฌธ์ œ๋Š” ์—ฌ๊ธฐ์˜€์Šต๋‹ˆ๋‹ค.

while (!candidate.empty() && candidate.top().first == point) {
    president.push_back(candidate.top().second);
    candidate.pop();
}

candidate๊ฐ€ ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ปค๋ฒ„ํ•˜์ง€ ๋ชปํ•ด์„œ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์กŒ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋˜ ๊ฒƒ์ด๋„ค์š”

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

2
1 2
-1 -1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„๋‹ˆ ์ด๋Ÿฐ ์‹ค์ˆ˜๋ฅผ.. ํ•ด๋‹น ๋ถ€๋ถ„์€ ํ‹€๋ ธ์„๊ฑฐ๋ผ ์ƒ๊ฐ๋„ ์•ˆํ•œ ๋ถ€๋ถ„์ด๋ผ ํ˜ผ์ž์˜€๋‹ค๋ฉด ์ ˆ๋Œ€ ๋ชป์ฐพ์•˜์„ ๊ฒ๋‹ˆ๋‹คใ…œใ…œ ์ฐพ์•„์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ œ๋ฅผ ์ฝ๋‹ค๋ณด๋‹ˆ BFS๋ฌธ์ œ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

3๋ช…์ด๋ผ๋ฉด ์ฒซ์‚ฌ๋žŒ๋ถ€ํ„ฐ ๋Œ์•„๊ฐ€๋ฉฐ ๋‚˜๋จธ์ง€ ๋‘์‚ฌ๋žŒ๊ณผ์˜ ๊ด€๊ณ„์—์„œ ์–ผ๋งˆ๋‚˜๋–จ์–ด์ ธ์žˆ๋Š”์ง€ ์•Œ๊ธฐ์œ„ํ•ด BFS๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ฒ ๋‹ค ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ์นœ๊ตฌ์˜ ์นœ๊ตฌ๊ฐ€ ์•„๋‹Œ ์ง์ ‘์  ์นœ๊ตฌ๋ฉด BFS 1ํšŒ์— ์นœ๊ตฌ๋…ธ๋“œ๊ฐ€ ์žกํž๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ ๋ชจ๋‘๊ฐ€ ์ง์ ‘์ ์ธ ์นœ๊ตฌ๋ผ๋ฉด, ์ฒซ BFS์— ๋ชจ๋“  ๋…ธ๋“œ๋“ค์ด ๋‹ค ๋“ค์–ด์™€์•ผ๊ฒ ์ฃ .

์ด๋ฅผ ๊ฐ ๋…ธ๋“œ๋“ค๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋กœ ํŒ๋‹จํ•ด ์ „๋ถ€ 1์ด๋ผ๋ฉด ํ•ด๋‹น๋…ธ๋“œ๋Š” 1์ ์œผ๋กœ ๊ฐ€์žฅ ๋‚ฎ์€ ์ ์ˆ˜๋ฅผ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

from collections import deque

N = int(input())
friends = [[]for _ in range(N+1)]

while True:
    a, b = map(int, input().split())
    if a== -1 and b == -1:
        break
    friends[a].append(b)
    friends[b].append(a)

def bfs(start):
    dist = [-1] * (N + 1)
    dist[start] = 0
    queue = deque([start])
    
    while queue:
        current = queue.popleft()
        for neighbor in friends[current]:
            if dist[neighbor] == -1:
                dist[neighbor] = dist[current] + 1
                queue.append(neighbor)
    return dist
    
scores = []
for i in range(1, N + 1):
    dist = bfs(i)
    max_dist = max([d for d in dist[1:] if d != -1])
    scores.append((i, max_dist))

min_score = min(score for _, score in scores)
candidates = [member for member, score in scores if score == min_score]

print(min_score, len(candidates))
print(*sorted(candidates))

์ถ”๊ฐ€์‚ฌํ•ญ
ํŒŒ์ด์ฌ์—์„œ print(*sorted())๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ฆฌ์ŠคํŠธ์•ˆ์˜ ๋‚ด์šฉ์„ ๊ณต๋ฐฑ๊ธฐ์ค€์œผ๋กœ ํ’€์–ด์„œ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

max_dist = max([d for d in dist[1:] if d != -1])๋Š” ๋ฆฌ์ŠคํŠธ์ปดํ”„๋ฆฌํ—จ์…˜์ด๋ž€ ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค.
์ฆ‰ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ์ œ์™ธํ•˜๊ณ  1๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋‹ค๋งŒ if๋ฌธ์œผ๋กœ -1์ด ์•„๋‹Œ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋ฌธ์ œ์—์„œ ์–ด๋–ป๊ฒŒ๋“  ๋‹ค ์•„๋Š” ์‚ฌ์ด๋ผ ํ–ˆ์œผ๋‹ˆ ์‚ฌ์‹ค ํ•„์š”์—†๋Š” ๋ถ€๋ถ„์ด๊ธด ํ•ฉ๋‹ˆ๋‹ค,,

@kokeunho
Copy link
Collaborator

kokeunho commented Jan 7, 2025

ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ ํ’€์ด๊ฐ€ ์žˆ๋”๊ตฐ์š”.
๋‹ค๋“ค BFS๋กœ ํ’€์—ˆ๊ธฐ์— ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ์„ ๋Œ€์ถฉ ๊ณต๋ถ€ํ•˜๊ณ  ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.
ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‚ฌ์šฉ ๋ชฉ์ ์€ ๋ชจ๋“  ์ •์  ์Œ ๊ฐ„์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๊ทธ์— ๋ฐ˜ํ•ด BFS๋Š” ํŠน์ • ์ •์ ์—์„œ ๋‹ค๋ฅธ ์ •์ ๋“ค๊นŒ์ง€์˜ ๊ฒฝ๋กœ์ด๋ฏ€๋กœ ๋ชจ๋“  ์ •์ ์—์„œ BFS๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
ํ•ด๋‹น ๋ฌธ์ œ์—์„œ ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ์€ O(v^3) (v๋Š” ์ •์ ์˜ ๊ฐœ์ˆ˜), BFS๋Š” O(v * (v + E))์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
์ •์ ์˜ ์ˆ˜๊ฐ€ ์ ์„ ๋•Œ๋Š” ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ๋„ ๊ดœ์ฐฎ์œผ๋‚˜ ์‚ฌ์‹ค BFS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋Œ€๋ถ€๋ถ„์˜ ์ƒํ™ฉ์—์„œ ์ข‹๋‹ค๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

java code
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
        int[] scores = new int[n+1];
        int min = INF;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                scores[i] = Math.max(scores[i], dist[i][j]);
            }
            min = Math.min(min, scores[i]);
        }

์ค‘๊ฐ„ ๋…ธ๋“œ k๋ฅผ ์ถ”๊ฐ€ํ•ด๊ฐ€๋ฉฐ i์—์„œ j๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

scores[i]์—๋Š” dist[i]์—์„œ์˜ ์ตœ๋Œ€๊ฐ’์„ ๋„ฃ์Šต๋‹ˆ๋‹ค.
socres[] ์ค‘ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ’์ด ํ›„๋ณด๊ฐ€ ๋  ํšŒ์›๋“ค์˜ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค.

๋‹ค๋“ค ์ด๋ฏธ ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ์•Œ๊ณ ์žˆ๊ฒ ์ง€๋งŒ ๊ทธ๋ƒฅ ์ €๋ฅผ ์œ„ํ•ด ๋„์ ์—ฌ๋ดค์Šต๋‹ˆ๋‹ค. ๊ท ํ˜ธ๋‹˜ ์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants