Submission #207563

# Submission time Handle Problem Language Result Execution time Memory
207563 2020-03-08T04:01:10 Z quocnguyen1012 Designated Cities (JOI19_designated_cities) C++14
100 / 100
428 ms 35064 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long ll;

const int maxn = 2e5 + 5;

class Edge
{
public:
  int v, c, d;
  Edge(int v, int c, int d):
    v(v), c(c), d(d) {}
};
vector<Edge> adj[maxn];
int N, M, deg[maxn];
ll res[maxn], sum[maxn];

void prepare(int u, int p = -1)
{
  for(auto v : adj[u]) if(v.v != p){
    prepare(v.v, u);
    sum[u] += v.c + sum[v.v];
  }
}

void down(int u, int p, ll s)
{
  res[1] = min(res[1], s);
  for(auto v : adj[u]) if(v.v != p){
    down(v.v, u, s - v.c + v.d);
  }
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  if(fopen("A.INP", "r")){
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N;
  for(int i = 1; i < N; ++i){
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    adj[a].eb(b, c, d); adj[b].eb(a, d, c);
    ++deg[a]; ++deg[b];
  }
  res[1] = 1e18;
  prepare(1); down(1, -1, sum[1]);
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
  for(int i = 1; i <= N; ++i){
    if(adj[i].size() == 1) pq.push(mp(adj[i][0].d, i));
  }
  while(pq.size() > 2){
    auto now = pq.top(); pq.pop();
    int nxt = -1, cnt = 0;
    for(auto v : adj[now.se]){
      if(deg[v.v] > 1){
        deg[v.v]--;
        if(deg[v.v] == 1) nxt = v.v, ++cnt;
        else res[pq.size()] = res[pq.size() + 1] + now.fi;
      }
    }
    assert(cnt <= 1);
    if(nxt != -1) for(auto v : adj[nxt]) if(deg[v.v] > 1)
        pq.push(mp(now.fi + v.d, nxt));
  }
  int q; cin >> q;
  while(q--){
    int x; cin >> x;
    cout << res[x] << '\n';
  }
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:46:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 4988 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 7 ms 4984 KB Output is correct
5 Correct 7 ms 4984 KB Output is correct
6 Correct 7 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 7 ms 5016 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 4984 KB Output is correct
2 Correct 380 ms 24244 KB Output is correct
3 Correct 300 ms 32760 KB Output is correct
4 Correct 349 ms 22768 KB Output is correct
5 Correct 357 ms 24608 KB Output is correct
6 Correct 359 ms 25960 KB Output is correct
7 Correct 328 ms 25940 KB Output is correct
8 Correct 303 ms 33780 KB Output is correct
9 Correct 251 ms 27644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 357 ms 24180 KB Output is correct
3 Correct 286 ms 34936 KB Output is correct
4 Correct 354 ms 22896 KB Output is correct
5 Correct 353 ms 24608 KB Output is correct
6 Correct 363 ms 26272 KB Output is correct
7 Correct 281 ms 28500 KB Output is correct
8 Correct 324 ms 31208 KB Output is correct
9 Correct 257 ms 27284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 4988 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 7 ms 4984 KB Output is correct
5 Correct 7 ms 4984 KB Output is correct
6 Correct 7 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 7 ms 5016 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 7 ms 5112 KB Output is correct
13 Correct 9 ms 5244 KB Output is correct
14 Correct 9 ms 5236 KB Output is correct
15 Correct 10 ms 5368 KB Output is correct
16 Correct 10 ms 5240 KB Output is correct
17 Correct 9 ms 5240 KB Output is correct
18 Correct 10 ms 5240 KB Output is correct
19 Correct 9 ms 5240 KB Output is correct
20 Correct 9 ms 5240 KB Output is correct
21 Correct 10 ms 5240 KB Output is correct
22 Correct 10 ms 5368 KB Output is correct
23 Correct 10 ms 5368 KB Output is correct
24 Correct 10 ms 5368 KB Output is correct
25 Correct 10 ms 5368 KB Output is correct
26 Correct 9 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 4984 KB Output is correct
2 Correct 380 ms 24244 KB Output is correct
3 Correct 300 ms 32760 KB Output is correct
4 Correct 349 ms 22768 KB Output is correct
5 Correct 357 ms 24608 KB Output is correct
6 Correct 359 ms 25960 KB Output is correct
7 Correct 328 ms 25940 KB Output is correct
8 Correct 303 ms 33780 KB Output is correct
9 Correct 251 ms 27644 KB Output is correct
10 Correct 7 ms 5112 KB Output is correct
11 Correct 357 ms 24180 KB Output is correct
12 Correct 286 ms 34936 KB Output is correct
13 Correct 354 ms 22896 KB Output is correct
14 Correct 353 ms 24608 KB Output is correct
15 Correct 363 ms 26272 KB Output is correct
16 Correct 281 ms 28500 KB Output is correct
17 Correct 324 ms 31208 KB Output is correct
18 Correct 257 ms 27284 KB Output is correct
19 Correct 8 ms 4984 KB Output is correct
20 Correct 373 ms 24304 KB Output is correct
21 Correct 312 ms 35064 KB Output is correct
22 Correct 347 ms 22896 KB Output is correct
23 Correct 350 ms 24300 KB Output is correct
24 Correct 353 ms 23272 KB Output is correct
25 Correct 351 ms 24172 KB Output is correct
26 Correct 345 ms 23276 KB Output is correct
27 Correct 353 ms 24360 KB Output is correct
28 Correct 334 ms 26008 KB Output is correct
29 Correct 362 ms 24168 KB Output is correct
30 Correct 343 ms 24108 KB Output is correct
31 Correct 329 ms 27048 KB Output is correct
32 Correct 341 ms 31600 KB Output is correct
33 Correct 250 ms 27676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 4988 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 7 ms 4984 KB Output is correct
5 Correct 7 ms 4984 KB Output is correct
6 Correct 7 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 7 ms 5016 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 9 ms 4984 KB Output is correct
13 Correct 380 ms 24244 KB Output is correct
14 Correct 300 ms 32760 KB Output is correct
15 Correct 349 ms 22768 KB Output is correct
16 Correct 357 ms 24608 KB Output is correct
17 Correct 359 ms 25960 KB Output is correct
18 Correct 328 ms 25940 KB Output is correct
19 Correct 303 ms 33780 KB Output is correct
20 Correct 251 ms 27644 KB Output is correct
21 Correct 7 ms 5112 KB Output is correct
22 Correct 357 ms 24180 KB Output is correct
23 Correct 286 ms 34936 KB Output is correct
24 Correct 354 ms 22896 KB Output is correct
25 Correct 353 ms 24608 KB Output is correct
26 Correct 363 ms 26272 KB Output is correct
27 Correct 281 ms 28500 KB Output is correct
28 Correct 324 ms 31208 KB Output is correct
29 Correct 257 ms 27284 KB Output is correct
30 Correct 7 ms 5112 KB Output is correct
31 Correct 9 ms 5244 KB Output is correct
32 Correct 9 ms 5236 KB Output is correct
33 Correct 10 ms 5368 KB Output is correct
34 Correct 10 ms 5240 KB Output is correct
35 Correct 9 ms 5240 KB Output is correct
36 Correct 10 ms 5240 KB Output is correct
37 Correct 9 ms 5240 KB Output is correct
38 Correct 9 ms 5240 KB Output is correct
39 Correct 10 ms 5240 KB Output is correct
40 Correct 10 ms 5368 KB Output is correct
41 Correct 10 ms 5368 KB Output is correct
42 Correct 10 ms 5368 KB Output is correct
43 Correct 10 ms 5368 KB Output is correct
44 Correct 9 ms 5368 KB Output is correct
45 Correct 8 ms 4984 KB Output is correct
46 Correct 373 ms 24304 KB Output is correct
47 Correct 312 ms 35064 KB Output is correct
48 Correct 347 ms 22896 KB Output is correct
49 Correct 350 ms 24300 KB Output is correct
50 Correct 353 ms 23272 KB Output is correct
51 Correct 351 ms 24172 KB Output is correct
52 Correct 345 ms 23276 KB Output is correct
53 Correct 353 ms 24360 KB Output is correct
54 Correct 334 ms 26008 KB Output is correct
55 Correct 362 ms 24168 KB Output is correct
56 Correct 343 ms 24108 KB Output is correct
57 Correct 329 ms 27048 KB Output is correct
58 Correct 341 ms 31600 KB Output is correct
59 Correct 250 ms 27676 KB Output is correct
60 Correct 8 ms 4984 KB Output is correct
61 Correct 428 ms 26852 KB Output is correct
62 Correct 352 ms 34680 KB Output is correct
63 Correct 404 ms 25452 KB Output is correct
64 Correct 415 ms 26724 KB Output is correct
65 Correct 416 ms 25316 KB Output is correct
66 Correct 401 ms 26728 KB Output is correct
67 Correct 410 ms 25448 KB Output is correct
68 Correct 416 ms 27048 KB Output is correct
69 Correct 406 ms 28192 KB Output is correct
70 Correct 421 ms 26084 KB Output is correct
71 Correct 401 ms 26868 KB Output is correct
72 Correct 365 ms 29340 KB Output is correct
73 Correct 368 ms 32616 KB Output is correct
74 Correct 307 ms 31956 KB Output is correct