์ƒˆ์†Œ์‹

Programming Problem Solving

[PPS] Task Force (C++)

  • -
๋ฐ˜์‘ํ˜•

< Problem >

The Marine Corps wants to form a special task-force team for a critical mission. The team members are selected from their soldiers. The Marine Corps made the following member selection rule for a strong fellowship among the team members: every member of the special force team has at least ๐‘˜ friends in the special force team.

 

Given the friendship relations between the soldiers, write a program that finds the maximum size of the special task-force team that satisfies this rule.

For example, suppose that there are 5 soldiers, and their friendship relations are represented as a graph shown above: a node represents a soldier and an edge the friendship between a pair of soldiers. For ๐‘˜ =2, there are 3 teams that satisfy the rule, that is, {2,3,4

}, {2,4,5} and {2,3,4,5}. Hence, the answer is 4, which is the maximum number of team members over all possible teams satisfying the rule. For ๐‘˜ is 3, no team satisfies the rule.

 

 

< Requirements >
Input Data

  • The first line from the standard input has three integers ๐‘›, ๐‘˜, and ๐‘“, where ๐‘› is the number of soldiers, ๐‘˜ is the minimum number of friends that a soldier must have to join the team, and ๐‘“ is the total number
    of friendship relations, for 1≤๐‘˜ <๐‘›≤2000 and 1≤๐‘“ ≤๐‘›(๐‘›−1). The soldiers have IDs from 1 to ๐‘›.
  • Each of the next ๐‘“ lines contains two integers that represent the IDs of two soldiers who are in a friendship.

Output Data

  • Your program should print out the maximum size (an integer) of the special task-force team to the standard output.
  • If there are no teams that satisfy the rule, print out 0 to the standard output.
  • Your program should return the result within 0.5 seconds.

 

 

<Problem Analysis>

 n ๋ช…์˜ ํ•ด๊ตฐ๋“ค ์ค‘ ํŠน์ˆ˜ํŒ€ ๋‚ด์— k๋ช… ์ด์ƒ์˜ ์นœ๊ตฌ๋ฅผ ๊ฐ€์ง„ ํ•ด๊ตฐ๋“ค๋กœ๋งŒ ํŠน์ˆ˜ํŒ€์„ ๊พธ๋ฆฐ๋‹ค. ํŠน์ˆ˜ํŒ€์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ธ์›์„ ๊ตฌํ•˜๋ผ

-> ํŠน์ˆ˜ํŒ€ ๋‚ด์— k ๋ช… ์ด์ƒ์˜ ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๊ธฐ์—, k ๋ช… ์ด์ƒ์˜ ์นœ๊ตฌ๊ฐ€ ์—†๋Š” ์‚ฌ๋žŒ๋“ค์„ ๋ฐฐ์ œํ•˜๊ณ  ์ƒ๊ฐํ•œ๋‹ค. 

 

<Solution>

  1. ์ž…๋ ฅ ๋ฐ›์€ ์นœ๊ตฌ ๊ด€๊ณ„ ๊ฐ’์„ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. 
  2. ์นœ๊ตฌ ๊ด€๊ณ„์ธ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ์‚ฌ์ด์ฆˆ๊ฐ€ k ๋ฏธ๋งŒ์ด๋ฉด DFS ๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ(๊ตฐ์ธ)๋“ค๊ณผ์˜ ์—ฐ๊ฒฐ์„ ์ง€์šฐ๊ณ  ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์˜ ๋ฆฌ์ŠคํŠธ ์‚ฌ์ด์ฆˆ๊ฐ€ k ๋ฏธ๋งŒ์ธ์ง€ ๋‹ค์‹œ ํ™•์ธํ•œ๋‹ค. 
  3. ๋ฆฌ์ŠคํŠธ ์‚ฌ์ด์ฆˆ๊ฐ€ k๋ฏธ๋งŒ์ด๋ฉด ์œ„ ์ž‘์—…์„ DFS๋ฅผ ํƒ€๊ณ  ๋‹ค๋‹ˆ๋ฉด์„œ ๋ฐ˜๋ณตํ•œ๋‹ค. 
  4. ๋งŒ์•ฝ ์—ฌ๊ธฐ์„œ ์ธ์ ‘๋ฆฌ์ŠคํŠธ ์‚ฌ์ด์ฆˆ๊ฐ€ k๊ฐœ ์ด์ƒ์ธ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด 0์„ ๋ฆฌํ„ดํ•œ๋‹ค. 
  5. ์ธ์ ‘๋ฆฌ์ŠคํŠธ ์‚ฌ์ด์ฆˆ๊ฐ€ k๊ฐœ ์ด์ƒ์ธ ๋…ธ๋“œ๋“ค์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •๋‹ต์ด ๋œ๋‹ค. 

<Asymptotic time complexity>

n = soldiers (๋…ธ๋“œ) , r = relations

O(n) + O(r) + O(n) =  O(n + r) 

 

<Code>

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Graph {
public:
    int N;
    vector<vector<int> > adj;

    Graph(int vertices) {
        N = vertices + 1; 
        adj.resize(N);
    }

    void addEdge(int u, int v) {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    int DFS(int start, int k) {
        vector<bool> visited(N, false);
        queue<int> s;
        queue<int> head;
        int count = 0;

        for (int i=1; i<=start; i++) {
            s.push(i);
        }

        while (!s.empty()) {
        
            int vertex = s.front();
            s.pop();

            if (!visited[vertex]) {
                visited[vertex] = true;

                int next_vertex = -1;  // ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜

                if (adj[vertex].size() < k) {
                    if (adj[vertex].size() == 0) continue;

                    for (int i = 0; i < adj[vertex].size(); i++) {
                        int neighbor = adj[vertex][i];
                        removeFriend(neighbor, vertex);

                        if (adj[neighbor].size() < k) {
                            visited[neighbor] = false;
                            s.push(neighbor);
                        }
                    }
                    adj[vertex].clear();
                }
            }
        }

        for (int i=1; i<=start; i++) {
           if (adj[i].size() != 0) {
               count++;
           }
        }

        return count;
    }

    void removeFriend(int vertex, int head) {
        for (int i = 0; i < adj[vertex].size(); i++) {
        	if (adj[vertex][i] == head) {
            	adj[vertex].erase(adj[vertex].begin() + i);
                 break;
            }
        }
    }
};

int main() {
    int soldiers = 0;
    int k = 0;
    int relation = 0;
    int a, b;

    cin >> soldiers >> k >> relation;

    Graph g(soldiers); 

    for (int i = 0; i < relation; i++) {
        cin >> a >> b;
        g.addEdge(a, b);
    }
  
    int specialTeam = g.DFS(soldiers, k);
    cout << specialTeam << endl;

    return 0;
}
๋ฐ˜์‘ํ˜•

'Programming Problem Solving' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[PPS] Electricity Poles (C++)  (0) 2024.02.07
Contents
-

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.