답안 #670789

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670789 2022-12-10T13:56:05 Z Do_you_copy Designated Cities (JOI19_designated_cities) C++17
23 / 100
4 ms 5332 KB
/*
    I find it wholesome to be alone the greater part of the time.
    To be in company, even with the best, is soon wearisome and dissipating.
    I love to be alone.
    I never found the companion that was so companionable as solitude.
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define int long long
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 1;
//const int Mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n;

struct TEdge{
    int v, w, rw;
};

vector <TEdge> adj[maxN];
ll sum = 0;
ll ans[maxN];
int deg[maxN];
void dfs(int u, int p){
    for (auto &[v, w, rw]: adj[u]){
        if (v == p) continue;
        sum += w;
        dfs(v, u);
    }
}

void dfs1(int u, int p){
    ans[1] = min(ans[1], sum);
    for (auto &[v, w, rw]: adj[u]){
        if (v == p) continue;
        sum -= w - rw;
        dfs1(v, u);
        sum += w - rw;
    }
}

bool visited[maxN];
pii find_nxt(int u){
    for (auto &[v, w, rw]: adj[u]){
        if (!visited[v]){
            return {rw, v};
        }
    }
}

void Init(){
    cin >> n;
    for (int i = 1; i < n; ++i){
        int u, v, w, rw;
        cin >> u >> v >> w >> rw;
        adj[u].pb({v, w, rw});
        adj[v].pb({u, rw, w});
        ++deg[u];
        ++deg[v];
    }
    ans[1] = inf;
    dfs(1, 0);
    dfs1(1, 0);
    priority_queue <pii, vector <pii>, greater <pii>> PQ;
    for (int i = 1; i <= n; ++i){
        if (deg[i] == 1){
            PQ.push({find_nxt(i).fi, i});
        }
    }
    while (PQ.size() > 2){
        auto r = PQ.top(); PQ.pop();
        visited[r.se] = 1;
        auto p = find_nxt(r.se);
        if (--deg[p.se] == 1){
            PQ.push(pii{find_nxt(p.se).fi + r.fi, p.se});
        }
        else{
            ans[PQ.size()] = ans[PQ.size() + 1] + r.fi;
        }
    }
    int q; cin >> q;
    while (q--){
        int x; cin >> x; cout << ans[x] << "\n";
    }
}

#define debug
#define taskname "test"
signed main(){
    faster
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    int tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
        timeout.close();
        #ifndef debug
        cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
        #endif // debug
    }
}

Compilation message

designated_cities.cpp: In function 'pii find_nxt(long long int)':
designated_cities.cpp:56:1: warning: control reaches end of non-void function [-Wreturn-type]
   56 | }
      | ^
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2680 KB Output is correct
7 Correct 2 ms 2680 KB Output is correct
8 Correct 2 ms 2676 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Runtime error 4 ms 5204 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Runtime error 4 ms 5332 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2680 KB Output is correct
7 Correct 2 ms 2680 KB Output is correct
8 Correct 2 ms 2676 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 3 ms 2820 KB Output is correct
16 Correct 3 ms 2948 KB Output is correct
17 Correct 3 ms 2900 KB Output is correct
18 Correct 3 ms 2900 KB Output is correct
19 Correct 3 ms 2908 KB Output is correct
20 Correct 3 ms 2956 KB Output is correct
21 Correct 3 ms 2920 KB Output is correct
22 Correct 3 ms 2900 KB Output is correct
23 Correct 3 ms 2900 KB Output is correct
24 Correct 3 ms 2900 KB Output is correct
25 Correct 3 ms 2948 KB Output is correct
26 Correct 3 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Runtime error 4 ms 5204 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2680 KB Output is correct
7 Correct 2 ms 2680 KB Output is correct
8 Correct 2 ms 2676 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Runtime error 4 ms 5204 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -