답안 #1021405

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1021405 2024-07-12T17:35:23 Z Nika533 Designated Cities (JOI19_designated_cities) C++14
100 / 100
554 ms 60756 KB
#pragma gcc diagnostic "-std=c++1z"
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define f first
#define s second
#define MOD 1000000007
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
using namespace std;

const int N=2e5+5;
int n,m,T,k,q,tot,sum,val[N],ans[N];
pii mx={0,0};
vector<pair<int,pii>> g[N];

pii ml[N];
int parent[N];

void dfs_build(int x, int p) {
     ml[x]={0,x}; parent[x]=p;
     for (auto A:g[x]) {
          int y=A.f,w1=A.s.f,w2=A.s.s;
          if (y==p) continue;
          dfs_build(y,x); ml[x]=max(ml[x],{ml[y].f+w1,ml[y].s});
     }
}

void solve(int root) {
     // cout<<"ROOT "<<root<<endl;
     dfs_build(root,0);
     vector<bool> used(n+1,0); used[0]=1;
     priority_queue<pii> q; q.push(ml[root]);
     int cur_ans=tot-val[root],cur_ind=2;
     while (!q.empty()) {
          pii AA=q.top(); q.pop();
          int v=AA.s,num=AA.f;
          cur_ans-=num;
          while (!used[v]) {
               used[v]=1;
               for (auto A:g[v]) {
                    int u=A.f,w1=A.s.f,w2=A.s.s; 
                    if (u!=parent[v] && !used[u]) q.push({ml[u].f+w1,ml[u].s});
               }
               v=parent[v];
          }
          ans[cur_ind]=min(ans[cur_ind],cur_ans); cur_ind++;
     }
     for (int i=cur_ind; i<=n; i++) ans[i]=0;
}

void dfs0(int x, int p) {
     for (auto A:g[x]) {
          int y=A.f,w1=A.s.f,w2=A.s.s;
          if (y==p) continue;
          sum+=w2; dfs0(y,x);
     }
}
void dfs1(int x, int p) {
     val[x]=sum; 
     for (auto A:g[x]) {
          int y=A.f,w1=A.s.f,w2=A.s.s;
          if (y==p) continue;
          sum+=w1-w2; dfs1(y,x); sum-=w1-w2; 
     }
}
void dfs(int x, int p, int dist) {
     mx=max(mx,{dist+val[x],x});
     for (auto A:g[x]) {
          int y=A.f,w1=A.s.f,w2=A.s.s;
          if (y==p) continue;
          dfs(y,x,dist+w1+w2);
     }
}

void test_case() {
     cin>>n;
     for (int i=1; i<=n-1; i++) {
          int a,b,c,d; cin>>a>>b>>c>>d;
          g[a].pb({b,{c,d}}); g[b].pb({a,{d,c}});
          tot+=c+d;
     }
     cin>>q;
     int query[q+1];
     for (int i=1; i<=q; i++) cin>>query[i];

     dfs0(1,0); dfs1(1,1);
     dfs(1,0,val[1]); int r1=mx.s; dfs(r1,0,val[r1]); int r2=mx.s;

     ans[1]=1e18;
     for (int i=1; i<=n; i++) ans[1]=min(ans[1],tot-val[i]);
     for (int i=2; i<=n; i++) ans[i]=1e18;

     solve(r1); solve(r2);

     for (int i=1; i<=q; i++) cout<<ans[query[i]]<<endl;
}
main () {
	ios :: sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	T=1; 
	while (T--) test_case();
}

Compilation message

designated_cities.cpp:1: warning: ignoring '#pragma gcc diagnostic' [-Wunknown-pragmas]
    1 | #pragma gcc diagnostic "-std=c++1z"
      | 
designated_cities.cpp: In function 'void dfs_build(long long int, long long int)':
designated_cities.cpp:24:30: warning: unused variable 'w2' [-Wunused-variable]
   24 |           int y=A.f,w1=A.s.f,w2=A.s.s;
      |                              ^~
designated_cities.cpp: In function 'void solve(long long int)':
designated_cities.cpp:43:40: warning: unused variable 'w2' [-Wunused-variable]
   43 |                     int u=A.f,w1=A.s.f,w2=A.s.s;
      |                                        ^~
designated_cities.cpp: In function 'void dfs0(long long int, long long int)':
designated_cities.cpp:55:21: warning: unused variable 'w1' [-Wunused-variable]
   55 |           int y=A.f,w1=A.s.f,w2=A.s.s;
      |                     ^~
designated_cities.cpp: At global scope:
designated_cities.cpp:99:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   99 | main () {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 5168 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 210 ms 34388 KB Output is correct
3 Correct 304 ms 57728 KB Output is correct
4 Correct 199 ms 31056 KB Output is correct
5 Correct 193 ms 34192 KB Output is correct
6 Correct 275 ms 37500 KB Output is correct
7 Correct 186 ms 35064 KB Output is correct
8 Correct 261 ms 56660 KB Output is correct
9 Correct 168 ms 37604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 233 ms 34388 KB Output is correct
3 Correct 330 ms 57500 KB Output is correct
4 Correct 184 ms 31168 KB Output is correct
5 Correct 192 ms 34328 KB Output is correct
6 Correct 226 ms 38400 KB Output is correct
7 Correct 208 ms 37364 KB Output is correct
8 Correct 257 ms 50292 KB Output is correct
9 Correct 185 ms 37308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 5168 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4952 KB Output is correct
13 Correct 6 ms 5468 KB Output is correct
14 Correct 5 ms 5724 KB Output is correct
15 Correct 5 ms 5472 KB Output is correct
16 Correct 6 ms 5464 KB Output is correct
17 Correct 6 ms 5468 KB Output is correct
18 Correct 6 ms 5468 KB Output is correct
19 Correct 6 ms 5468 KB Output is correct
20 Correct 5 ms 5272 KB Output is correct
21 Correct 6 ms 5468 KB Output is correct
22 Correct 5 ms 5324 KB Output is correct
23 Correct 6 ms 5264 KB Output is correct
24 Correct 6 ms 5468 KB Output is correct
25 Correct 5 ms 5724 KB Output is correct
26 Correct 5 ms 5332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 210 ms 34388 KB Output is correct
3 Correct 304 ms 57728 KB Output is correct
4 Correct 199 ms 31056 KB Output is correct
5 Correct 193 ms 34192 KB Output is correct
6 Correct 275 ms 37500 KB Output is correct
7 Correct 186 ms 35064 KB Output is correct
8 Correct 261 ms 56660 KB Output is correct
9 Correct 168 ms 37604 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 233 ms 34388 KB Output is correct
12 Correct 330 ms 57500 KB Output is correct
13 Correct 184 ms 31168 KB Output is correct
14 Correct 192 ms 34328 KB Output is correct
15 Correct 226 ms 38400 KB Output is correct
16 Correct 208 ms 37364 KB Output is correct
17 Correct 257 ms 50292 KB Output is correct
18 Correct 185 ms 37308 KB Output is correct
19 Correct 3 ms 5168 KB Output is correct
20 Correct 205 ms 34348 KB Output is correct
21 Correct 318 ms 57648 KB Output is correct
22 Correct 206 ms 31248 KB Output is correct
23 Correct 211 ms 34740 KB Output is correct
24 Correct 205 ms 33556 KB Output is correct
25 Correct 223 ms 34612 KB Output is correct
26 Correct 195 ms 33408 KB Output is correct
27 Correct 211 ms 34104 KB Output is correct
28 Correct 237 ms 37936 KB Output is correct
29 Correct 205 ms 34960 KB Output is correct
30 Correct 193 ms 32792 KB Output is correct
31 Correct 172 ms 35072 KB Output is correct
32 Correct 246 ms 51100 KB Output is correct
33 Correct 193 ms 37616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 5168 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 210 ms 34388 KB Output is correct
14 Correct 304 ms 57728 KB Output is correct
15 Correct 199 ms 31056 KB Output is correct
16 Correct 193 ms 34192 KB Output is correct
17 Correct 275 ms 37500 KB Output is correct
18 Correct 186 ms 35064 KB Output is correct
19 Correct 261 ms 56660 KB Output is correct
20 Correct 168 ms 37604 KB Output is correct
21 Correct 2 ms 4956 KB Output is correct
22 Correct 233 ms 34388 KB Output is correct
23 Correct 330 ms 57500 KB Output is correct
24 Correct 184 ms 31168 KB Output is correct
25 Correct 192 ms 34328 KB Output is correct
26 Correct 226 ms 38400 KB Output is correct
27 Correct 208 ms 37364 KB Output is correct
28 Correct 257 ms 50292 KB Output is correct
29 Correct 185 ms 37308 KB Output is correct
30 Correct 2 ms 4952 KB Output is correct
31 Correct 6 ms 5468 KB Output is correct
32 Correct 5 ms 5724 KB Output is correct
33 Correct 5 ms 5472 KB Output is correct
34 Correct 6 ms 5464 KB Output is correct
35 Correct 6 ms 5468 KB Output is correct
36 Correct 6 ms 5468 KB Output is correct
37 Correct 6 ms 5468 KB Output is correct
38 Correct 5 ms 5272 KB Output is correct
39 Correct 6 ms 5468 KB Output is correct
40 Correct 5 ms 5324 KB Output is correct
41 Correct 6 ms 5264 KB Output is correct
42 Correct 6 ms 5468 KB Output is correct
43 Correct 5 ms 5724 KB Output is correct
44 Correct 5 ms 5332 KB Output is correct
45 Correct 3 ms 5168 KB Output is correct
46 Correct 205 ms 34348 KB Output is correct
47 Correct 318 ms 57648 KB Output is correct
48 Correct 206 ms 31248 KB Output is correct
49 Correct 211 ms 34740 KB Output is correct
50 Correct 205 ms 33556 KB Output is correct
51 Correct 223 ms 34612 KB Output is correct
52 Correct 195 ms 33408 KB Output is correct
53 Correct 211 ms 34104 KB Output is correct
54 Correct 237 ms 37936 KB Output is correct
55 Correct 205 ms 34960 KB Output is correct
56 Correct 193 ms 32792 KB Output is correct
57 Correct 172 ms 35072 KB Output is correct
58 Correct 246 ms 51100 KB Output is correct
59 Correct 193 ms 37616 KB Output is correct
60 Correct 3 ms 4952 KB Output is correct
61 Correct 462 ms 37180 KB Output is correct
62 Correct 494 ms 60756 KB Output is correct
63 Correct 457 ms 35284 KB Output is correct
64 Correct 488 ms 37364 KB Output is correct
65 Correct 425 ms 36156 KB Output is correct
66 Correct 460 ms 37696 KB Output is correct
67 Correct 411 ms 36132 KB Output is correct
68 Correct 494 ms 36956 KB Output is correct
69 Correct 502 ms 40704 KB Output is correct
70 Correct 418 ms 37860 KB Output is correct
71 Correct 462 ms 35628 KB Output is correct
72 Correct 401 ms 37884 KB Output is correct
73 Correct 554 ms 53704 KB Output is correct
74 Correct 439 ms 40412 KB Output is correct