답안 #126450

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
126450 2019-07-07T18:48:44 Z TadijaSebez Designated Cities (JOI19_designated_cities) C++11
56 / 100
606 ms 56432 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=400050;
vector<pair<int,int>> E[N];
ll sum[N],all_up;
int up[N],down[N];
ll dep[N];
int lid[N],rid[N],tid,nod[N],par[N],my[N],rt,n;
ll mxd[N],sol[N];
void DFS(int u, int p)
{
	for(auto e:E[u])
	{
		int v=e.first,w=e.second;
		if(v==p) up[u]=w,all_up+=w;
	}
	if(p) sum[u]=sum[p]+down[u]-up[u];
	mxd[u]=dep[u];
	for(auto e:E[u])
	{
		int v=e.first,w=e.second;
		if(v!=p)
		{
			down[v]=w;
			dep[v]=dep[u]+w;
			DFS(v,u);
			mxd[u]=max(mxd[u],mxd[v]);
		}
	}
}
void Solve(int u, int p, ll hi)
{
	ll fir=0,sec=0;
	sol[u]=max(hi,mxd[u]-dep[u])+sum[u];
	for(auto e:E[u]) if(e.first!=p)
	{
		int v=e.first,w=e.second;
		if(fir<mxd[v]) sec=fir,fir=mxd[v];
		else if(sec<mxd[v]) sec=mxd[v];
	}
	for(auto e:E[u]) if(e.first!=p)
	{
		int v=e.first,w=e.second;
		ll best=hi;
		if(fir==mxd[v]) best=max(best,sec);
		else best=max(best,fir);
		Solve(v,u,best+up[v]);
	}
}
void DFS2(int u, int p)
{
	lid[u]=++tid;
	nod[tid]=u;
	par[u]=p;
	my[u]=u;
	for(auto e:E[u]) if(e.first!=p)
	{
		int v=e.first;
		dep[v]=dep[u]+e.second;
		DFS2(v,u);
		if(dep[my[u]]<dep[my[v]]) my[u]=my[v];
	}
	rid[u]=tid;
}
const int M=2*N;
int ls[M],rs[M],tsz,root,id[M];
ll mx[M],lzy[M];
void Build(int &c, int ss, int se)
{
	c=++tsz;
	if(ss==se){ mx[c]=dep[nod[ss]];id[c]=nod[ss];return;}
	int mid=ss+se>>1;
	Build(ls[c],ss,mid);
	Build(rs[c],mid+1,se);
	mx[c]=max(mx[ls[c]],mx[rs[c]]);
	if(mx[c]==mx[ls[c]]) id[c]=id[ls[c]];
	else id[c]=id[rs[c]];
	//printf("%i %lld %i\n",c,mx[c],id[c]);
}
void Add(int c, int ss, int se, int qs, int qe, ll x)
{
	if(qs>qe || qs>se || ss>qe) return;
	if(qs<=ss && qe>=se){ mx[c]+=x;lzy[c]+=x;return;}
	int mid=ss+se>>1;
	Add(ls[c],ss,mid,qs,qe,x);
	Add(rs[c],mid+1,se,qs,qe,x);
	mx[c]=max(mx[ls[c]],mx[rs[c]]);
	if(mx[c]==mx[ls[c]]) id[c]=id[ls[c]];
	else id[c]=id[rs[c]];
	mx[c]+=lzy[c];
}
bool was[N];
ll ans[N],cur;
void Add(int x)
{
	for(;x!=rt;x=par[x])
	{
		if(was[x]) break;
		was[x]=1;
		Add(root,1,n,lid[x],rid[x],-dep[x]+dep[par[x]]);
		cur+=dep[x]-dep[par[x]];
	}
}
int main()
{
	int q,u,v,c,d;
	scanf("%i",&n);
	ll z=0;
	for(int i=1;i<n;i++)
	{
		scanf("%i %i %i %i",&u,&v,&c,&d);
		E[u].pb({v,c});
		E[v].pb({u,d});
		z+=c+d;
	}
	DFS(1,0);
	rt=1;
	for(int i=1;i<=n;i++)
	{
		sum[i]+=all_up;
		if(sum[i]>sum[rt]) rt=i;
	}
	ans[1]=sum[rt];
	Solve(1,0,0);
	for(int i=1;i<=n;i++) if(sol[i]>sol[rt]) rt=i;
	for(int i=1;i<=n;i++) dep[i]=0;
	DFS2(rt,0);
    Build(root,1,n);
    cur=sum[rt];
    //printf("cur:%lld\n",cur);
    /*int fir=0,sec=0;
    for(auto e:E[rt])
	{
		v=e.first;
		if(dep[my[v]]>dep[fir]) sec=fir,fir=my[v];
		else if(dep[my[v]]>dep[sec]) sec=my[v];
	}
	Add(fir);
	if(sec) Add(sec);*/
	for(int i=2;i<=n;i++)
	{
		//printf("%i: %i\n",i,id[root]);
		Add(id[root]);
		ans[i]=cur;
	}
	scanf("%i",&q);
	while(q--)
	{
		int x;
		scanf("%i",&x);
		printf("%lld\n",z-ans[x]);
	}
	return 0;
}

Compilation message

designated_cities.cpp: In function 'void Solve(int, int, long long int)':
designated_cities.cpp:39:17: warning: unused variable 'w' [-Wunused-variable]
   int v=e.first,w=e.second;
                 ^
designated_cities.cpp:45:17: warning: unused variable 'w' [-Wunused-variable]
   int v=e.first,w=e.second;
                 ^
designated_cities.cpp: In function 'void Build(int&, int, int)':
designated_cities.cpp:74:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
designated_cities.cpp: In function 'void Add(int, int, int, int, int, long long int)':
designated_cities.cpp:86:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
designated_cities.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i %i %i",&u,&v,&c,&d);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:148:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&q);
  ~~~~~^~~~~~~~~
designated_cities.cpp:152:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i",&x);
   ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9848 KB Output is correct
3 Correct 10 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 10 ms 9848 KB Output is correct
7 Correct 10 ms 9848 KB Output is correct
8 Correct 10 ms 9848 KB Output is correct
9 Correct 10 ms 9840 KB Output is correct
10 Correct 10 ms 9848 KB Output is correct
11 Correct 13 ms 9840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 516 ms 42020 KB Output is correct
3 Correct 510 ms 53588 KB Output is correct
4 Correct 402 ms 42144 KB Output is correct
5 Correct 493 ms 42220 KB Output is correct
6 Correct 500 ms 43904 KB Output is correct
7 Correct 472 ms 42232 KB Output is correct
8 Correct 492 ms 54252 KB Output is correct
9 Correct 411 ms 42724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 485 ms 42292 KB Output is correct
3 Correct 460 ms 56220 KB Output is correct
4 Correct 409 ms 42424 KB Output is correct
5 Correct 465 ms 42312 KB Output is correct
6 Correct 506 ms 44408 KB Output is correct
7 Correct 529 ms 42968 KB Output is correct
8 Correct 606 ms 50320 KB Output is correct
9 Correct 437 ms 43112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9848 KB Output is correct
3 Correct 10 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 10 ms 9848 KB Output is correct
7 Correct 10 ms 9848 KB Output is correct
8 Correct 10 ms 9848 KB Output is correct
9 Correct 10 ms 9840 KB Output is correct
10 Correct 10 ms 9848 KB Output is correct
11 Correct 13 ms 9840 KB Output is correct
12 Correct 10 ms 9848 KB Output is correct
13 Correct 13 ms 10232 KB Output is correct
14 Correct 13 ms 10360 KB Output is correct
15 Correct 13 ms 10232 KB Output is correct
16 Correct 13 ms 10232 KB Output is correct
17 Correct 13 ms 10232 KB Output is correct
18 Correct 13 ms 10232 KB Output is correct
19 Correct 13 ms 10232 KB Output is correct
20 Correct 13 ms 10232 KB Output is correct
21 Correct 13 ms 10232 KB Output is correct
22 Correct 13 ms 10232 KB Output is correct
23 Correct 13 ms 10232 KB Output is correct
24 Correct 13 ms 10236 KB Output is correct
25 Correct 13 ms 10360 KB Output is correct
26 Correct 13 ms 10232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 516 ms 42020 KB Output is correct
3 Correct 510 ms 53588 KB Output is correct
4 Correct 402 ms 42144 KB Output is correct
5 Correct 493 ms 42220 KB Output is correct
6 Correct 500 ms 43904 KB Output is correct
7 Correct 472 ms 42232 KB Output is correct
8 Correct 492 ms 54252 KB Output is correct
9 Correct 411 ms 42724 KB Output is correct
10 Correct 10 ms 9848 KB Output is correct
11 Correct 485 ms 42292 KB Output is correct
12 Correct 460 ms 56220 KB Output is correct
13 Correct 409 ms 42424 KB Output is correct
14 Correct 465 ms 42312 KB Output is correct
15 Correct 506 ms 44408 KB Output is correct
16 Correct 529 ms 42968 KB Output is correct
17 Correct 606 ms 50320 KB Output is correct
18 Correct 437 ms 43112 KB Output is correct
19 Correct 10 ms 9852 KB Output is correct
20 Correct 600 ms 42448 KB Output is correct
21 Correct 512 ms 56432 KB Output is correct
22 Correct 471 ms 42616 KB Output is correct
23 Correct 497 ms 42552 KB Output is correct
24 Correct 451 ms 42488 KB Output is correct
25 Correct 588 ms 42360 KB Output is correct
26 Correct 501 ms 42616 KB Output is correct
27 Correct 488 ms 42480 KB Output is correct
28 Correct 527 ms 44024 KB Output is correct
29 Correct 496 ms 42616 KB Output is correct
30 Correct 467 ms 42376 KB Output is correct
31 Correct 469 ms 42476 KB Output is correct
32 Correct 511 ms 50632 KB Output is correct
33 Correct 411 ms 42852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9848 KB Output is correct
3 Correct 10 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 10 ms 9848 KB Output is correct
7 Correct 10 ms 9848 KB Output is correct
8 Correct 10 ms 9848 KB Output is correct
9 Correct 10 ms 9840 KB Output is correct
10 Correct 10 ms 9848 KB Output is correct
11 Correct 13 ms 9840 KB Output is correct
12 Correct 10 ms 9848 KB Output is correct
13 Correct 516 ms 42020 KB Output is correct
14 Correct 510 ms 53588 KB Output is correct
15 Correct 402 ms 42144 KB Output is correct
16 Correct 493 ms 42220 KB Output is correct
17 Correct 500 ms 43904 KB Output is correct
18 Correct 472 ms 42232 KB Output is correct
19 Correct 492 ms 54252 KB Output is correct
20 Correct 411 ms 42724 KB Output is correct
21 Correct 10 ms 9848 KB Output is correct
22 Correct 485 ms 42292 KB Output is correct
23 Correct 460 ms 56220 KB Output is correct
24 Correct 409 ms 42424 KB Output is correct
25 Correct 465 ms 42312 KB Output is correct
26 Correct 506 ms 44408 KB Output is correct
27 Correct 529 ms 42968 KB Output is correct
28 Correct 606 ms 50320 KB Output is correct
29 Correct 437 ms 43112 KB Output is correct
30 Correct 10 ms 9848 KB Output is correct
31 Correct 13 ms 10232 KB Output is correct
32 Correct 13 ms 10360 KB Output is correct
33 Correct 13 ms 10232 KB Output is correct
34 Correct 13 ms 10232 KB Output is correct
35 Correct 13 ms 10232 KB Output is correct
36 Correct 13 ms 10232 KB Output is correct
37 Correct 13 ms 10232 KB Output is correct
38 Correct 13 ms 10232 KB Output is correct
39 Correct 13 ms 10232 KB Output is correct
40 Correct 13 ms 10232 KB Output is correct
41 Correct 13 ms 10232 KB Output is correct
42 Correct 13 ms 10236 KB Output is correct
43 Correct 13 ms 10360 KB Output is correct
44 Correct 13 ms 10232 KB Output is correct
45 Correct 10 ms 9852 KB Output is correct
46 Correct 600 ms 42448 KB Output is correct
47 Correct 512 ms 56432 KB Output is correct
48 Correct 471 ms 42616 KB Output is correct
49 Correct 497 ms 42552 KB Output is correct
50 Correct 451 ms 42488 KB Output is correct
51 Correct 588 ms 42360 KB Output is correct
52 Correct 501 ms 42616 KB Output is correct
53 Correct 488 ms 42480 KB Output is correct
54 Correct 527 ms 44024 KB Output is correct
55 Correct 496 ms 42616 KB Output is correct
56 Correct 467 ms 42376 KB Output is correct
57 Correct 469 ms 42476 KB Output is correct
58 Correct 511 ms 50632 KB Output is correct
59 Correct 411 ms 42852 KB Output is correct
60 Correct 10 ms 9848 KB Output is correct
61 Incorrect 530 ms 43612 KB Output isn't correct
62 Halted 0 ms 0 KB -