Submission #153313

# Submission time Handle Problem Language Result Execution time Memory
153313 2019-09-13T12:11:02 Z username Designated Cities (JOI19_designated_cities) C++14
100 / 100
610 ms 48380 KB
#include<bits/stdc++.h>
#define int int64_t
using namespace std;
typedef pair<int,int> pii;
#define REP(i,j,k) for(int i=(j);i<(k);++i)
#define MAX(x,y) (x=max(x,(y)))
#define pb push_back
#define f first
#define s second
#define endl '\n'
#define IOS cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false)

struct tup{int f,s,t;};
const int maxn=2e5+9;
int n,q,s=0,k=0,x=0,res[maxn];
vector<tup>g[maxn];
vector<int>ch;

pii far(int u,int f){
	pii ret={0,u};
	REP(i,0,g[u].size()){
		tup&e=g[u][i];
		if(e.f==f)continue;
		pii tt=far(e.f,u);
		MAX(ret,pii(tt.f+e.s,tt.s));
	}
	return ret;
}

int tdc(int u,int f){
	int mx=0;
	REP(i,0,g[u].size()){
		tup&e=g[u][i];
		if(e.f==f){
			k+=e.s;
			continue;
		}
		int t=tdc(e.f,u)+e.s;
		if(t<=mx)ch.pb(t);
		if(t>mx)ch.pb(mx),mx=t;
	}
	return mx;
}

void dfs(int u,int f){
	REP(i,0,g[u].size()){
		tup&e=g[u][i];
		if(e.f==f){
			x+=e.s;
			continue;
		}
		dfs(e.f,u);
	}
}

void calc(int u,int f){
	MAX(res[1],x);
	REP(i,0,g[u].size()){
		tup&e=g[u][i];
		if(e.f==f)continue;
		x+=e.s-e.t;
		calc(e.f,u);
		x-=e.s-e.t;
	}
}
main(){
	IOS;
	cin>>n;
	REP(i,1,n){
		int a,b,c,d;cin>>a>>b>>c>>d,--a,--b;
		g[a].pb(tup{b,c,d});
		g[b].pb(tup{a,d,c});
		s+=c+d;
	}
	ch.pb(tdc(far(far(0,-1).s,-1).s,-1));
	sort(ch.begin(),ch.end(),[](int a,int b){return a>b;});
	res[1]=k;
	REP(i,2,n+1)res[i]=res[i-1]+(i-2<ch.size()?ch[i-2]:0);
	dfs(0,-1);
	calc(0,-1);
	cin>>q;
	REP(i,0,q){
		int e;cin>>e;
		cout<<s-res[e]<<endl;
	}
}

Compilation message

designated_cities.cpp: In function 'pii far(int64_t, int64_t)':
designated_cities.cpp:5:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,j,k) for(int i=(j);i<(k);++i)
                                   ^
designated_cities.cpp:21:2: note: in expansion of macro 'REP'
  REP(i,0,g[u].size()){
  ^~~
designated_cities.cpp: In function 'int64_t tdc(int64_t, int64_t)':
designated_cities.cpp:5:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,j,k) for(int i=(j);i<(k);++i)
                                   ^
designated_cities.cpp:32:2: note: in expansion of macro 'REP'
  REP(i,0,g[u].size()){
  ^~~
designated_cities.cpp: In function 'void dfs(int64_t, int64_t)':
designated_cities.cpp:5:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,j,k) for(int i=(j);i<(k);++i)
                                   ^
designated_cities.cpp:46:2: note: in expansion of macro 'REP'
  REP(i,0,g[u].size()){
  ^~~
designated_cities.cpp: In function 'void calc(int64_t, int64_t)':
designated_cities.cpp:5:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,j,k) for(int i=(j);i<(k);++i)
                                   ^
designated_cities.cpp:58:2: note: in expansion of macro 'REP'
  REP(i,0,g[u].size()){
  ^~~
designated_cities.cpp: At global scope:
designated_cities.cpp:66:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:78:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  REP(i,2,n+1)res[i]=res[i-1]+(i-2<ch.size()?ch[i-2]:0);
                               ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 7 ms 4984 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 8 ms 5112 KB Output is correct
9 Correct 8 ms 5112 KB Output is correct
10 Correct 8 ms 5112 KB Output is correct
11 Correct 8 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 554 ms 22216 KB Output is correct
3 Correct 610 ms 41164 KB Output is correct
4 Correct 397 ms 22272 KB Output is correct
5 Correct 460 ms 22864 KB Output is correct
6 Correct 455 ms 24764 KB Output is correct
7 Correct 346 ms 22944 KB Output is correct
8 Correct 536 ms 40204 KB Output is correct
9 Correct 226 ms 22044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 387 ms 22260 KB Output is correct
3 Correct 512 ms 41004 KB Output is correct
4 Correct 396 ms 22248 KB Output is correct
5 Correct 383 ms 22984 KB Output is correct
6 Correct 468 ms 25400 KB Output is correct
7 Correct 246 ms 22168 KB Output is correct
8 Correct 480 ms 34396 KB Output is correct
9 Correct 253 ms 22036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 7 ms 4984 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 8 ms 5112 KB Output is correct
9 Correct 8 ms 5112 KB Output is correct
10 Correct 8 ms 5112 KB Output is correct
11 Correct 8 ms 4984 KB Output is correct
12 Correct 7 ms 5112 KB Output is correct
13 Correct 10 ms 5240 KB Output is correct
14 Correct 9 ms 5500 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 10 ms 5240 KB Output is correct
17 Correct 10 ms 5244 KB Output is correct
18 Correct 11 ms 5240 KB Output is correct
19 Correct 10 ms 5240 KB Output is correct
20 Correct 10 ms 5240 KB Output is correct
21 Correct 9 ms 5372 KB Output is correct
22 Correct 9 ms 5368 KB Output is correct
23 Correct 9 ms 5368 KB Output is correct
24 Correct 9 ms 5244 KB Output is correct
25 Correct 9 ms 5496 KB Output is correct
26 Correct 8 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 554 ms 22216 KB Output is correct
3 Correct 610 ms 41164 KB Output is correct
4 Correct 397 ms 22272 KB Output is correct
5 Correct 460 ms 22864 KB Output is correct
6 Correct 455 ms 24764 KB Output is correct
7 Correct 346 ms 22944 KB Output is correct
8 Correct 536 ms 40204 KB Output is correct
9 Correct 226 ms 22044 KB Output is correct
10 Correct 6 ms 4984 KB Output is correct
11 Correct 387 ms 22260 KB Output is correct
12 Correct 512 ms 41004 KB Output is correct
13 Correct 396 ms 22248 KB Output is correct
14 Correct 383 ms 22984 KB Output is correct
15 Correct 468 ms 25400 KB Output is correct
16 Correct 246 ms 22168 KB Output is correct
17 Correct 480 ms 34396 KB Output is correct
18 Correct 253 ms 22036 KB Output is correct
19 Correct 8 ms 4984 KB Output is correct
20 Correct 400 ms 23024 KB Output is correct
21 Correct 605 ms 46672 KB Output is correct
22 Correct 407 ms 26472 KB Output is correct
23 Correct 516 ms 28060 KB Output is correct
24 Correct 485 ms 27064 KB Output is correct
25 Correct 482 ms 28144 KB Output is correct
26 Correct 484 ms 26904 KB Output is correct
27 Correct 411 ms 28068 KB Output is correct
28 Correct 442 ms 30468 KB Output is correct
29 Correct 416 ms 28392 KB Output is correct
30 Correct 417 ms 27396 KB Output is correct
31 Correct 344 ms 28304 KB Output is correct
32 Correct 485 ms 40688 KB Output is correct
33 Correct 237 ms 27672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 7 ms 4984 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 8 ms 5112 KB Output is correct
9 Correct 8 ms 5112 KB Output is correct
10 Correct 8 ms 5112 KB Output is correct
11 Correct 8 ms 4984 KB Output is correct
12 Correct 8 ms 5112 KB Output is correct
13 Correct 554 ms 22216 KB Output is correct
14 Correct 610 ms 41164 KB Output is correct
15 Correct 397 ms 22272 KB Output is correct
16 Correct 460 ms 22864 KB Output is correct
17 Correct 455 ms 24764 KB Output is correct
18 Correct 346 ms 22944 KB Output is correct
19 Correct 536 ms 40204 KB Output is correct
20 Correct 226 ms 22044 KB Output is correct
21 Correct 6 ms 4984 KB Output is correct
22 Correct 387 ms 22260 KB Output is correct
23 Correct 512 ms 41004 KB Output is correct
24 Correct 396 ms 22248 KB Output is correct
25 Correct 383 ms 22984 KB Output is correct
26 Correct 468 ms 25400 KB Output is correct
27 Correct 246 ms 22168 KB Output is correct
28 Correct 480 ms 34396 KB Output is correct
29 Correct 253 ms 22036 KB Output is correct
30 Correct 7 ms 5112 KB Output is correct
31 Correct 10 ms 5240 KB Output is correct
32 Correct 9 ms 5500 KB Output is correct
33 Correct 9 ms 5240 KB Output is correct
34 Correct 10 ms 5240 KB Output is correct
35 Correct 10 ms 5244 KB Output is correct
36 Correct 11 ms 5240 KB Output is correct
37 Correct 10 ms 5240 KB Output is correct
38 Correct 10 ms 5240 KB Output is correct
39 Correct 9 ms 5372 KB Output is correct
40 Correct 9 ms 5368 KB Output is correct
41 Correct 9 ms 5368 KB Output is correct
42 Correct 9 ms 5244 KB Output is correct
43 Correct 9 ms 5496 KB Output is correct
44 Correct 8 ms 5368 KB Output is correct
45 Correct 8 ms 4984 KB Output is correct
46 Correct 400 ms 23024 KB Output is correct
47 Correct 605 ms 46672 KB Output is correct
48 Correct 407 ms 26472 KB Output is correct
49 Correct 516 ms 28060 KB Output is correct
50 Correct 485 ms 27064 KB Output is correct
51 Correct 482 ms 28144 KB Output is correct
52 Correct 484 ms 26904 KB Output is correct
53 Correct 411 ms 28068 KB Output is correct
54 Correct 442 ms 30468 KB Output is correct
55 Correct 416 ms 28392 KB Output is correct
56 Correct 417 ms 27396 KB Output is correct
57 Correct 344 ms 28304 KB Output is correct
58 Correct 485 ms 40688 KB Output is correct
59 Correct 237 ms 27672 KB Output is correct
60 Correct 6 ms 4984 KB Output is correct
61 Correct 472 ms 30384 KB Output is correct
62 Correct 552 ms 48380 KB Output is correct
63 Correct 389 ms 29160 KB Output is correct
64 Correct 573 ms 30688 KB Output is correct
65 Correct 432 ms 29352 KB Output is correct
66 Correct 456 ms 30840 KB Output is correct
67 Correct 551 ms 29380 KB Output is correct
68 Correct 509 ms 30888 KB Output is correct
69 Correct 452 ms 33280 KB Output is correct
70 Correct 456 ms 31208 KB Output is correct
71 Correct 374 ms 30112 KB Output is correct
72 Correct 368 ms 31908 KB Output is correct
73 Correct 561 ms 42760 KB Output is correct
74 Correct 276 ms 31896 KB Output is correct