Submission #334707

# Submission time Handle Problem Language Result Execution time Memory
334707 2020-12-09T20:48:01 Z CaroLinda Designated Cities (JOI19_designated_cities) C++14
100 / 100
541 ms 67692 KB
#include <bits/stdc++.h>

#define debug printf
#define ll long long
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size() )

const int MAXN = 2e5+10 ;
const ll inf = 1e18+10LL ;

using namespace std ;

int N , known ;
int ans[MAXN] ;
vector< pair<int,ll> > adj[MAXN] ;
ll ans1, ans2 , sumOfAll ;
ll prof[MAXN] , sub[MAXN] ;
bool spinned[MAXN] ;
vector<ll> leaves ;

void dfs1(int x, int parent )
{

	for(auto e : adj[x] )
	{
		if( e.first == parent ) continue ;

		dfs1(e.first,x) ;
	
	   sub[x] += sub[e.first] + e.second ;
		prof[x] = max(prof[x] , prof[e.first] + e.second ) ;

	}	
}

void dfs2(int x)
{
	spinned[x] = true ;
	sub[x] = 0 ;

	int tam = sz(adj[x] ) ;
	vector<ll> pref, suf ;

	for(int i = 0 ; i < tam ; i++ )
	{
		pref.push_back( prof[ adj[x][i].first ] + adj[x][i].second ) ;
		suf.push_back( prof[ adj[x][i].first ] + adj[x][i].second ) ;

		sub[x] += sub[ adj[x][i].first ] + adj[x][i].second ;
	}

	for(int i = 1 , j = tam-2 ; i < tam ; i++ , j-- ) 
	{
		pref[i] = max(pref[i] , pref[i-1] ) ;
		suf[j] = max(suf[j], suf[j+1] ) ;
	}

	prof[x] = suf[0] ;

	ans1 = min(ans1, sub[x] ) ;

	if(ans2 >= sub[x] - prof[x] && tam == 1 )
	{
		ans2 = sub[x] - prof[x] ;		
		known = x ;
	}

	for(int i = 0 ; i < tam ; i++ )
	{

		if( spinned[ adj[x][i].first ] ) continue ;

		prof[x] = 0 ;
		if( i ) prof[x] = max( pref[i-1], prof[x] ) ;
		if( i+1 < tam ) prof[x] = max(prof[x], suf[i+1] ) ;
		
		ll saveSubX = sub[x] ;
		ll saveSubChild = sub[ adj[x][i].first ] ;

		sub[x] -= sub[ adj[x][i].first ] + adj[x][i].second ;

		dfs2( adj[x][i].first ) ;

		sub[x] = saveSubX ;
		sub[ adj[x][i].first ] = saveSubChild ;

	}  
     
}

ll dfs3(int x, int parent)
{
	sub[x] = 0 ;
	
	vector< pair<ll,ll> > children ;

	for(auto e : adj[x] )
	{
		if(e.first == parent ) continue ;

		children.push_back( make_pair(dfs3(e.first,x)+e.second, e.second) ) ;
		sub[x] += sub[e.first] + e.second ;
	}

	sort(all(children) ) ;

	for(int i = 0 ; i < sz(children) - 1 ; i++ )
		leaves.push_back(children[i].first ) ;

	return (sz(children) == 0) ? 0LL : children.back().first ;

}

int main()
{
	scanf("%d", &N ) ;
	for(int i = 0 , u , v , uv, vu ; i < N-1 ; i++ )
	{
		scanf("%d %d %d %d", &u, &v, &uv, &vu ) ;

		adj[u].push_back( make_pair(v,(ll)uv) ) ;
		adj[v].push_back( make_pair(u,(ll)vu) ) ;

	}

	ans1 = ans2 = inf ;

	dfs1(1,-1) ;
	dfs2(1) ;
	leaves.push_back(dfs3(known,-1) ) ;

	int Q ;
	scanf("%d", &Q ) ;

	sort(all(leaves) ) ;
	reverse( all(leaves) ) ;

	vector<ll> ans(N+1) ;

	ans[1] = ans1 ;
	ans[2] = sub[ known ] - leaves[0] ;

	for(int i = 1 , j = 3 ; j <= N ; i++ , j++ )
	{
		if( i < sz(leaves) )
			ans[j] = ans[j-1] - leaves[i] ;		
		else ans[j] = 0 ;
	}

	for(int i = 0 , x ; i < Q ; i++ )
	{
		scanf("%d", &x ) ;
		printf("%lld\n", ans[x] ) ;
	}  

}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  116 |  scanf("%d", &N ) ;
      |  ~~~~~^~~~~~~~~~~
designated_cities.cpp:119:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  119 |   scanf("%d %d %d %d", &u, &v, &uv, &vu ) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:133:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  133 |  scanf("%d", &Q ) ;
      |  ~~~~~^~~~~~~~~~~
designated_cities.cpp:152:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  152 |   scanf("%d", &x ) ;
      |   ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 400 ms 21088 KB Output is correct
3 Correct 502 ms 59116 KB Output is correct
4 Correct 399 ms 21216 KB Output is correct
5 Correct 375 ms 21684 KB Output is correct
6 Correct 414 ms 27240 KB Output is correct
7 Correct 330 ms 21868 KB Output is correct
8 Correct 494 ms 60396 KB Output is correct
9 Correct 241 ms 24800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 392 ms 21088 KB Output is correct
3 Correct 501 ms 67692 KB Output is correct
4 Correct 387 ms 21088 KB Output is correct
5 Correct 380 ms 21716 KB Output is correct
6 Correct 431 ms 28140 KB Output is correct
7 Correct 272 ms 25684 KB Output is correct
8 Correct 472 ms 45420 KB Output is correct
9 Correct 249 ms 24724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 6 ms 5356 KB Output is correct
14 Correct 6 ms 5612 KB Output is correct
15 Correct 6 ms 5228 KB Output is correct
16 Correct 6 ms 5356 KB Output is correct
17 Correct 6 ms 5228 KB Output is correct
18 Correct 6 ms 5356 KB Output is correct
19 Correct 6 ms 5228 KB Output is correct
20 Correct 6 ms 5228 KB Output is correct
21 Correct 6 ms 5356 KB Output is correct
22 Correct 6 ms 5356 KB Output is correct
23 Correct 6 ms 5228 KB Output is correct
24 Correct 6 ms 5356 KB Output is correct
25 Correct 6 ms 5612 KB Output is correct
26 Correct 6 ms 5356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 400 ms 21088 KB Output is correct
3 Correct 502 ms 59116 KB Output is correct
4 Correct 399 ms 21216 KB Output is correct
5 Correct 375 ms 21684 KB Output is correct
6 Correct 414 ms 27240 KB Output is correct
7 Correct 330 ms 21868 KB Output is correct
8 Correct 494 ms 60396 KB Output is correct
9 Correct 241 ms 24800 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 392 ms 21088 KB Output is correct
12 Correct 501 ms 67692 KB Output is correct
13 Correct 387 ms 21088 KB Output is correct
14 Correct 380 ms 21716 KB Output is correct
15 Correct 431 ms 28140 KB Output is correct
16 Correct 272 ms 25684 KB Output is correct
17 Correct 472 ms 45420 KB Output is correct
18 Correct 249 ms 24724 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 388 ms 21184 KB Output is correct
21 Correct 502 ms 66540 KB Output is correct
22 Correct 380 ms 21216 KB Output is correct
23 Correct 396 ms 21600 KB Output is correct
24 Correct 380 ms 21480 KB Output is correct
25 Correct 404 ms 21668 KB Output is correct
26 Correct 385 ms 21548 KB Output is correct
27 Correct 374 ms 21376 KB Output is correct
28 Correct 413 ms 26988 KB Output is correct
29 Correct 406 ms 22072 KB Output is correct
30 Correct 361 ms 21364 KB Output is correct
31 Correct 311 ms 23424 KB Output is correct
32 Correct 492 ms 47320 KB Output is correct
33 Correct 243 ms 24792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 3 ms 5100 KB Output is correct
13 Correct 400 ms 21088 KB Output is correct
14 Correct 502 ms 59116 KB Output is correct
15 Correct 399 ms 21216 KB Output is correct
16 Correct 375 ms 21684 KB Output is correct
17 Correct 414 ms 27240 KB Output is correct
18 Correct 330 ms 21868 KB Output is correct
19 Correct 494 ms 60396 KB Output is correct
20 Correct 241 ms 24800 KB Output is correct
21 Correct 4 ms 5100 KB Output is correct
22 Correct 392 ms 21088 KB Output is correct
23 Correct 501 ms 67692 KB Output is correct
24 Correct 387 ms 21088 KB Output is correct
25 Correct 380 ms 21716 KB Output is correct
26 Correct 431 ms 28140 KB Output is correct
27 Correct 272 ms 25684 KB Output is correct
28 Correct 472 ms 45420 KB Output is correct
29 Correct 249 ms 24724 KB Output is correct
30 Correct 4 ms 5100 KB Output is correct
31 Correct 6 ms 5356 KB Output is correct
32 Correct 6 ms 5612 KB Output is correct
33 Correct 6 ms 5228 KB Output is correct
34 Correct 6 ms 5356 KB Output is correct
35 Correct 6 ms 5228 KB Output is correct
36 Correct 6 ms 5356 KB Output is correct
37 Correct 6 ms 5228 KB Output is correct
38 Correct 6 ms 5228 KB Output is correct
39 Correct 6 ms 5356 KB Output is correct
40 Correct 6 ms 5356 KB Output is correct
41 Correct 6 ms 5228 KB Output is correct
42 Correct 6 ms 5356 KB Output is correct
43 Correct 6 ms 5612 KB Output is correct
44 Correct 6 ms 5356 KB Output is correct
45 Correct 4 ms 5100 KB Output is correct
46 Correct 388 ms 21184 KB Output is correct
47 Correct 502 ms 66540 KB Output is correct
48 Correct 380 ms 21216 KB Output is correct
49 Correct 396 ms 21600 KB Output is correct
50 Correct 380 ms 21480 KB Output is correct
51 Correct 404 ms 21668 KB Output is correct
52 Correct 385 ms 21548 KB Output is correct
53 Correct 374 ms 21376 KB Output is correct
54 Correct 413 ms 26988 KB Output is correct
55 Correct 406 ms 22072 KB Output is correct
56 Correct 361 ms 21364 KB Output is correct
57 Correct 311 ms 23424 KB Output is correct
58 Correct 492 ms 47320 KB Output is correct
59 Correct 243 ms 24792 KB Output is correct
60 Correct 4 ms 5100 KB Output is correct
61 Correct 444 ms 22624 KB Output is correct
62 Correct 541 ms 61548 KB Output is correct
63 Correct 429 ms 22752 KB Output is correct
64 Correct 463 ms 23136 KB Output is correct
65 Correct 442 ms 22912 KB Output is correct
66 Correct 452 ms 23044 KB Output is correct
67 Correct 441 ms 22916 KB Output is correct
68 Correct 432 ms 23520 KB Output is correct
69 Correct 461 ms 27772 KB Output is correct
70 Correct 458 ms 23284 KB Output is correct
71 Correct 419 ms 22784 KB Output is correct
72 Correct 384 ms 22900 KB Output is correct
73 Correct 537 ms 44652 KB Output is correct
74 Correct 298 ms 24684 KB Output is correct