답안 #417908

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
417908 2021-06-04T13:39:39 Z 반딧불(#7603) Rooted MST (innopolis2021_final_E) C++17
10 / 100
3277 ms 45484 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Edge{
    int x, y; ll z;
    Edge(){}
    Edge(int x, int y, ll z): x(x), y(y), z(z){}

    bool operator<(const Edge &r)const{
        return z < r.z;
    }
};

struct DisjointSet{
    int n;
    int par[300002];
    int sz[300002];

    void init(int _n){
        n = _n;
        for(int i=0; i<=n; i++){
            par[i] = i;
            sz[i] = 1;
        }
    }

    int find(int x){
        if(x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    void merge(int x, int y){
        x = find(x), y = find(y);
        if(x==y) return;
        if(sz[x] < sz[y]) swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
    }
};
DisjointSet dsu;

int n, m, q;
ll arr[300002];
Edge link[300002];

ll dflt;
ll lim;

ll printMST(){
    vector<Edge> vec;
    for(int i=1; i<=m; i++) vec.push_back(link[i]);
    for(int i=1; i<=n; i++){
        vec.push_back(Edge(0, i, arr[i]));
    }
    sort(vec.begin(), vec.end());

    dsu.init(n);

    ll ans = 0;
    for(auto tmp: vec){
        int x = tmp.x, y = tmp.y;
        if(dsu.find(x) == dsu.find(y)) continue;
        dsu.merge(x, y);
        ans += tmp.z;
    }
    return ans;
}

int main(){
    scanf("%d %d", &n, &m);
    for(int i=1; i<=n; i++){
        scanf("%lld", &arr[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d %d %lld", &link[i].x, &link[i].y, &link[i].z);
    }

    arr[1] = 0;
    dflt = printMST();

    ll MIN = 0, MAX = 1e9 + 1, ANS = 0;
    while(MIN <= MAX){
        ll MID = (MIN + MAX) / 2;
        arr[1] = MID;
        if(printMST() == dflt + MID){
            ANS = MID;
            MIN = MID + 1;
        }
        else MAX = MID - 1;
    }
    lim = ANS;

    scanf("%d", &q);
    while(q--){
        ll x;
        scanf("%lld %lld", &x, &x);
        printf("%lld\n", dflt + min(lim, x));
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:75:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         scanf("%lld", &arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         scanf("%d %d %lld", &link[i].x, &link[i].y, &link[i].z);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
Main.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         scanf("%lld %lld", &x, &x);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1646 ms 35536 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1333 ms 18060 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1580 ms 18052 KB Output is correct
2 Correct 1591 ms 18172 KB Output is correct
3 Correct 1567 ms 18236 KB Output is correct
4 Correct 1356 ms 18068 KB Output is correct
5 Correct 862 ms 10712 KB Output is correct
6 Correct 293 ms 4788 KB Output is correct
7 Correct 3189 ms 35604 KB Output is correct
8 Correct 3064 ms 45484 KB Output is correct
9 Correct 3277 ms 45372 KB Output is correct
10 Correct 3223 ms 45364 KB Output is correct
11 Correct 3217 ms 45356 KB Output is correct
12 Correct 3131 ms 45376 KB Output is correct
13 Correct 3179 ms 45348 KB Output is correct
14 Correct 3101 ms 45380 KB Output is correct
15 Correct 3223 ms 45396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1580 ms 18052 KB Output is correct
2 Correct 1591 ms 18172 KB Output is correct
3 Correct 1567 ms 18236 KB Output is correct
4 Correct 1356 ms 18068 KB Output is correct
5 Correct 862 ms 10712 KB Output is correct
6 Correct 293 ms 4788 KB Output is correct
7 Correct 3189 ms 35604 KB Output is correct
8 Correct 3064 ms 45484 KB Output is correct
9 Correct 3277 ms 45372 KB Output is correct
10 Correct 3223 ms 45364 KB Output is correct
11 Correct 3217 ms 45356 KB Output is correct
12 Correct 3131 ms 45376 KB Output is correct
13 Correct 3179 ms 45348 KB Output is correct
14 Correct 3101 ms 45380 KB Output is correct
15 Correct 3223 ms 45396 KB Output is correct
16 Incorrect 1648 ms 21000 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3136 ms 35628 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -