디시인사이드 갤러리

마이너 갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

백준 17726 Inheritance

꽃지갤로그로 이동합니다. 2023.01.04 02:31:38
조회 59 추천 0 댓글 0
														
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#define _DE321BUG
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define all(x) (x).begin(),(x).end() 
typedef long long ll;
typedef long double ld;
typedef pair<intint> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
const long double pi = acos(-1.0);
const int INF = 1987654321;
const ll LLINF = 4e18;
const double eps = 1e-9;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//
 
int parent[10002][1002]; // kid/vertice
 
int find(int kid, int n){
    if(parent[kid][n] == -1return n;
    return parent[kid][n] = find(kid, parent[kid][n]);
}
 
void merge(int kid, int u, int v){
    u = find(kid, u);
    v = find(kid, v);
    if(u == v) return;
    parent[kid][v] = u;
}
 
struct Edge{
    int idx, u, v, weight;
    // bool operator<(const Edge &other) const{
    //     return weight < other.weight;
    // }
    bool operator<(const Edge &other) const{
        return weight > other.weight;
    }
};
 
vector<Edge> edges;
 
int main(){
#ifdef _DEBUG
    freopen("input.txt""r", stdin);
    freopen("output.txt""w", stdout);
#endif  
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    memset(parent, -1sizeof(parent));
    
    int N, M, K; cin >> N >> M >> K;
    // if(K > 10){
    //     return 0;
    // }
    for(int i = 1; i <= M; i++){
        int a, b, c; cin >> a >> b >> c;
        edges.push_back({i, a, b, c});
    }
    
    sort(edges.begin(), edges.end());
    // for(auto v : edges){
    //     cout << v.idx << " " << v.u << " " << v.v << " " << v.weight << endl;
    // }
    int ans[300004];
    memset(ans, 0sizeof(ans));
    for(auto edge : edges){
        // for(int i = 1; i <= K; i++){    
        //     if(find(i, edge.u) == find(i, edge.v)) continue;
        //     merge(i, find(i, edge.u), find(i, edge.v));
        //     ans[edge.idx] = i;
        //     break;
        // }
        bool flag = false;
        int lo = 0;
        int hi = K;
        while(lo + 1 < hi){
            int mid = (lo + hi)/2;
            if(find(mid, edge.u) != find(mid, edge.v)){
                hi = mid;
                flag = true;
            }
            else{
                lo = mid;
            }
        }
        // cout << edge.idx << " " << hi << " " << flag << endl;
        if(find(hi, edge.u) != find(hi, edge.v)){
            merge(hi, find(hi, edge.u), find(hi, edge.v));
            ans[edge.idx] = hi;
        }
        else{
            ans[edge.idx] = 0;
        }
        // if(flag) ans[edge.idx] = K-hi;
        // if(flag) ans[edge.idx] = hi;
        // else ans[edge.idx] = 0;
    }
    for(int i = 1; i <= M; i++cout << ans[i] << endl;
    
    return 0;
}   
cs

https://www.acmicpc.net/problem/17726
 




문제 설명:

가중치가 C인 양방향 A<->B 그래프. K명이 있을때 1번부터 K번까지 가중치의 합이 가장 크도록 간선들을 선택함. 이 때 선택한 간선들이 사이클을 이루면 안 됨. 1 ~ K번의 사람이 각각 최대로 가져간 간선의 가중치의 합은?

서브태스크 1 :

10명밖에 없기 때문에 모든 간선에 대해 1 ~ 10번까지 크루스칼 알고리즘을 변형해서 최대한 앞 번호한테 주면 됨.

서브태스크 2 :

10000명이나 존재하기 때문에 서브태스크 1의 과정을 O(K) -> O(logK)로 줄여야 됨. 이분탐색을 이용해서 주면 되는데, 임의의 Q가 간선을 가져갈 수 없으면 1 ~ Q-1 모두 가져갈 수 없다는게 그리디하게 증명이 되기 때문에 가능함.



추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 2025년 가장 기억에 남는 인터넷 이슈는? 운영자 25/12/22 - -
333 3355. Zero Array Transformation I 흑화뉴비갤로그로 이동합니다. 05.20 53 0
332 2918. Minimum Equal Sum of Two Arrays Af 흑화뉴비갤로그로 이동합니다. 05.10 21 0
331 2033. Minimum Operations to Make a Uni-V 흑화뉴비갤로그로 이동합니다. 03.28 45 0
330 2401. Longest Nice Subarray 흑화뉴비갤로그로 이동합니다. 03.19 40 0
329 1079. Letter Tile Possibilities 흑화뉴비갤로그로 이동합니다. 02.18 45 0
328 1718. Construct the Lexicographically La 흑화뉴비갤로그로 이동합니다. 02.16 29 0
327 1910. Remove All Occurrences of a Substr 흑화뉴비갤로그로 이동합니다. 02.11 31 0
326 2381. Shifting Letters II 흑화뉴비갤로그로 이동합니다. 01.05 43 0
323 2471. Minimum Number of Operations to So 흑화뉴비갤로그로 이동합니다. 24.12.25 31 0
322 773. Sliding Puzzle 흑화뉴비갤로그로 이동합니다. 24.11.27 30 0
321 3243. Shortest Distance After Road Addit 흑화뉴비갤로그로 이동합니다. 24.11.27 23 0
320 1233. Remove Sub-Folders from the Filesy 흑화뉴비갤로그로 이동합니다. 24.10.29 30 0
319 2684. Maximum Number of Moves in a Grid 흑화뉴비갤로그로 이동합니다. 24.10.29 31 0
318 2501. Longest Square Streak in an Array 흑화뉴비갤로그로 이동합니다. 24.10.29 43 0
317 3043. Find the Length of the Longest Com 흑화뉴비갤로그로 이동합니다. 24.09.24 38 0
316 1514. Path with Maximum Probability 흑화뉴비갤로그로 이동합니다. 24.08.27 38 0
315 264. Ugly Number II 흑화뉴비갤로그로 이동합니다. 24.08.19 35 0
314 2285. Maximum Total Importance of Roads 흑화뉴비갤로그로 이동합니다. 24.06.28 48 0
313 1552. Magnetic Force Between Two Balls 흑화뉴비갤로그로 이동합니다. 24.06.20 38 0
312 1482. Minimum Number of Days to Make m B 흑화뉴비갤로그로 이동합니다. 24.06.19 41 0
311 523. Continuous Subarray Sum 흑화뉴비갤로그로 이동합니다. 24.06.09 41 0
310 리트코드 갤러리 5월 이벤트 우승자 공지 [1] 진척갤로그로 이동합니다. 24.06.08 65 0
309 1208. Get Equal Substrings Within Budget 흑화뉴비갤로그로 이동합니다. 24.05.28 41 0
308 1608. Special Array With X Elements Grea 흑화뉴비갤로그로 이동합니다. 24.05.28 33 0
307 552. Student Attendance Record II 흑화뉴비갤로그로 이동합니다. 24.05.28 32 0
306 2597. The Number of Beautiful Subsets [1] 흑화뉴비갤로그로 이동합니다. 24.05.24 57 0
305 78. Subsets 흑화뉴비갤로그로 이동합니다. 24.05.21 32 0
304 1863. Sum of All Subset XOR Totals 흑화뉴비갤로그로 이동합니다. 24.05.21 36 0
302 1325. Delete Leaves With a Given Value 흑화뉴비갤로그로 이동합니다. 24.05.18 37 0
301 2331. Evaluate Boolean Binary Tree 흑화뉴비갤로그로 이동합니다. 24.05.18 28 0
298 3075. Maximize Happiness of Selected Chi 흑화뉴비갤로그로 이동합니다. 24.05.12 25 0
297 786. K-th Smallest Prime Fraction 흑화뉴비갤로그로 이동합니다. 24.05.12 29 0
296 506. Relative Ranks 흑화뉴비갤로그로 이동합니다. 24.05.08 38 0
295 2816. Double a Number Represented as a L 흑화뉴비갤로그로 이동합니다. 24.05.08 25 0
294 2487. Remove Nodes From Linked List 흑화뉴비갤로그로 이동합니다. 24.05.06 26 0
293 79. Word Search o오어o(14.37) 24.05.04 29 0
292 45. Jump Game II o오어o(14.37) 24.05.04 35 0
291 881. Boats to Save People o오어o(14.37) 24.05.04 29 0
290 2441. Largest Positive Integer That Exis 흑화뉴비갤로그로 이동합니다. 24.05.02 28 0
289 2997. Minimum Number of Operations to Ma 흑화뉴비갤로그로 이동합니다. 24.05.01 25 0
288 2000. Reverse Prefix of Word 흑화뉴비갤로그로 이동합니다. 24.05.01 34 0
287 리트코드 갤러리 5월 이벤트 공지 [3] 진척갤로그로 이동합니다. 24.04.28 97 0
282 백준 1260. DFS와 BFS 차근차근춘식이(211.234) 24.04.23 47 0
277 206. Reverse Linked List 흑화뉴비갤로그로 이동합니다. 24.03.21 48 0
276 1669. Merge In Between Linked Lists 흑화뉴비갤로그로 이동합니다. 24.03.20 30 0
275 57. Insert Interval 흑화뉴비갤로그로 이동합니다. 24.03.18 31 0
274 2485. Find the Pivot Integer 흑화뉴비갤로그로 이동합니다. 24.03.13 25 0
273 1750. Minimum Length of String After Del 흑화뉴비갤로그로 이동합니다. 24.03.06 26 0
272 451. Sort Characters By Frequency 흑화뉴비갤로그로 이동합니다. 24.02.07 40 0
271 49. Group Anagrams 흑화뉴비갤로그로 이동합니다. 24.02.06 35 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2