๐Ÿ’ป

[๋ฐฑ์ค€] ๋ฐฑํŠธ๋ž˜ํ‚น - 13265. ์ƒ‰์น ํ•˜๊ธฐ(๋ฌธ์ œ ํ‘ธ๋Š” ์ค‘) ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฌธ์ œํ’€์ด Baekjoon

[๋ฐฑ์ค€] ๋ฐฑํŠธ๋ž˜ํ‚น - 13265. ์ƒ‰์น ํ•˜๊ธฐ(๋ฌธ์ œ ํ‘ธ๋Š” ์ค‘)

๋˜ํšจ๋‹ˆ 2020. 2. 24. 00:03

๋ฌธ์ œ

์–ด๋ฆฐ ํ† ๋‹ˆํ‚ด์€ ์ƒ‰์น ๊ณต๋ถ€๋ฅผ ์ข‹์•„ํ•œ๋‹ค.

ํ† ๋‹ˆํ‚ด์€ ๋จผ์ € ์—ฌ๋Ÿฌ ๋™๊ทธ๋ผ๋ฏธ์™€ ๋™๊ทธ๋ผ๋ฏธ ๋‘ ๊ฐœ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์ง์„ ๋“ค ๋งŒ์œผ๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๊ณ  (๋ชจ๋“  ๋™๊ทธ๋ผ๋ฏธ๋“ค ์‚ฌ์ด์— ์ง์„ ์ด ์žˆ์„ ํ•„์š”๋Š” ์—†๋‹ค), ์—ฐ๊ฒฐ๋œ ๋‘ ๋™๊ทธ๋ผ๋ฏธ๋Š” ์„œ๋กœ ์ƒ‰์ด ๋‹ค๋ฅด๊ฒŒ ๋˜๋„๋ก ์ƒ‰์„ ์น ํ•˜๊ณ ์ž ํ•œ๋‹ค.

์ด ๊ทธ๋ฆผ์„ ์ƒ‰์น ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ์˜ ์ƒ‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋Š” ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ํ† ๋‹ˆํ‚ด์€ 2 ๊ฐ€์ง€ ์ƒ‰์ƒ์œผ๋กœ ์ƒ‰์น ์ด ๊ฐ€๋Šฅํ•œ์ง€์˜ ์—ฌ๋ถ€๋งŒ์„ ์•Œ๊ณ  ์‹ถ์–ดํ•œ๋‹ค.

๋™๊ทธ๋ผ๋ฏธ๋“ค์˜ ๋ฒˆํ˜ธ์™€ ๋™๊ทธ๋ผ๋ฏธ๋“ค์ด ์„œ๋กœ ์—ฐ๊ฒฐ๋œ ์ง์„ ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ๋™๊ทธ๋ผ๋ฏธ๋“ค์ด 2 ๊ฐ€์ง€ ์ƒ‰์ƒ์œผ๋กœ ์ƒ‰์น ์ด ๊ฐ€๋Šฅํ•œ์ง€ ์•Œ์•„๋‚ด์ž.

 

์ž…๋ ฅ

์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ๋™๊ทธ๋ผ๋ฏธ์˜ ๊ฐœ์ˆ˜ n(n<=1000)๊ณผ ์ง์„ ๋“ค์˜ ๊ฐœ์ˆ˜ m(m<=100,000)์ด ์ฃผ์–ด์ง€๊ณ , ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ m ์ค„์— ๊ฑธ์ณ ๋™๊ทธ๋ผ๋ฏธ๋“ค์ด ์—ฐ๊ฒฐ๋œ ์ง์„ ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (x y) ๋กœ ์ฃผ์–ด์ง€๋ฉด ๋™๊ทธ๋ผ๋ฏธ x ์™€ ๋™๊ทธ๋ผ๋ฏธ y ๊ฐ€ ์ง์„ ์œผ๋กœ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์—ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ๋™๊ทธ๋ผ๋ฏธ๋“ค์˜ ์ˆซ์ž๋Š” 1 ๋ถ€ํ„ฐ n ๊นŒ์ง€์ด๋‹ค.

 

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ possible ์ด๋‚˜ impossible ์„ ์ถœ๋ ฅํ•œ๋‹ค. 2 ๊ฐ€์ง€ ์ƒ‰์ƒ์œผ๋กœ ์ƒ‰์น ์ด ๊ฐ€๋Šฅํ•˜๋ฉด possible. ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด impossible ์ด๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ1

3        ----------  ๊ฐœ์ˆ˜  ----------
4 5     --- ์ฒซ๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ---
1 2
2 3
3 4
1 3
2 4
6 9    --- ๋‘๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ---
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
8 8    --- ์„ธ๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ---
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 1

 

์˜ˆ์ œ ์ถœ๋ ฅ1

impossible

possible

possible

 

์ƒ๊ฐ

์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ•  ์ง€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๊ฒฐ๊ตญ ์Šคํ„ฐ๋”” ์ „๊นŒ์ง€ ํ’€์ง€ ๋ชปํ–ˆ๋‹ค. 

ํ‘ผ ์‚ฌ๋žŒ๋“ค ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ํ˜ผ์ž ํ’€์–ด๋ณด๋„๋ก ํ•ด์•ผ๊ฒ ๋‹ค. 

 

์ž‘์„ฑํ•œ ์ฝ”๋“œ

#include<iostream>
#include<queue>

using namespace std;

int T, n, m;
bool flag;

void dfs(int idx, int cnt,  bool cir[][1001], int* color) {
   if (!flag) {
      return;
   }
   // ์ƒ‰์ƒ์€ 1, 2 ๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ
   if (color[idx] == 1) {
      for (int i = 1;i <= n;i++) {
         if (cir[idx][i]) {
            if (color[i] == 1) flag = false;
            else if (color[i] == 0) color[i] = 2;
            else continue;
            dfs(i, cnt + 1, cir, color);
         }
      }
   }
   else if (color[idx] == 2) {
      for (int i = 1;i <= n;i++) {
         if (cir[idx][i]) {
            if (color[i] == 2) flag = false;
            else if (color[i] == 0) color[i] = 1;
            else continue;
            dfs(i, cnt + 1, cir, color);
         }
      }
   }
}


int main(int* argc, char** argv) {
   cin.sync_with_stdio(false);
   cin.tie(0);

   cin >> T;

   for (int i = 0;i < T;i++) {
      cin >> n >> m;
      bool cir[1001][1001] = { false, };
      int color[1001] = { 0, };

      for (int j = 0;j < m;j++) {
         int x, y;
         cin >> x >> y;
         cir[x][y] = true;
         cir[y][x] = true;
      }

      flag = true;

      for (int i = 1;i <= n;i++) {
         bool alone = true;
         for (int j = 1;j <= n;j++) {
            if (cir[i][j]) {
               alone = false;
               break;
            }
         }
         if (!alone) {   // ํ˜ผ์ž ๋™๋–จ์–ด์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ ์‹œ์ž‘. ๋ถ™์–ด์žˆ๋Š” ๊ฒƒ๋“ค๋งŒ ๊ณ ๋ คํ•˜๋ฉด ๋จ
            color[i] = 1;
            dfs(i, 0, cir, color);
            break;
         }
      }
      
      if (flag) cout << "possible\n";
      else cout << "impossible\n";
   }

   return 0;
}

+) ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
int ch[1001];

int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);

   int T,n,m,x,y;
   cin >> T;

   while (T-- > 0)
   {
      int V, E;
      cin >> V >> E;

      vector <int> vc[1002];
      vector <bool> visited(1002, false);
      vector <int> color(1002);

      for (int i = 0; i < E; i++)
      {
         int from, to;
         cin >> from >> to;

         vc[from].push_back(to);
         vc[to].push_back(from);
      }

      int palette;
      bool ch = true;

      for (int i = 1; i <= V; i++)
      {
         if (visited[i]) continue;

         queue<int> q;
         q.push(i);
		 
         // ์ƒ‰์ƒ์€ -1, 1 ๋‘๊ฐ€์ง€
         while (!q.empty())
         {
            int here = q.front();
            q.pop();

            if (visited[here]) continue;
            visited[here] = true;
            if (color[here] == 0)
            {
               color[here] = 1;
               palette = -1;
            }
            else if (color[here] == 1)
               palette = -1;
            else if (color[here] == -1)
               palette = 1;

            for (int i = 0; i < vc[here].size(); i++)
            {
               int next = vc[here][i];
               if (color[next] != 0 && color[next] == -palette)
               {
                  ch = false;
                  break;
               }
               color[next] = palette;
               q.push(next);
            }
            if (!ch) break;
         }

      }
      if (ch) cout << "possible\n";
      else cout << "impossible\n";
   }
   return 0;
}
๋ฐ˜์‘ํ˜•
Comments