λ¬Έμ
μ½λ
#include <iostream>
#include <vector>
#include <unordered_map>
#include <sstream>
using namespace std;
int N, M;
unordered_map<string, vector<pair<int ,string>>> graph;
unordered_map<string, int> visited;
int file_cnt, file_type_cnt;
void dfs(string node){
for(auto it: graph[node]){
if(it.first == 0){
if(!visited[it.second]){
visited[it.second] = 1;
file_type_cnt++;
}
file_cnt++;
}
if(it.first == 1){
dfs(it.second);
}
}
}
vector<string> parsing(string s){
vector<string> v;
istringstream ss(s);
string stringbuffer;
while(getline(ss, stringbuffer, '/')){
v.push_back(stringbuffer);
}
return v;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>N>>M;
for(int i=0; i<N+M; i++){
string P, E; int C; cin>>P>>E>>C;
vector<pair<int ,string>> v = graph[P];
v.push_back({C, E});
graph[P] = v;
}
int Q; cin>>Q;
for(int i=0; i<Q; i++){
string s; cin>>s;
vector<string> v = parsing(s);
dfs(v.back());
cout<<file_type_cnt << ' '<< file_cnt << '\n';
file_cnt = 0;
file_type_cnt = 0;
visited.clear();
}
}
νμ΄
dfsλ¬Έμ μΈλ°, λ Έλμ μ 보λ€μ mapμ μ νμ©ν΄μ μ μ₯ν΄μΌ νλ λ¬Έμ μ λλ€.
λ Έλμ μ 보λ₯Ό μ μ₯νλ λΆλΆμ λ€μκ³Ό κ°μ΅λλ€. unordered_map<string, vector<pair<int ,string>>> graph;
3 4
main FolderA 1
main FolderB 1
FolderA File1 0
FolderA File2 0
FolderB FolderC 1
FolderB File1 0
FolderB File3 0
λ€μ μ μΆλ ₯μ μμλ‘ λ€μ΄ graphμ μ μ₯νλ©΄ λ€μκ³Ό κ°μ΅λλ€.
- [main] = {{1, "FolderA"}, {1, "FolderB"}
- [FolderA] = {{0, "File1"}, {0, "File2"}}
- [FolderB] = {{1, "FolderC"}, {0, "File1}, {0, "File3"}}
κ·Έ ν dfsλ₯Ό μ΄μ©νμ¬ ν΄λμΌκ²½μ° κ³μ μ§μ νμ¬ νμνλ©΄μ νμΌμ κ°μλ₯Ό μ°Ύμμ£Όλ©΄ λ©λλ€.
μκ° λ³΅μ‘λλ dfs: O(N), 쿼리 κ°μ: O(Q) μ΄λ―λ‘ O(N^2)μ΄ μμλ©λλ€.
'Algorithm π§π»βπ» > λ°±μ€(BOJ)' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€,c++] 1038λ² - κ°μνλ μ (0) | 2022.09.27 |
---|---|
[λ°±μ€,c++] 16935λ² - λ°°μ΄ λ리기 3 (0) | 2022.09.22 |
[λ°±μ€,c++] 10703λ² - μ μ± (0) | 2022.09.21 |
[λ°±μ€,c++] 14500λ² - ν νΈλ‘λ―Έλ Έ (0) | 2022.09.18 |
[λ°±μ€,c++] 21278λ² - νΈμμ΄ λ λ§λ¦¬ μΉν¨ (0) | 2022.09.18 |
[λ°±μ€,c++] 15661λ² - λ§ν¬μ μ€ννΈ (0) | 2022.09.18 |
λκΈ