-
Notifications
You must be signed in to change notification settings - Fork 2
22-hyeokbini #77
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
22-hyeokbini #77
Conversation
flydongwoo
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.
์ ๋ ์๋ ฅ์์ ์คํจํ์ต๋๋ค
๊ณจ๋๋ ์ญ์ ๊ณจ๋๋ต๋ค์ ,,,
์ผ๋จ BFS๋ก ํ์ด์ผํ๋ค๋ ๊ฒ์ ์ฐพ์ ์ํ์์ต๋๋ค
์ฝ๋ ๊ณต์ ๋๋ฆฝ๋๋ค. ๋ฌธ์ ํธ๋๋ผ ๊ณ ์ํ์ จ์ด์~!
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
static const long long MOD = 1000000007LL;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<vector<int>> g(N + 1);
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> dist(N + 1, -1);
queue<int> q;
dist[1] = 0;
q.push(1);
int maxDepth = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (size_t i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
if (dist[v] > maxDepth) {
maxDepth = dist[v];
}
q.push(v);
}
}
}
vector<long long> cnt(maxDepth + 1, 0);
for (int i = 1; i <= N; i++) {
if (dist[i] != -1) {
cnt[dist[i]]++;
}
}
long long ways = 1;
for (int d = 0; d <= maxDepth; d++) {
ways = (ways * ((cnt[d] + 1) % MOD)) % MOD;
}
ways = (ways - 1 + MOD) % MOD;
cout << ways << endl;
return 0;
}
Seol-Munhyeok
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.
๊ฐ์ ๋ ๋ฒจ์ ์๋ ๋
ธ๋๋ผ๋ฆฌ๋ ๋ฐ๋์ ์ถฉ๋์ด ๋ฐ์ํ๋ค. ๋ผ๋ ๋ฐ์์ด ์๊ณ ๋๋ฉด ๋น์ฐํ ๊ฒ์ฒ๋ผ ์๊ฐ๋๋๋ฐ ์ฒ์๋ถํฐ ์ด ๋ฐ์์ ํ๋ค๋๊ฒ ์ด๋ ค์ด ๋ฌธ์ ์์ต๋๋ค.
์ผ๋จ ์ฒซ ๋ฒ์งธ ์์ ๊ฐ์ ๊ฒฝ์ฐ
1
/
2
/
3
์ด๋ฐ ๊ทธ๋ํ๋๊น
๊ฒฐ๊ตญ ๊ทธ ์๋ฏธ๊ฐ ๊ฐ ๋ ๋ฒจ์ ๋
ธ๋๊ฐ 1๊ฐ์ฉ ์์ผ๋๊น ๊ทธ๋ ๊ฒ ๋์จ ๊ฑฐ๋ค ํ๊ณ ๋์ค์ ์๊ฒ๋์์ต๋๋ค.
์ฝ๋ฉํ
์คํธ ๋ฌธ์ ๋ฅผ ํ ๋ ๊ฐ์ฅ ์ค์ํ ์ํ ์ง์์ 'ํ๋ฅ ๊ณผ ํต๊ณ'์์ ๋ฐฐ์ด ์ง์ ๊ฐ์ต๋๋ค.
ํํ '์กฐํฉ๋ก '์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ ์ ํ๋ ๋ง์ด ํ์ด๋ด์ผํ ๊ฑฐ ๊ฐ์ต๋๋ค.
ํ์ด : http://boj.kr/842a576a84e546cc8b274159dedc2a96
์๊ณ ํ์ต๋๋ค!!!!
|
์ ๋ ์ฒ์์ ์กฐํฉ + ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๋ฏธ๋ฆฌ ์๋ฎฌ๋ ์ด์
ํด๋
ผ ๊ฒฐ๊ณผ๋ก ํ๋ ค๋ค๊ฐ ์๋ฌด๋๋ ์๊ฐ ์ด๊ณผ๊ฐ๋ ๊ฒ ๊ฐ์ ๋๋์ด ๊ฐํด์ ํ์ด ์ฐธ๊ณ ํ์ต๋๋ค. ์ถฉ๋์ ๋ฐ์์ํค์ง ์๊ธฐ ์ํด ํธ๋ฆฌ์ ๊น์ด๋ฅผ ์๊ฐํด๋ด๋ ๊ฒ์ด ์ฝ์ง ์์๋ณด์ด๋ค์. ํ์ด๋ฅผ ์๊ฐํด๋ด๊ธฐ ๊น์ง๊ฐ ์ด๋ ค์ด ๋ฌธ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ์๊ณ ํ์
จ์ต๋๋ค. ์ฐจ๋ ๋ฐฐ์น#include <bits/stdc++.h>
using namespace std;
#define N 1000000007
int main() {
// freopen("input.txt", "rt", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m; cin >> n >> m;
vector<vector<int>> adj(n);
for(int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<bool> visited(n, false);
visited[0] = true;
map<int, int> depth;
depth[0] = 1;
queue<pair<int, int>> q; q.push({0, 0});
while(!q.empty()) {
auto tp = q.front(); q.pop();
for(auto idx : adj[tp.first]) {
if(!visited[idx]) {
visited[idx] = true;
depth[tp.second + 1] += 1;
q.push({idx, tp.second + 1});
}
}
}
long long res = 1;
for(auto d : depth) {
res = res * (d.second + 1) % N;
}
cout << res - 1;
return 0;
}
|
๐ ๋ฌธ์ ๋งํฌ
๊ณจ๋ 4
์ฐจ๋ ๋ฐฐ์น
โ๏ธ ์์๋ ์๊ฐ
2์๊ฐ
โจ ์๋ ์ฝ๋
์ฐ์ ์๋ ฅ์์ ์คํจํ์ต๋๋ค. ๋ฐฉํฅ์ฑ์ ์๋ชป ์ก์๋ ๊ฒ ํฌ๊ฒ ๊ตด๋ฌ๊ฐ๋ค์. ๋ธ๋ก๊ทธ์ ํ์ด๋ฅผ ๋ณด๊ณ ์์ด๋์ด๋ฅผ ์ป์ด์ ๊ตฌํํ์ต๋๋ค.
๋ฌธ์ ์์ ์๊ตฌํ๋ ๋ด์ฉ์ ์ฐจ๋์ด ๊ฐ ์ง์ ์ ๋ค์ํ ๊ฒฝ์ฐ์ ์๋ก ๋ค์ด๊ฐ ์๊ณ ๋๋ก๋ฅผ ๋ฐ๋ผ ์ต๋ํ ๋ฎ์ ๋ฒํธ์ ์ง์ ์ผ๋ก ๋์์ ์ด๋ํ ๋, ์ถฉ๋(ํ ์ง์ ์ 2๊ฐ ์ด์์ ์ฐจ๋์ด ๋ค์ด๊ฐ) ์ด ์ผ์ด๋์ง ์๊ณ ๋ชจ๋ ์ฐจ๋์ด 1๋ฒ ์ง์ ์ ํตํด ๋ฐ์ผ๋ก ๋๊ฐ ์ ์๋ ์ฐจ๋ ๋ฐฐ์น ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ ๊ฒ์ ์๊ตฌํฉ๋๋ค.
์คํจ ์์ด๋์ด
์ฒซ ์์ด๋์ด๋ ์กฐํฉ + ์๋ฎฌ๋ ์ด์ ์ ํตํด ์ค์ ์ฐจ๋์ ๋ฐฐ์นํ ๋ค, ๋ชจ๋ ์ฐจ๋์ ๋บ ์ ์๋์ง๋ฅผ ํ์ํ์ต๋๋ค.
์ฐจ๋ ๋ฐฐ์น / ์คํจ
ํ์ง๋ง ๋ช ๋ฒ ์๋ํด๋ดค์ ๋, ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๋ ์๊ฐ ์ด๊ณผ๋ฅผ ๋จน์์ต๋๋ค.
์ด๋ป๊ฒ ๋ณด๋ฉด ๋น์ฐํ ๊ฒฐ๊ณผ์ธ๋ฐ, ์ ๋ ฅ์ผ๋ก ์ฃผ๋ ๋ ธ๋ / ๊ฐ์ ์ ๊ฐ์๊ฐ ์ต๋ 20๋ง์ ๋๋ค. ๋ฐ๋ผ์ ์ด๋ฐ ๋ธ๋ฃจํธํฌ์ค ํ์์ผ๋ก๋ ์ ๋ ์๊ฐ ๋ด์ ํต๊ณผํ ์ ์์ต๋๋ค.
ํต์ฌ ์์ด๋์ด์ ๋ํด ๊ฐํผ๋ฅผ ๋ชป ์ก๊ณ ๊ณ ๋ฏผํ๋ค๊ฐ ๊ฒฐ๊ตญ ํ์ด๋ฅผ ํ์ธํ์ต๋๋ค.
์ฑ๊ณต ์์ด๋์ด
๋ฌธ์ ๋ฅผ ์ ์ดํดํด ๋ณธ๋ค๋ฉด, ํต์ฌ์ ์ผ๋ก ์ฒ๋ฆฌํด์ผ ํ๋ ๋ถ๋ถ์ ์ฐจ๋ ์ถฉ๋์ ๊ดํ ๋ถ๋ถ์ ๋๋ค.
์ฐจ๋์ด ์์ง์ด๋ ํจํด์ ์ ํด์ ธ ์์ต๋๋ค. ๊ฐ ์ ์๋ ๋๋ก๋ค ์ค ๊ฐ์ฅ ๋ฒํธ๊ฐ ๋ฎ์ ์ง์ ์ผ๋ก ์ด๋ํ๊ณ , ๋ชจ๋ ์ฐจ๋์ ๋์์ ์ด๋ํฉ๋๋ค.
์ด๋ฅผ ๊ฑฐ๊พธ๋ก ์๊ฐํ๋ค๋ฉด ๋ฃจํธ ๋ ธ๋(1๋ฒ ์ง์ ) ์์ ํน์ ํ ์ฐจ๋์ ๊น์ด๋ฅผ ๊ณ์ฐํ ํ, ๊น์ด๊ฐ ์ค๋ณต๋๋ ์ฐจ๋์ด ์๋ค๋ฉด ๊ฒฐ๊ตญ 1๋ฒ ์ง์ ์์ ์ถฉ๋ํ๊ธฐ ๋๋ฌธ์ ํ ๋ ๋ฒจ์์ ํ๋์ ์ฐจ๋์ ๋ฝ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ชจ๋ ๋ ๋ฒจ์ ์งํํ ๋ค, ๊ณฑ์ ์ ํตํด ํด๊ฒฐํ๋ ์์ด๋์ด๋ฅผ ์ป์ ์ ์์ต๋๋ค.
์ด๋ฏธ์ง ์ถ์ฒ : https://untitledtblog.tistory.com/85
์ ์ด๋ฏธ์ง์ฒ๋ผ ๊ทธ๋ํ๊ฐ ํ์ฑ๋๋ค๋ฉด A / B,C / D,E,F๊ฐ ๊ฐ์ ๋ ๋ฒจ์ด๊ณ , ์ด๋ ๊ฒ ๋๋ ๋ชฉ๋ก๋ค์์ ์ค๋ณต๋ ๋ ๋ฒจ์ ๋ํด 2๊ฐ ์ด์์ ์ฐจ๋์ ๋ฝ์ผ๋ฉด ์ถฉ๋์ด ์ผ์ด๋ฉ๋๋ค.
๊ฐ๋จํ ์์๋ก D / F์ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํด ๋ณด๋๋ผ๋ A(1๋ฒ ๋์ฐฉ์ง์ )์์ ์ถฉ๋์ด ์ผ์ด๋ฉ๋๋ค. ๊ฐ์ ๋ ๋ฒจ์ ๋ํด ์ด๋ ํ ์กฐํฉ์ ๋ฝ์๋ ๋ฌด์กฐ๊ฑด ์ถฉ๋์ด ์ผ์ด๋จ์ ์ ์ ์์ต๋๋ค.
๋ฐ๋ผ์ ๋ ๋ฒจ์ ๊ตฌํ ๋ค, ๊ฐ ๋ ๋ฒจ์ ์ฐจ๋ ๊ฐ์ + 1(์์ ์ ํํ์ง ์๋ ์) ๋ฅผ ๊ตฌํ๊ณ , ์ด ๊ฒฝ์ฐ์ ์๋ค์ ๋ชจ๋ ๋ ๋ฆฝ์ ์ด๋ฏ๋ก ์ ๋ถ ๊ณฑํด์ฃผ๋ฉด ๊ฐ๋ฅํ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ ์ ์์ต๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
๊ทธ๋ํ ์ด๋ก ์ ์ค์ ์ ๋ ๋ฌธ์ ์ผ ์ค ์์๋๋ฐ, ๊ฐ์ธ์ ์ผ๋ก๋ ์ ๋ํน...? ๊ฐ์ฑ๋ ์์ฌ ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ๊น์ด๋ผ๋ ๊ฐ๋ ์ ์๊ฐํ๊ณ ๊ทธ๊ฑธ ์ด์ฉํด ๋ต์ ๋์ถํ๋ ์์ด๋์ด๋ฅผ ๋ ์ฌ๋ฆฌ์ง ๋ชปํ๋ค๋ฉด ๊ฐํผ๋ฅผ ์ก๊ธฐ ์ด๋ ค์ ๋ ๋ฌธ์ ๊ฐ์ต๋๋ค.