{"id":34334,"date":"2024-11-01T09:26:57","date_gmt":"2024-11-01T09:26:57","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34334"},"modified":"2024-11-01T10:57:39","modified_gmt":"2024-11-01T10:57:39","slug":"c-coding-test-course-understanding-friend-relationships-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34334\/","title":{"rendered":"C++ Coding Test Course, Understanding Friend Relationships"},"content":{"rendered":"<p><body><\/p>\n<p>\n        This article discusses the process of solving an algorithm problem that identifies friendship relationships using C++. The problem involves applying graph theory based on the number of friends and their relationships to understand the friendship network. In this context, we will explore the fundamental concepts of data structures and algorithms, and examine the implementation methods in C++ in detail.\n    <\/p>\n<h2>Problem Description<\/h2>\n<p>\n<strong>Problem:<\/strong> There are N students. Each student has the potential to have friends. The friendship relationship is bidirectional, and given the friendship relationships, implement an algorithm to determine how many friends a specific student has. Additionally, it should be able to check whether two students are friends with each other.\n    <\/p>\n<p>\n        Input:<\/p>\n<ul>\n<li>The first line contains the number of students N (1 \u2264 N \u2264 1000).<\/li>\n<li>The second line contains M (0 \u2264 M \u2264 N*(N-1)\/2) friendship relationships.<\/li>\n<li>Each friendship relation consists of A and B, where A is a friend of B and B is a friend of A.<\/li>\n<li>Finally, queries are given to check the friendship relationships.<\/li>\n<\/ul>\n<h2>Problem Solving Process<\/h2>\n<p>\n        To solve the problem, we will go through the following steps:<\/p>\n<ol>\n<li><strong>Graph Modeling:<\/strong> Model the friendship relationships as a graph. The vertices represent students, and the edges represent friendship relationships.<\/li>\n<li><strong>Constructing an Adjacency List:<\/strong> Create an adjacency list representing friends for each vertex.<\/li>\n<li><strong>Count Friends:<\/strong> Implement a function to calculate the number of friends of a specific student.<\/li>\n<li><strong>Check Friendship Relationships:<\/strong> Implement a function to check the friendship relationship between two students.<\/li>\n<\/ol>\n<h3>1. Graph Modeling<\/h3>\n<p>\n        To solve the given problem, we need to first model the friendship relationships in the form of a graph. In this case, the vertices of the graph represent students, and the edges represent friendship relationships between those students.\n    <\/p>\n<p>\n        Therefore, assuming there are N students, we can represent them with integers ranging from 0 to N-1. For example, student A can be represented as 0, and student B as 1.\n    <\/p>\n<h3>2. Constructing an Adjacency List<\/h3>\n<p>\n        We use an adjacency list to represent the graph. We can use a hashmap or vector where each student is a key and the list of students who are friends with that student is the value. Below is a method for constructing the adjacency list in C++:\n    <\/p>\n<pre><code class=\"cpp\">\n#include <iostream>\n#include <vector>\n#include <unordered_map>\n#include <string>\n\nusing namespace std;\n\nunordered_map<int, vector<int> > friends;\n\nvoid addFriendship(int a, int b) {\n    \/\/ Add friendship (bidirectional)\n    friends[a].push_back(b);\n    friends[b].push_back(a);\n}\n    <\/int,><\/code><\/pre>\n<p>\n        In the above code, the <code>addFriendship<\/code> function adds a friendship relationship between two students.\n    <\/p>\n<h3>3. Counting Friends<\/h3>\n<p>\n        To count the number of friends of a specific student, we implement a function that returns the size of that student&#8217;s adjacency list. Below is an example implementation:\n    <\/p>\n<pre><code class=\"cpp\">\nint countFriends(int student) {\n    return friends[student].size();\n}\n    <\/code><\/pre>\n<p>\n        This code returns the size of the list of friends for a specific student from the adjacency list.\n    <\/p>\n<h3>4. Checking Friendship Relationships<\/h3>\n<p>\n        We also need a function to check whether two students are friends. This function searches through the adjacency list to see if the two students are in the same list of friends. Below is an example implementation:\n    <\/p>\n<pre><code class=\"cpp\">\nbool areFriends(int a, int b) {\n    \/\/ Get A's list of friends\n    for (int friend_id : friends[a]) {\n        if (friend_id == b) {\n            return true; \/\/ Friendship exists\n        }\n    }\n    return false; \/\/ Friendship does not exist\n}\n    <\/code><\/pre>\n<p>\n        The above function checks if student A has student B in their list of friends.\n    <\/p>\n<h2>Complete Code<\/h2>\n<p>\n        Now, I will write the complete code incorporating all the explanations given so far:\n    <\/p>\n<pre><code class=\"cpp\">\n#include <iostream>\n#include <vector>\n#include <unordered_map>\n\nusing namespace std;\n\nunordered_map<int, vector<int> > friends;\n\nvoid addFriendship(int a, int b) {\n    friends[a].push_back(b);\n    friends[b].push_back(a);\n}\n\nint countFriends(int student) {\n    return friends[student].size();\n}\n\nbool areFriends(int a, int b) {\n    for (int friend_id : friends[a]) {\n        if (friend_id == b) {\n            return true;\n        }\n    }\n    return false;\n}\n\nint main() {\n    int N, M;\n    cout << \"Enter the number of students (N) and the number of friendship relations (M): \";\n    cin >> N >> M;\n\n    cout << \"Enter friendship relations (e.g., A B): \" << endl;\n    for (int i = 0; i < M; i++) {\n        int a, b;\n        cin >> a >> b;\n        addFriendship(a, b);\n    }\n\n    int query;\n    cout << \"Enter the number of the student to check the number of friends: \";\n    cin >> query;\n    cout << \"Student \" << query << \" has \" << countFriends(query) << \" friends.\" << endl;\n\n    int a, b;\n    cout << \"Enter the numbers of the two students to check their friendship relationship: \";\n    cin >> a >> b;\n    if (areFriends(a, b)) {\n        cout << \"Student \" << a << \" and student \" << b << \" are friends.\" << endl;\n    } else {\n        cout << \"Student \" << a << \" and student \" << b << \" are not friends.\" << endl;\n    }\n\n    return 0;\n}\n    <\/int,><\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        In this tutorial, we closely examined the process of solving an algorithm problem that identifies friendship relationships using the C++ language. We learned how to model friendship relationships using important concepts from graph theory and data structures, and how to solve the problem through this modeling. Skills in solving such algorithmic problems are crucial for coding tests for employment, so I encourage you to strengthen your abilities through a variety of problems.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article discusses the process of solving an algorithm problem that identifies friendship relationships using C++. The problem involves applying graph theory based on the number of friends and their relationships to understand the friendship network. In this context, we will explore the fundamental concepts of data structures and algorithms, and examine the implementation methods &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34334\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Understanding Friend Relationships&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34334","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Understanding Friend Relationships - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34334\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Understanding Friend Relationships - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"This article discusses the process of solving an algorithm problem that identifies friendship relationships using C++. The problem involves applying graph theory based on the number of friends and their relationships to understand the friendship network. In this context, we will explore the fundamental concepts of data structures and algorithms, and examine the implementation methods &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Understanding Friend Relationships&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34334\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:26:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:39+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34334\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34334\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Understanding Friend Relationships\",\"datePublished\":\"2024-11-01T09:26:57+00:00\",\"dateModified\":\"2024-11-01T10:57:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34334\/\"},\"wordCount\":570,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34334\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34334\/\",\"name\":\"C++ Coding Test Course, Understanding Friend Relationships - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:26:57+00:00\",\"dateModified\":\"2024-11-01T10:57:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34334\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34334\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34334\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Understanding Friend Relationships\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Understanding Friend Relationships - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/34334\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Understanding Friend Relationships - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"This article discusses the process of solving an algorithm problem that identifies friendship relationships using C++. The problem involves applying graph theory based on the number of friends and their relationships to understand the friendship network. In this context, we will explore the fundamental concepts of data structures and algorithms, and examine the implementation methods &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Understanding Friend Relationships\"","og_url":"https:\/\/atmokpo.com\/w\/34334\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:26:57+00:00","article_modified_time":"2024-11-01T10:57:39+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34334\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34334\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Understanding Friend Relationships","datePublished":"2024-11-01T09:26:57+00:00","dateModified":"2024-11-01T10:57:39+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34334\/"},"wordCount":570,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34334\/","url":"https:\/\/atmokpo.com\/w\/34334\/","name":"C++ Coding Test Course, Understanding Friend Relationships - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:26:57+00:00","dateModified":"2024-11-01T10:57:39+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34334\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34334\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34334\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Understanding Friend Relationships"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34334"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34334\/revisions"}],"predecessor-version":[{"id":34335,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34334\/revisions\/34335"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}