기본 콘텐츠로 건너뛰기

map 사용법

#include <cstdio>
#include <map>
#include <string>
#include <vector>
using namespace std;
char b1[55], b2[55];
map <string, vector <string> > mp;
map <string, vector <string> > ::iterator it;
void solve(){
int n;
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%s %s", b1, b2);
mp[b2].push_back( b1 );
}
long long ans = 1;
for(it = mp.begin(); it != mp.end(); it++){
ans = ans * (it->second.size() + 1 );
}
printf("%lld\n", ans-1);
mp.clear();
}
int main(){
int T;
scanf("%d", &T);
while( T-- ){
solve();
}
}

댓글

이 블로그의 인기 게시물

Tree traversal의 3가지

1. 전위 순회 (Preorder Traversal) Root -> Left Tree -> Right Tree   ( 루트를 제일 처음에 방문 ) 2. 중위 순회 (Inorder Traversal) Left Tree -> Root -> Right Tree   ( 루트를 중간에 방문 ) 3. 후위 순회 (Postorder Traversal) Left Tree -> Right Tree -> Root   ( 루트를 제일 마지막에 방문 ) <소스코드> 출처 : http://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder /// C program for different tree traversals #include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child    and a pointer to right child */ struct node {      int data;      struct node* left;      struct node* right; }; /* Helper function that allocates a new node with the    given data and NULL left and right pointers. */ struct node* newNode(int data) {      struct node* node = (struct node*)                 ...