diff --git "a/11\354\233\224 30\354\235\274/1368.cpp" "b/11\354\233\224 30\354\235\274/1368.cpp" new file mode 100644 index 0000000..7a278f3 --- /dev/null +++ "b/11\354\233\224 30\354\235\274/1368.cpp" @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; +const int INF = 1e5 + 1; //»ó¼ö ¼±¾ð + +int prim(int size, int start, vector>& graph) { + int sum = 0; //ÃʱâÈ­ + vector dist(size, INF); //°¢ ³í±îÁöÀÇ ºñ¿ë + vector visited(size, false); //³í ¹æ¹® ¿©ºÎ + priority_queue, vector>, greater<>> pq; //¿ì¼±¼øÀ§ Å¥ ¼±¾ð + + //ÃʱâÈ­ + dist[start] = 0; + pq.push({ 0, start }); + + while (!pq.empty()) { + int cost = pq.top().first; //°£¼± °¡ÁßÄ¡ + int cur = pq.top().second; //ÇöÀç ³í + pq.pop(); //Á¦°ÅÇϱâ + + if (visited[cur]) //ÀÌ¹Ì È®ÀÎÇß´ø Á¤Á¡ + continue; + sum += cost; //MST °£¼± °¡ÁßÄ¡ ÃÑÇÕ + visited[cur] = true; //¹æ¹® ó¸® + + for (int i = 0; i < size; i++) { //¹Ýº¹¹®µ¹±â + if (!visited[i] && graph[cur][i] < dist[i]) { //¹Ì¹æ¹® Á¤Á¡À̸鼭 ´õ ªÀº °£¼±À» ÅëÇØ °¥ ¼ö ÀÖ´Ù¸é + dist[i] = graph[cur][i]; //°»½ÅÈÄ + pq.push({ dist[i], i }); //¿ì¼±¼øÀ§ Å¥¿¡ ³Ö¾îÁÖ±â + } + } + } + return sum; //¹Ýȯ +} + +/** + * °¢ ³íµé »çÀÌÀÇ °£¼±µµ °í·ÁÇϰí, ¿ì¹°À» ÆÄ´Â °æ¿ìµµ °í·Á? -> º¹Àâ + * ³í¿¡ Ãß°¡·Î ¸ðµç ¿ì¹°°ú ¿¬°áµÇ´Â ¼ö¿øÀÌ ÀÖ´Ù°í °¡Á¤! + * ->Á÷Á¢ ³í¿¡ ¿ì¹°À» ÆÄ´Â °æ¿ì´Â ¼ö¿ø°ú °¢ ³í »çÀÌÀÇ °£¼± °¡ÁßÄ¡¶ó°í ÇÒ ¼ö ÀÖÀ½ + * + * 0 2 2 2 5 + * 2 0 3 3 4 + * 2 3 0 4 4 + * 2 3 4 0 3 + * 5 4 4 3 0 + * + * À妽º 0 ~ n-1Àº ³í, À妽º nÀº ¼ö¿ø + * 1°³ ÀÌ»óÀÇ ³íÀº ¹Ýµå½Ã Á÷Á¢ ¿ì¹°À» ÆÄ¾ß ÇϹǷΠ¼ö¿ø(n)¿¡¼­ ½ÃÀÛÇÏ´Â ÇÁ¸² ¾Ë°í¸®Áò + */ +int main() { + int n, w; //¼±¾ð + + cin >> n; //ÀÔ·Â + vector> graph(n + 1, vector(n + 1, 0)); //ÀÌÁ߹迭 ÃʱâÈ­ + for (int i = 0; i < n; i++) { //¼ö¿øÀ¸·ÎºÎÅÍ ¹°À» ²ø¾î¿À´Â ºñ¿ë + cin >> w; //ÀÔ·Â + graph[i][n] = graph[n][i] = w; //±×·¡ÇÁ ¹«¹æÇâ Ç¥½Ã + } + + for (int i = 0; i < n; i++) { //¹Ýº¹¹® µ¹¸é¼­ ±×·¡ÇÁ ³ëµå(ºñ¿ë) ÀÔ·Â + for (int j = 0; j < n; j++) + cin >> graph[i][j]; //³íµé »çÀÌ¿¡¼­ ¹°À» ²ø¾î¿À´Â ºñ¿ë + } + + cout << prim(n + 1, n, graph); //¼ö¿ø¿¡¼­ ½ÃÀÛÇÏ´Â ÇÁ¸² ¾Ë°í¸®Áò +} \ No newline at end of file diff --git "a/11\354\233\224 30\354\235\274/16202.cpp" "b/11\354\233\224 30\354\235\274/16202.cpp" new file mode 100644 index 0000000..547119c --- /dev/null +++ "b/11\354\233\224 30\354\235\274/16202.cpp" @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +using namespace std; +typedef tuple tp; //Æ©Çà ¼±¾ð + +vector parent; //ºÎ¸ð ¹è¿­ + +//Find ¿¬»ê +int findParent(int node) { + if (parent[node] < 0) //°ªÀÌ À½¼ö¸é ·çÆ® Á¤Á¡ + return node; //node ¹Ýȯ + return parent[node] = findParent(parent[node]); //±×·¡ÇÁ ¾ÐÃàÇÏ¸ç ·çÆ® Á¤Á¡ ã±â +} + +//Union ¿¬»ê +bool unionInput(int x, int y) { + int xp = findParent(x); //ºÎ¸ðã±â + int yp = findParent(y); //ºÎ¸ðã±â + + if (xp == yp) //°°Àº ÁýÇÕ¿¡ ÀÖ´Ù¸é À¯´Ï¿Â ÇÒ ¼ö ¾øÀ½ + return false; //À¯´Ï¿Â ºÒ°¡ + if (parent[xp] < parent[yp]) { //»õ·Î¿î ·çÆ® xp + parent[xp] += parent[yp]; //³ëµå°³¼ö Ç¥½Ã + parent[yp] = xp; //xp¸¦ ºÎ¸ð·Î + } + else { //»õ·Î¿î ·çÆ® yp + parent[yp] += parent[xp]; //³ëµå°³¼ö Ç¥½Ã + parent[xp] = yp; //yp¸¦ ºÎ¸ð·Î + } + return true; //À¯´Ï¿ÂÇßÀ½ +} + +int kruskal(int n, int idx, vector& edges) { + int cnt = 0, sum = 0; //ÃʱâÈ­ + for (int i = idx; i < edges.size(); i++) { + if (cnt == n - 1) //n-1°³ÀÇ °£¼±À» ¸ðµÎ ¿¬°áÇÔ + break;//Áß´Ü + int dist = get<0>(edges[i]); //Æ©Ç𪠹޾ƿÀ±â + int x = get<1>(edges[i]);//Æ©Ç𪠹޾ƿÀ±â + int y = get<2>(edges[i]);//Æ©Ç𪠹޾ƿÀ±â + + if (unionInput(x, y)) { //unionÇϱâ + cnt++; //°³¼ö Ãß°¡ + sum += dist; //°Å¸® Áõ°¡ + } + } + if (cnt < n - 1) //mst¸¦ ¸¸µé ¼ö ¾øÀ½ + return 0; //0À¸·Î Ç¥½Ã + return sum; //°Å¸® ¹Ýȯ +} + +/** + * MST ¾Ë°í¸®ÁòÀ» ¿©·¯ ¹ø ½ÇÇàÇØµµ µÉ±î? + * 1. Å©·ç½ºÄ® ¾Ë°í¸®ÁòÀÇ ½Ã°£ º¹Àâµµ´Â O(ElogE) + * ÀÌ´Â ¿ÀÁ÷ °£¼±À» Á¤·ÄÇÏ´Â ¿¬»êÀÇ ½Ã°£ º¹Àâµµ! + * Áï, ¸ðµç °£¼±À» ÇÑ ¹ø Á¤·ÄÇØ¼­ ÀúÀåÇØµÎ¸é ÀÌÈÄ ¸î ¹øÀÇ ¾Ë°í¸®ÁòÀ» ¼öÇàÇÏ¿©µµ ¿¬»ê ½Ã°£¿¡ Å« ¿µÇâÀÌ ¾øÀ½ + * 2. °£¼± Àç»ç¿ëÀ» À§ÇØ ¿ì¼±¼øÀ§ Å¥°¡ ¾Æ´Ñ º¤ÅÍ¿¡ ÀúÀåÇϰí Å©·ç½ºÄ® ¾Ë°í¸®Áò k¹ø ½ÇÇà + * 3. ¸Å¹ø Å©·ç½ºÄ®À» ¼öÇàÇÒ ¶§¸¶´Ù Á¦ÀÏ ¸ÕÀú Ãß°¡ÇÑ °£¼±À» Á¦¿ÜÇÔ + * -> Á¦¿ÜµÉ °£¼±Àº ¹è¿­ÀÇ 0¹øÂ° °£¼±ºÎÅÍ 1, 2, 3¹øÂ° °£¼±À¸·Î ¼øÂ÷Àû Á¦¿Ü + * 4. ¸¸¾à ÇÑ ¹ø MST¸¦ ¸¸µé ¼ö ¾ø´Ù´Â°Ô È®Àεƴٸé ÀÌÈÄ¿¡µµ MST¸¦ ¸¸µé ¼ö ¾øÀ¸¹Ç·Î flag º¯¼ö·Î ºÒÇÊ¿äÇÑ ¿¬»ê Àý¾à + */ +int main() { + int n, m, k, x, y; //ÃʱâÈ­ + + cin >> n >> m >> k; //ÀÔ·Â + vector edges; //Àç»ç¿ëÇҰŶó ¿ì¼±¼øÀ§ Å¥°¡ ¾Æ´Ñ º¤ÅÍ¿¡ ÀúÀå + for (int i = 1; i <= m; i++) { //¹Ýº¹ + cin >> x >> y; //ÀÔ·Â + edges.emplace_back(i, x, y); //Æ©Çú¤ÅÍ¿¡ ³Ö¾îÁØ´Ù + } + + bool flag = false; //flag + for (int i = 0; i < k; i++) { //¹Ýº¹µ¹¸é¼­ mst¸¸µç´Ù + if (flag) { //´õÀÌ»ó mst¸¦ ¸¸µé ¼ö ¾øÀ½ + cout << 0 << ' '; + continue; + } + parent.assign(n + 1, -1); //ÃʱâÈ­ + int ans = kruskal(n, i, edges); //sum¹Þ±â + if (ans == 0) //0À̸é mst¸¦ ¸¸µé¼ö¾ø´Ù´Â ¶æÀ¸·Î flagÇ¥½Ã + flag = true; //true·Î ÇØ¼­ À§¿¡¼­ conitnueÇϵµ·Ï ¸¸µç´Ù + cout << ans << ' '; //ansÃâ·Â + } +} \ No newline at end of file diff --git "a/11\354\233\224 30\354\235\274/16235.cpp" "b/11\354\233\224 30\354\235\274/16235.cpp" new file mode 100644 index 0000000..f0300fc --- /dev/null +++ "b/11\354\233\224 30\354\235\274/16235.cpp" @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +typedef vector> matrix; //ÀÌÁ߹迭 +typedef tuple tp; //Æ©Çà ¿ä¼Ò 3°³ + +queue spring(matrix& land, deque& tree, queue>& breeding_tree) { + queue dead_tree; //queue¼±¾ð + int size = tree.size(); //size ¼±¾ð + while (size--) { //¸ðµç ³ª¹« °Ë»ç + int age = get<0>(tree.front()); //³ªÀÌ + int row = get<1>(tree.front()); //Çà + int col = get<2>(tree.front()); //¿­ + tree.pop_front(); //¾Õ¿¡ ÀÖ´Â °ªÀ» Á¦°ÅÇÑ´Ù. + + if (land[row][col] < age) { //ÀÚ½ÅÀÇ ³ªÀ̸¸Å­ ¾çºÐÀ» ¸ÔÀ» ¼ö ¾ø´Ù¸é + dead_tree.push({ age, row, col }); //Á×¾ú´Ù°í Ç¥½Ã + continue; //°è¼ÓÇϱâ + } + land[row][col] -= age; //³ªÀ̸¸Å­ Á¦¿ÜÇϱâ + tree.emplace_back(age + 1, row, col); //Æ®¸®¿¡ »õ·Î Ãß°¡ + if ((age + 1) % 5 == 0) //³ªÀ̰¡ 5ÀÇ ¹è¼ö¶ó¸é + breeding_tree.push({ row, col }); //»õ·Î Ãß°¡ + } + return dead_tree; +} + +void summer(queue& dead_tree, matrix& land) { + while (!dead_tree.empty()) { //Á×Àº ³ª¹«°¡ ÀÖ´Â ¹è¿­ÀÌ ºñ¾îÀÖÁö¾Ê´Ù¸é + int age = get<0>(dead_tree.front()); //Á×Àº ³ª¹«ÀÇ ³ªÀÌ + int row = get<1>(dead_tree.front()); //Á×Àº ³ª¹«ÀÇ Çà À§Ä¡ + int col = get<2>(dead_tree.front()); //Á×Àº ³ª¹«ÀÇ ¿­ À§Ä¡ + dead_tree.pop(); //Çϳª¾¿ »«´Ù + land[row][col] += (age / 2); //land¿¡ °»½Å + } +} + +void fall(int n, deque& tree, queue>& breeding_tree) { + int dr[8] = { -1, 1, 0, 0, -1, -1, 1, 1 }; //¹æÇâÇ¥½Ã »óÇÏÁÂ¿ì ´ë°¢¼± + int dc[8] = { 0, 0, -1, 1, -1, 1, -1, 1 }; //¹æÇâÇ¥½Ã + + while (!breeding_tree.empty()) { + int row = breeding_tree.front().first; //¹ø½ÄÇÒ ³ª¹«ÀÇ Çà + int col = breeding_tree.front().second; //¹ø½ÄÇÒ ³ª¹«ÀÇ ¿­ + breeding_tree.pop(); //¹ø½Ä³ª´©¿¡ Àִ°ŠÇϳª»©±â + + for (int j = 0; j < 8; j++) { //¹æÇâµ¹·Áº¸±â + int nr = row + dr[j]; //Çà + int nc = col + dc[j]; //¿­ + if (nr < 0 || nr >= n || nc < 0 || nc >= n) //¹è¿­ ¹üÀ§¸¦ ³Ñ¾î¼¹´Ù¸é Á¦¿Ü + continue; //¹Ýº¹¹® Àç½ÃÀÛ + tree.push_front({ 1, nr, nc }); //»õ·Î »ý±ä ³ª¹« + } + } +} + +void winter(int n, matrix& a, matrix& land) { + for (int i = 0; i < n; i++) //¹Ýº¹¹® µ¹¸é¼­ land »õ·Î Ãß°¡ + for (int j = 0; j < n; j++) //ÀÌÁ߹ݺ¹¹® + land[i][j] += a[i][j]; //land Ãß°¡ +} + +/** + * [¹®Á¦ ¼³¸í] - ´Ü¼ø ±¸Çö ¹®Á¦ + * º½: ÇϳªÀÇ Ä­¸¶´Ù ³ªÀ̰¡ ¾î¸° ³ª¹«ºÎÅÍ ÀÚ½ÅÀÇ ³ªÀ̸¸Å­ ¾çºÐÀ» ¸Ô°í, ³ªÀ̰¡ 1 Áõ°¡ÇÔ + * °¢ Ä­¿¡ ¾çºÐÀÌ ºÎÁ·ÇØ ÀÚ½ÅÀÇ ³ªÀ̸¸Å­ ¾çºÐÀ» ¸ø ¸Ô´Â ³ª¹«´Â Áï½Ã Á×À½ + * ¿©¸§: º½¿¡ Á×Àº ³ª¹«°¡ ¾çºÐÀ¸·Î º¯ÇÔ. Á×Àº ³ª¹«¸¶´Ù ³ªÀ̸¦ 2·Î ³ª´« °ªÀÌ ¾çºÐÀ¸·Î Ãß°¡ (¼Ò¼öÁ¡ ¹ö¸²) + * °¡À»: ³ªÀ̰¡ 5ÀÇ ¹è¼öÀÎ ³ª¹«°¡ ¹ø½Ä. ÀÎÁ¢ÇÑ 8°³ Ä­¿¡ ³ªÀ̰¡ 1ÀÎ ³ª¹«°¡ »ý±è + * °Ü¿ï: ·Îº¿(S2D2)ÀÌ ¶¥À» µ¹¾Æ´Ù´Ï¸é¼­ A[r][c]¸¸Å­ °¢ Ä­¿¡ ¾çºÐ Ãß°¡ + * + * K³âÀÌ Áö³­ ÈÄ »óµµÀÇ ¶¥¿¡ »ì¾ÆÀÖ´Â ³ª¹«ÀÇ °³¼ö + * + * [¹®Á¦ Ç®ÀÌ] + * a: ·Îº¿(S2D2)°¡ °Ü¿ï¿¡ ÁÖ´Â ¾çºÐÀÇ ¾ç + * land: ¶¥ÀÇ ¾çºÐ + * breeding_tree: ³ªÀ̰¡ 5ÀÇ ¹è¼öÀÎ Æ®¸® (¹ø½ÄÇÒ Æ®¸®) + * tree: ¶¥¿¡ ½ÉÀº ³ª¹« ³ªÀÌ, Çà, ¿­ Á¤º¸ + * -> deque ÄÁÅ×À̳ʸ¦ Ȱ¿ëÇØ ¹ø½ÄÇÑ ³ª¹«¸¦ ¾Õ¿¡ ³Ö¾îÁÖ¸é ÀÔ·Â ÈÄ¿¡¸¸ Á¤·ÄÇØ¼­ »ç¿ë °¡´É + * + * ¹®Á¦ÀÇ ¼³¸í´ë·Î °èÀýº° ¿¬»êÀ» ÁøÇà + */ + +int main() { + int n, m, k, x, y, z; + + //ÀÔ·Â + cin >> n >> m >> k; + matrix a(n, vector(n, 0)); //ÃʱâÈ­ + matrix land(n, vector(n, 5)); //óÀ½ ¾çºÐ ¸ðµç Ä­¿¡ 5 + queue> breeding_tree; //¹ø½ÄÇÒ Æ®¸® + deque tree; //treeÀüü + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + cin >> a[i][j]; //¹Ýº¹¹® µ¹¸é¼­ ÀԷ¹ޱâ + while (m--) { + cin >> x >> y >> z; //ÀÔ·Â + tree.emplace_back(z, x - 1, y - 1); //(0, 0)ºÎÅÍ ½ÃÀÛÇϵµ·Ï ±¸ÇöÇϱâÀ§ÇØ 1À» »©ÁØ À妽º¿¡ Á¢±Ù + } + + //¿¬»ê + sort(tree.begin(), tree.end()); //¾î¸° ³ªÀÌ ¼øÀ¸·Î Á¤·Ä + while (k--) { //k¸¸Å­ ¹Ýº¹µ¹±â + queue dead_tree = spring(land, tree, breeding_tree); //º½ÀÌ Áö³ª°í Á×Àº ³ª¹« + summer(dead_tree, land); //¿©¸§ + fall(n, tree, breeding_tree); //°¡À» + winter(n, a, land); //°Ü¿ï + } + + //Ãâ·Â + cout << tree.size(); //³ª¹« Å©±â Ãâ·Â +} \ No newline at end of file diff --git "a/11\354\233\224 30\354\235\274/1713.cpp" "b/11\354\233\224 30\354\235\274/1713.cpp" new file mode 100644 index 0000000..1a76969 --- /dev/null +++ "b/11\354\233\224 30\354\235\274/1713.cpp" @@ -0,0 +1,54 @@ +#include +#include +#include + +using namespace std; +typedef pair ci; //pairÀçÁ¤ÀÇ + +map::iterator delCandidate(map& candidate) { + auto del = candidate.begin(); //óÀ½ È常¦ »èÁ¦ÇÑ´Ù °¡Á¤ + int cnt = candidate.begin()->second.first; //óÀ½ Èĺ¸ÀÇ Ãßõ Ƚ¼ö + int t = candidate.begin()->second.second; //óÀ½ Èĺ¸ÀÇ °Ô½Ã ½Ã°£ + for (auto iter = ++candidate.begin(); iter != candidate.end(); iter++) { //Èĺ¸ ¹Ýº¹µ¹¸é¼­ + int cur_cnt = iter->second.first; //ÃßõȽ¼ö + int cur_t = iter->second.second; //°Ô½Ã½Ã°£ + if (cur_cnt < cnt) { //Ãßõ Ƚ¼ö°¡ °¡Àå ÀÛÀº Èĺ¸ ã±â + cnt = cur_cnt; //Ãßõ ȹ¼ö °»½Å + t = cur_t; //°Ô½Ã½Ã°£ °»½Å + del = iter; //del¿¡ ³Ö±â + } + else if (cur_cnt == cnt && cur_t < t) { //Ãßõ Ƚ¼ö°¡ °¡Àå ÀÛÀº È帰¡ ¿©·¯¸íÀ̶ó¸é, °Ô½Ã ½Ã°£ÀÌ ¿À·¡µÈ Èĺ¸ ã±â + t = cur_t; //°Ô½Ã½Ã°£ °»½Å + del = iter; //del¿¡ ³Ö±â + } + } + return del; //¹Ýȯ +} + +/** + * 1. ºñ¾îÀÖ´Â »çÁøÆ²ÀÌ ¾ø´Â °æ¿ì, °¡Àå Ãßõ¼ö°¡ ÀÛÀº Çлý Áß °Ô½Ã ½Ã°£ÀÌ ¿À·¡µÈ ÇлýÀ» »èÁ¦ + * 2. Èĺ¸ ÇлýÀ» ¹Ù·Î ã±â À§ÇØ º» Ç®ÀÌ´Â map ÄÁÅ×À̳ʸ¦ »ç¿ëÇØ ±¸Çö + * + * !ÁÖÀÇ! °Ô½Ã ½Ã°£ Á¤º¸ ÀúÀå ½Ã, È帷Π¿Ã¶ó°£ °¡Àå ù ½Ã°£À» ÀúÀå. ÀÌ¹Ì Èĺ¸¿¡ Àִµ¥ °Ô½Ã ½Ã°£ÀÌ °»½ÅµÇÁö ¾Êµµ·Ï ÁÖÀÇ. + */ + +int main() { + int n, m, input; //º¯¼ö¼±¾ð + + //ÀÔ·Â & ¿¬»ê + cin >> n >> m; + map candidate; //first: Èĺ¸ Çлý, second: {Ãßõ Ƚ¼ö, °Ô½Ã ½Ã°£} + for (int i = 0; i < m; i++) { //¹Ýº¹¹® µ¹¸é¼­ + cin >> input; //ÀԷ¹ޱâ + if (candidate.size() == n && candidate.find(input) == candidate.end()) //ºñ¾îÀÖ´Â »çÁøÆ²ÀÌ ¾ø´Â °æ¿ì + candidate.erase(delCandidate(candidate)); //Á¦°Å + + if (candidate.find(input) == candidate.end()) //ù °Ô½Ã¶ó¸é + candidate[input].second = i; //ÃʱâÈ­ + candidate[input].first++; //Ãßõ Ƚ¼ö Áõ°¡ + } + + //Ãâ·Â + for (auto iter = candidate.begin(); iter != candidate.end(); iter++) + cout << iter->first << ' '; //Á¤´ä Ãâ·Â +} \ No newline at end of file diff --git "a/11\354\233\224 30\354\235\274/1774.cpp" "b/11\354\233\224 30\354\235\274/1774.cpp" new file mode 100644 index 0000000..b38ebd1 --- /dev/null +++ "b/11\354\233\224 30\354\235\274/1774.cpp" @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include + +using namespace std; +typedef pair ci; //pair ci·Î Àç¼±¾ð +typedef tuple tp; //tuple ¼±¾ð + +vector parent; //parent ¹è¿­ + +//Find ¿¬»ê +int findParent(int node) { + if (parent[node] < 0) //°ªÀÌ À½¼ö¸é ·çÆ® Á¤Á¡ + return node; //node ¹Ýȯ + return parent[node] = findParent(parent[node]); //±×·¡ÇÁ ¾ÐÃàÇÏ¸ç ·çÆ® Á¤Á¡ ã±â +} + +//Union ¿¬»ê +bool unionInput(int x, int y) { + int xp = findParent(x); //ºÎ¸ðã±â + int yp = findParent(y); //ºÎ¸ðã±â + + if (xp == yp) //°°Àº ÁýÇÕ¿¡ ÀÖ´Ù¸é À¯´Ï¿Â ÇÒ ¼ö ¾øÀ½ + return false; //¸øÇϹǷΠfalse¹Ýȯ + if (parent[xp] < parent[yp]) { //»õ·Î¿î ·çÆ® xp + parent[xp] += parent[yp]; //°¡Àå ³ëµå °³¼ö ¸¹Àº°Ô ºÎ¸ðÇÒ¼öÀÖ°Ô Ç¥½Ã + parent[yp] = xp; //xp¸¦ ºÎ¸ð·Î + } + else { //»õ·Î¿î ·çÆ® yp + parent[yp] += parent[xp]; //°¡Àå ³ëµå °³¼ö ¸¹Àº°Ô ºÎ¸ðÇÒ¼öÀÖ°Ô Ç¥½Ã + parent[xp] = yp; //yp¸¦ ºÎ¸ð·Î + } + return true; //true¹Ýȯ +} + +double kruskal(int v, priority_queue, greater<>>& pq) { + int cnt = 0; //ÃʱâÈ­ + double sum = 0; //ÃʱâÈ­ + + while (cnt < v - 1) { //»ç¿ëÇÑ °£¼±ÀÇ ¼ö°¡ v-1º¸´Ù ÀûÀ» µ¿¾È + double cost = get<0>(pq.top()); //cost¿¡ ³Ö¾îÁÖ°í + int x = get<1>(pq.top()); //°¢°¢ x + int y = get<2>(pq.top());//y¿¡ ³Ö¾îÁØ´Ù/ + + pq.pop();//±×ÈÄ Á¦°Å + if (unionInput(x, y)) { //À¯´Ï¿Â Çß´Ù¸é + cnt++; //»ç¿ëµÈ °£¼± Áõ°¡ + sum += cost; //°£¼±ÀÇ °¡ÁßÄ¡ + } + } + return sum; //°¡ÁßÄ¡ ¹Ýȯ +} + +/** + * 4386¹ø : º°ÀÚ¸® ¸¸µé±âÀÇ ÀÀ¿ë ¹®Á¦ + * ÀÌ¹Ì ¿¬°áµÈ Á¤Á¡µéÀÌ Á¸ÀçÇÑ´Ù´Â °ÍÀ» Á¦¿ÜÇϰí´Â 4386¹ø°ú µ¿ÀÏ + * + * 1. ÀÓÀÇÀÇ µÎ º°¿¡ ´ëÇÑ °Å¸®(°£¼±) ¸ðµÎ ±¸Çϱâ + * 2. ÀÌ¹Ì Á¸ÀçÇÏ´Â Åë·Îµé Ç¥½Ã + * !ÁÖÀÇ! Åë·ÎÀÇ °³¼ö°¡ m°³¶ó¸é v-m-1°³ÀÇ °£¼±¸¸ ´õ Ãß°¡ÇÏ¸é µÉ±î? + * ÀÌ¹Ì ¿¬°áµÈ Åë·Îµéµµ »çÀÌŬÀ» ÀÌ·ê ¼ö Àֱ⠶§¹®¿¡ À¯´Ï¿Â ¿¬»êÀ» ÇÏ¸ç »çÀÌŬ ¾øÀÌ ¿¬°áµÈ °£¼±¸¸ ¼¼±â + * 3. ÀÌ¹Ì ¿¬°áµÈ Åë·ÎÀÇ ¼ö¸¦ k°³¶ó°í Çϸé v-k-1°³ÀÇ °£¼±À» Ãß°¡·Î ¼±Åà + */ +int main() { + //ÃʱâÈ­ + int n, m, a, b, v = 0; + priority_queue, greater<>> pq; + + //ÀÔ·Â + cin >> n >> m; + parent.assign(n + 1, -1); //ÀçÃʱâÈ­ + vector stars(n + 1); //ÃʱâÈ­ + for (int i = 1; i <= n; i++) //ÀÔ·Â + cin >> stars[i].first >> stars[i].second; + + + //¿¬»ê + //ÀÓÀÇÀÇ µÎ º°¿¡ ´ëÇÑ °Å¸®(°£¼±) ¸ðµÎ ±¸Çϱâ + for (int i = 1; i <= n - 1; i++) { //¹Ýº¹¹® µ¹¸é¼­ + for (int j = i + 1; j <= n; j++) { + double xd = stars[i].first - stars[j].first; //°£¼± ±¸Çؼ­ ³Ö¾îÁØ´Ù + double yd = stars[i].second - stars[j].second; //°£¼± ±¸Çϱâ + pq.push({ sqrt(xd * xd + yd * yd), i, j }); //±¸ÇÑ °£¼±À» pq¿¡ ³Ö¾îÁØ´Ù + } + } + while (m--) { //m¸¸Å­ ¹Ýº¹¹® µ¹¸é¼­ + cin >> a >> b; //ÀÔ·Â + if (unionInput(a, b)) //ÀÌ¹Ì ¿¬°áµÈ Åë·Î + v++; + } + + //¿¬»ê & Ãâ·Â + cout << fixed; //¼Ò¼öÁ¡ °íÁ¤ + cout.precision(2); //2ÀÚ¸®±îÁö + cout << kruskal(n - v, pq); //Ãâ·Â +} \ No newline at end of file