-
Notifications
You must be signed in to change notification settings - Fork 1
33-g0rnn #134
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
Conversation
kokeunho
left a comment
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.
νΈλ¦¬μ μ§λ¦μ μ°Ύλ λ²μ λ°°μΈ μ μμμ΅λλ€.
μμμ μ μ μμ κ°μ₯ λ¨Ό μ§μ μ μ°Ύμ ν,
κ·Έ μ§μ μμ κ°μ₯ λ¨Ό μ§μ κΉμ§μ κ±°λ¦¬κ° μ§λ¦μ΄ λλ€λ κ²μ μκ² λμμ΅λλ€.
μ§κ΄μ μΌλ‘ μ΄ν΄κ° λμ§ μμ λ©λνλλ° μκ°μ΄ μ’ κ±Έλ Έλ€μ.
μ μ μ²μ νμ΄λ (λ°©ν₯ κ·Έλνλ μλμ§λ§) μ μ λ§λ€ μ§μ
κ°μ μ μλ₯Ό μΈκ³
κ°μ₯ λ§μ μ§μ
κ°μ μ κ°μ§ μ μ μμ
κ°μ₯ λ¨Ό 거리μ λ λ²μ§Έλ‘ λ¨Ό 거리λ₯Ό ν©νλ©΄ μ§λ¦μ΄ λμ§ μμκΉ μκ°νμ¬ νμμ΅λλ€.
λκ° μ λ§€νλ€κ³ μκ°μ νμ§λ§
μμ μ
λ ₯μ λν΄μ μ λ΅μ μΆλ ₯μ΄ λμ€κΈΈλ λ§λ μ€ μμμ΅λλ€.
νμ§λ§ μ΄λ° κ²½μ° κ°μ₯ λ¨Ό λ
ΈλκΉμ§μ κ²½λ‘μ λ λ²μ§Έλ‘ λ¨Ό λ
ΈλκΉμ§μ κ²½λ‘κ°
μλ‘ λ€λ₯Έ λ°©ν₯μΌλ‘ λ»μ΄κ°λ κ²½λ‘λΌλ 보μ₯μ΄ μκΈ° λλ¬Έμ μμΈ μν©μ΄ λ°μν©λλ€.
μκ³ νμ ¨μ΅λλ€!
Details
import java.io.*;
import java.util.*;
public class Main {
static int V;
static List<List<Node>> graph;
static class Node {
Integer next;
Integer weight;
Node(int next, int weight) {
this.next = next;
this.weight = weight;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
V = Integer.parseInt(br.readLine());
graph = new ArrayList<>();
for (int i = 0; i <= V; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < V; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int idx = Integer.parseInt(st.nextToken());
while (true) {
int next = Integer.parseInt(st.nextToken());
if (next == -1) break;
int weight = Integer.parseInt(st.nextToken());
graph.get(idx).add(new Node(next, weight));
}
}
int[] dist1 = bfs(1);
int maxPath = 0;
int startOfDiameter = 1;
for (int i = 2; i <= V; i++) {
if (dist1[i] > maxPath) {
maxPath = dist1[i];
startOfDiameter = i;
}
}
int[] dist2 = bfs(startOfDiameter);
int answer = 0;
for (int i : dist2) {
answer = Math.max(answer, i);
}
System.out.print(answer);
}
static int[] bfs(int idx) {
int[] dist = new int[V+1];
Arrays.fill(dist, -1);
Queue<Integer> queue = new LinkedList<>();
queue.add(idx);
dist[idx] = 0;
while (!queue.isEmpty()) {
int current = queue.poll();
for (Node v : graph.get(current)) {
int nextNode = v.next;
if (dist[nextNode] == -1) {
queue.add(nextNode);
dist[nextNode] = dist[current] + v.weight;
}
}
}
return dist;
}
}
9kyo-hwang
left a comment
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.
μ€ν μ¨ μκ³ λ¦¬μ¦ μμ λ€μ λ, κ·Έλ£Ή μ‘ν°λΉν° λ¬Έμ λ‘ λ΄μ μ μ€λͺ λ€μλ κΈ°μ΅μ΄ μλ€μ. λ¬Όλ‘ κ·Έλλ μ΄ν΄ λͺ»νμ§λ§...
- μ무 λ Έλ(λ³΄ν΅ λ£¨νΈ)λ₯Ό μ‘κ³ DFS -> κ°μ₯ λ¨Ό λ Έλ νλ
- κ°μ₯ λ¨Ό λ Έλ κΈ°μ€μΌλ‘ λ€μ DFS -> λ€μ κ°μ₯ λ¨Ό λ Έλ νλ
- λ κ°μ₯ λ¨Ό λ Έλ κ° κΈΈμ΄κ° νΈλ¦¬μ μ§λ¦
μ μμ΄λμ΄λ§ μμ§νκ³ μλ€λ©΄ ꡬνμ λλ¦ κ°λ¨νκ² ν μ μλ κ² κ°λ€μ :) 1967 νΈλ¦¬μ μ§λ¦ λκ°μ λ¬Έμ λκΉ μ μ λ λ¨ΉνμμΌ ^^7
#include <iostream>
#include <vector>
using namespace std;
using pii = pair<int, int>;
int V;
vector<vector<pii>> tree;
vector<bool> visited;
int max_node = 0, max_dist = 0;
void dfs(int from, int total_dist = 0) {
visited[from] = true;
if (total_dist > max_dist) {
max_node = from;
max_dist = total_dist;
}
for (const auto &[to, distance] : tree[from]) {
if (!visited[to]) {
dfs(to, total_dist + distance);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> V;
tree.resize(V + 1);
for (int i = 1; i <= V; ++i) {
int src_num;
cin >> src_num;
int dst_num, dst_dist;
while (cin >> dst_num && dst_num != -1) {
cin >> dst_dist;
tree[src_num].emplace_back(dst_num, dst_dist);
}
}
visited.assign(V + 1, false);
dfs(1);
visited.assign(V + 1, false);
dfs(max_node);
cout << max_dist;
return 0;
}
kangrae-jo
left a comment
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.
νΈλ¦¬μ μ§λ¦μ ꡬνλ λ°©λ²μ μκ³ μμ΄λ μ΄λ ΅λ€μ.
50λΆ μ λ κ±Έλ Έμ΅λλ€.
μ²μμ μ΄λ€ λ
Έλλ‘ λΆν° κ°μ₯ λ¨Ό λ
Έλλ₯Ό μ°Ύκ³ ,
κ·Έ λ
Έλμμ κ°μ₯ λ¨Ό λ
ΈλκΉμ§μ κ°μ€μΉκ° νΈλ¦¬μ μ§λ¦μ΄μ£ .
μ²μ λ
Έλλ₯Ό μ°Ύμλ, κ·Έ λ
Έλλ₯Ό μ μ₯νλ λ°©μμ μκ°ν΄λ΄€λλ°,
μ λ solution ν¨μμμ 리ν΄μ ν΄λ²λ¦¬κ² νκ³ μΆμ΄μ pair<int,int> λ‘ λκ³ weightμ endnodeλ‘μ νμ©νμ΅λλ€.
κ·Έ κ³Όμ μμ endnode λ₯Ό = 0 μΌλ‘ μ΄κΈ°ν ν΄λ²λ¦¬λ μλͺ»μ ν΄μ ν 10λΆ μ΄μ λ λ¦° κ² κ°λ€μ.
μ’μ λ¬Έμ κ°μ¬ν©λλ€.
κ·Έλ¦¬κ³ λ°©λ¬Έμ²λ¦¬λ₯Ό forλ¬Έ λ°μμ ν΄μΌνλ μ΄μ λ μ νν λͺ¨λ₯΄κ² μ§λ§...
μλ§ if(len> max)μ΄ λ‘μ§μ΄ forλ¬Έ λ°μ μμ΄μ κ·Έλ°κ±° μλκΉμ.
λ©μλκ° intλ₯Ό 리ν΄νκ² νλ©΄ λ¬Έμ κ° μμ κ² κ°μμ (ν΄λ³΄μ§λμμ)
#include <iostream>
#include <vector>
using namespace std;
int V;
vector<vector<pair<int, int>>> graph;
pair<int,int> solution(vector<bool>& v, int cur) {
int answer = 0;
int node = cur;
for (auto [next, w] : graph[cur]) {
if (v[next]) continue;
v[next] = true;
auto [candiW, candiN] = solution(v, next);
if (answer < candiW + w) {
answer = candiW + w;
node = candiN;
}
}
return {answer, node};
}
int main() {
cin >> V;
graph = vector<vector<pair<int, int>>>(V + 1);
for (int i = 1; i <= V; i++) {
int a, b, w;
cin >> a;
while (cin >> b && b != -1) {
cin >> w;
graph[a].push_back({b, w});
graph[b].push_back({a, w});
}
}
vector<bool> visited(V + 1, false);
visited[1] = true;
auto [weigth, node] = solution(visited, 1);
visited.assign(V + 1, false);
visited[node] = true;
cout << solution(visited, node).first;
return 0;
}
π λ¬Έμ λ§ν¬
νΈλ¦¬μ μ§λ¦
βοΈ μμλ μκ°
1h
β¨ μλ μ½λ
μ¬μ€ νμκ° κΉμ§ κ±Έλ¦¬μ§ μκ³ , μ루μ μ μκ°λ§ νλ€λ©΄ κΈλ°© ν μ μλ λ¬Έμ μ λλ€.
μ°μ μ€ν΄νλ©΄ μλ κ²μ΄ "νΈλ¦¬μ κ°λ "μ λλ€. νΈλ¦¬λ μ΄λ€ 쑰건μ κ°μ§ κ·Έλνμ λλ€.
μ΄ μ¬μ€μ κΉ¨λ«λλ€λ©΄ μ루μ μ΄ κ½€λ μ½κ² λ μ€λ₯Όμ§λ λͺ¨λ¦ λλ€.
λ¬Έμ λ₯Ό νΈλ λ°©λ²μ μΌλ¨ μμμ λ Έλλ‘λΆν° κ°μ₯ λ¨Ό 거리μ λ Έλλ₯Ό μ°Ύμ΅λλ€. κ·Έλ¦¬κ³ κ·Έ λ Έλλ‘λΆν° λ€λ₯Έ λ ΈλκΉμ§μ 거리 μ€ κ°μ₯ ν° κ°μ μ°Ύλκ±°μ£ .
κ·Έλ¦Όμ 보면 μ’ λ μ½κ² μ΄ν΄κ° κ°λ₯ν©λλ€. μμμ λ Έλλ‘λΆν° κ°μ₯ λ©λ¦¬ λ¨μ΄μ Έμλ λ Έλλ λ¨ 1κ° μ λλ€. μ΄ 5λ² λ Έλλ‘λΆν° κ°μ₯ λ©λ¦¬ λ¨μ΄μ Έμλ λ Έλλ₯Ό μ°ΎμΌλ©΄? 그건 νΈλ¦¬μ μ§λ¦μ΄ λκ² μ£ .
π μλ‘κ² μκ²λ λ΄μ©
dfsλ‘ λ°©λ¬Έν λ 미리
visited = trueλ‘ μ€μ νλ©΄ μλλ€!!μ κ° 1μκ°μ΄λ μ¨λ²λ¦° μ΄μ μ λλ€. visitedλ₯Ό λ Έλμ λ°©λ¬ΈνκΈ°μ trueλ‘ μ€μ νλ©΄ λ€μ λ Έλλ₯Ό λ°©λ¬Έν λ λ¬Έμ κ° λ°μν μλ μμ΅λλ€. μ§κ΄μ μΈ μ΄ν΄κ° λμ§ μμΌλ μμ μλ κ°μ λ¬Έμ λ‘ μκ°μ μΌλ κΈ°μ΅μ΄ λ©λλ€. μ‘°μ¬νμκΈΈ λ°λλλ€.!
μμ²λΌ dfsλ₯Ό μ€ννκΈ° μ visited = trueλ‘ λ리면 μνν©λλ€! μ΄ λ¬Έμ μμ λ°©λ¬Έ νμλ₯Ό dfsμ μ νκ³ μΆλ€λ©΄ max κ°μ λλ²μ§Έ dfsμ€ννκΈ° μ μ μ΄κΈ°νν΄μ£Όλ©΄ λ©λλ€. κΆκΈνλ€λ©΄ μλ μ½λλ₯Ό μ°Έκ³ ν΄λ³΄μΈμ©
warning