답안 #918466

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
918466 2024-01-29T21:17:29 Z amirhoseinfar1385 Designated Cities (JOI19_designated_cities) C++17
100 / 100
906 ms 225896 KB
#include<bits/stdc++.h>
using namespace std;
const long long maxn=200000+10;
struct yal{
	long long u,v,wu,wv,jam;
	long long getad(long long fu){
		if(fu==u){
			return v;
		}
		return u;
	}
	long long getval(long long fu){
		if(fu==u){
			return wu;
		}
		return wv;
	}
}alle[maxn];
vector<long long>adj[maxn];
long long n,val[maxn],res[maxn],suma=0,kaf=0,valpre[maxn];
deque<long long>mxd[maxn];
 
void prea(long long u=1,long long p=-1){
	for(auto x:adj[u]){
		long long v=alle[x].getad(u);
		long long wu=alle[x].getval(u);
		if(v==p){
			val[u]+=wu;
		}
		else{
			val[1]+=wu;
			val[v]-=wu;
			prea(v,u);
		}
	}
}
 
void dfsa(long long u=1,long long p=-1){
	if(p!=-1){
		val[u]+=val[p];
	}
	for(auto x:adj[u]){
		long long v=alle[x].getad(u);
		if(v!=p){
			dfsa(v,u);
		}
	}
}
 
 
void aval(){
	prea();
	dfsa();
	long long mx=0;
	for(long long i=1;i<=n;i++){
		if(val[i]>=mx){
			res[1]=val[i];
			mx=val[i];
		}
	}
}
set<pair<long long,long long>>dp[maxn];
pair<long long,long long>stf[maxn];
long long timea=0; 

void predfs(long long u,long long par=-1){
	mxd[u].push_back(0);
	mxd[u].push_back(0);
	timea++;
	stf[u].first=timea;
	for(auto x:adj[u]){
		long long v=alle[x].getad(u);
		if(v!=par){
			predfs(v,u);
			mxd[u].push_back(mxd[v].back()+alle[x].getval(v));
			sort(mxd[u].begin(),mxd[u].end());
			mxd[u].pop_front();
		}
	}
	stf[u].second=timea;
	for(auto x:adj[u]){
		long long v=alle[x].getad(u);
		if(v==par){
			valpre[stf[u].first]+=alle[x].getval(u);
			valpre[stf[u].second+1]-=alle[x].getval(u);
			valpre[0]+=alle[x].getval(v);
			valpre[stf[u].first]-=alle[x].getval(v);
			valpre[stf[u].second+1]+=alle[x].getval(v);
		}
	}
}

void dfsdown(long long u,long long par=-1,long long len=0){
	mxd[u].push_back(len);
	sort(mxd[u].begin(),mxd[u].end());
	mxd[u].pop_front();
	for(auto x:adj[u]){
		long long v=alle[x].getad(u);
		if(v!=par){
			if(mxd[u].back()==mxd[v].back()+alle[x].getval(v)){
				dfsdown(v,u,mxd[u][0]+alle[x].getval(u));
			}
			else{
				dfsdown(v,u,mxd[u].back()+alle[x].getval(u));
			}
		}
	}
}

long long getd(){
	predfs(1);
	dfsdown(1);
	for(long long i=1;i<maxn;i++){
		valpre[i]+=valpre[i-1];
	}
	long long rt=1,mx=0;
	for(long long i=1;i<=n;i++){
		if(mx<=valpre[stf[i].first]+mxd[i].back()){
			rt=i;
			mx=valpre[stf[i].first]+mxd[i].back();
		}
	}
	return rt;
}
 
void merge(long long u,long long v){
	long long fu=u;
	if((long long)dp[u].size()<dp[v].size()){
		swap(u,v);
	}
	for(auto x:dp[v]){
		dp[u].insert(x);
	}
	if(fu!=u){
		swap(dp[fu],dp[u]);
	}
}
 
/*void dfs(long long u,long long p=-1,long long len=0){
	if(p!=-1){
		dp[u].insert(make_pair(0,u));
	}
	for(auto x:adj[u]){
		long long v=alle[x].getad(u);
		if(v!=p){
			//cout<<"ha: "<<u<<" "<<v<<" "<<len<<endl;
			kaf+=alle[x].getval(u);
			dfs(v,u,alle[x].getval(v));
			//cout<<"salam: "<<u<<" "<<v<<endl;
			merge(u,v);			
			//cout<<"injy vas mas "<<u<<" "<<dp[u].size()<<" "<<v<<" "<<dp[v].size()<<endl;
		}
	}
	auto x=*dp[u].rbegin();
	dp[u].erase(x);
	x.first+=len;
	dp[u].insert(x);
}*/





long long dfs(long long u,long long p=-1,long long len=0){
//	dp[u].clear();
//	if(p!=-1){
//		dp[u].insert(make_pair(0,u));
//	}
	val[u]=0;
	long long mx=0,rt=u;
	for(auto x:adj[u]){
		long long v=alle[x].getad(u);
		if(v!=p){
			kaf+=alle[x].getval(u);
			int z=dfs(v,u,alle[x].getval(v));
		//	merge(u,v);
			if(val[z]>=mx){
				rt=z;
				mx=val[z];
			}			
		}
	}
//	auto x=*dp[u].rbegin();
//	dp[u].erase(x);
//	x.first+=len;
//	dp[u].insert(x);
	val[rt]+=len;
	return rt;
}
 
 
void solve(long long u){
	kaf=0;
	dfs(u);
	vector<long long>hamres(n+2);
	hamres[0]=kaf;
	vector<long long>tof;
	for(int i=1;i<=n;i++){
		tof.push_back(val[i]);
	}
	sort(tof.begin(),tof.end());
	for(long long i=1;i<=n;i++){
		hamres[i]=hamres[i-1];
		if((long long)tof.size()>0){
			hamres[i]+=(tof.back());
			tof.pop_back();
		}
		res[i]=max(res[i],hamres[i-1]);
	}
}
 











void dovombebad(){
	long long g=getd();
	//cout<<"wtf: "<<g<<endl;
	solve(g);
}
 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(long long i=0;i<n-1;i++){
		cin>>alle[i].u>>alle[i].v>>alle[i].wv>>alle[i].wu;
		alle[i].jam=alle[i].wu+alle[i].wv;
		suma+=alle[i].wu+alle[i].wv;
		adj[alle[i].u].push_back(i);
		adj[alle[i].v].push_back(i);
	}
	long long q;
	cin>>q;
	aval();
	if(n>1){
		dovombebad();
	}
	for(long long i=1;i<=n;i++){
		res[i]=suma-res[i];
	}
	for(long long i=0;i<q;i++){
		long long d;
		cin>>d;
		cout<<res[d]<<"\n";
	}
}

Compilation message

designated_cities.cpp: In function 'void merge(long long int, long long int)':
designated_cities.cpp:128:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |  if((long long)dp[u].size()<dp[v].size()){
      |     ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 156240 KB Output is correct
2 Correct 70 ms 156244 KB Output is correct
3 Correct 74 ms 156244 KB Output is correct
4 Correct 70 ms 156244 KB Output is correct
5 Correct 74 ms 156616 KB Output is correct
6 Correct 70 ms 156152 KB Output is correct
7 Correct 70 ms 156240 KB Output is correct
8 Correct 72 ms 156316 KB Output is correct
9 Correct 71 ms 156240 KB Output is correct
10 Correct 77 ms 156496 KB Output is correct
11 Correct 70 ms 156136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 156244 KB Output is correct
2 Correct 545 ms 176020 KB Output is correct
3 Correct 806 ms 213072 KB Output is correct
4 Correct 523 ms 175820 KB Output is correct
5 Correct 516 ms 175832 KB Output is correct
6 Correct 567 ms 181192 KB Output is correct
7 Correct 492 ms 175948 KB Output is correct
8 Correct 869 ms 214008 KB Output is correct
9 Correct 334 ms 177280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 156200 KB Output is correct
2 Correct 566 ms 181964 KB Output is correct
3 Correct 851 ms 225332 KB Output is correct
4 Correct 561 ms 180756 KB Output is correct
5 Correct 503 ms 181572 KB Output is correct
6 Correct 618 ms 187724 KB Output is correct
7 Correct 385 ms 183032 KB Output is correct
8 Correct 726 ms 205408 KB Output is correct
9 Correct 347 ms 182720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 156240 KB Output is correct
2 Correct 70 ms 156244 KB Output is correct
3 Correct 74 ms 156244 KB Output is correct
4 Correct 70 ms 156244 KB Output is correct
5 Correct 74 ms 156616 KB Output is correct
6 Correct 70 ms 156152 KB Output is correct
7 Correct 70 ms 156240 KB Output is correct
8 Correct 72 ms 156316 KB Output is correct
9 Correct 71 ms 156240 KB Output is correct
10 Correct 77 ms 156496 KB Output is correct
11 Correct 70 ms 156136 KB Output is correct
12 Correct 72 ms 156252 KB Output is correct
13 Correct 75 ms 156612 KB Output is correct
14 Correct 78 ms 156876 KB Output is correct
15 Correct 74 ms 156604 KB Output is correct
16 Correct 76 ms 156500 KB Output is correct
17 Correct 76 ms 156452 KB Output is correct
18 Correct 77 ms 156400 KB Output is correct
19 Correct 73 ms 156496 KB Output is correct
20 Correct 77 ms 156604 KB Output is correct
21 Correct 77 ms 156624 KB Output is correct
22 Correct 76 ms 156360 KB Output is correct
23 Correct 74 ms 156652 KB Output is correct
24 Correct 74 ms 156496 KB Output is correct
25 Correct 74 ms 156752 KB Output is correct
26 Correct 74 ms 156500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 156244 KB Output is correct
2 Correct 545 ms 176020 KB Output is correct
3 Correct 806 ms 213072 KB Output is correct
4 Correct 523 ms 175820 KB Output is correct
5 Correct 516 ms 175832 KB Output is correct
6 Correct 567 ms 181192 KB Output is correct
7 Correct 492 ms 175948 KB Output is correct
8 Correct 869 ms 214008 KB Output is correct
9 Correct 334 ms 177280 KB Output is correct
10 Correct 72 ms 156200 KB Output is correct
11 Correct 566 ms 181964 KB Output is correct
12 Correct 851 ms 225332 KB Output is correct
13 Correct 561 ms 180756 KB Output is correct
14 Correct 503 ms 181572 KB Output is correct
15 Correct 618 ms 187724 KB Output is correct
16 Correct 385 ms 183032 KB Output is correct
17 Correct 726 ms 205408 KB Output is correct
18 Correct 347 ms 182720 KB Output is correct
19 Correct 73 ms 156240 KB Output is correct
20 Correct 576 ms 181824 KB Output is correct
21 Correct 906 ms 225896 KB Output is correct
22 Correct 583 ms 180844 KB Output is correct
23 Correct 598 ms 181860 KB Output is correct
24 Correct 513 ms 180944 KB Output is correct
25 Correct 541 ms 181708 KB Output is correct
26 Correct 534 ms 181200 KB Output is correct
27 Correct 529 ms 181700 KB Output is correct
28 Correct 667 ms 186780 KB Output is correct
29 Correct 612 ms 182108 KB Output is correct
30 Correct 525 ms 181576 KB Output is correct
31 Correct 483 ms 181936 KB Output is correct
32 Correct 756 ms 207184 KB Output is correct
33 Correct 346 ms 183228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 156240 KB Output is correct
2 Correct 70 ms 156244 KB Output is correct
3 Correct 74 ms 156244 KB Output is correct
4 Correct 70 ms 156244 KB Output is correct
5 Correct 74 ms 156616 KB Output is correct
6 Correct 70 ms 156152 KB Output is correct
7 Correct 70 ms 156240 KB Output is correct
8 Correct 72 ms 156316 KB Output is correct
9 Correct 71 ms 156240 KB Output is correct
10 Correct 77 ms 156496 KB Output is correct
11 Correct 70 ms 156136 KB Output is correct
12 Correct 70 ms 156244 KB Output is correct
13 Correct 545 ms 176020 KB Output is correct
14 Correct 806 ms 213072 KB Output is correct
15 Correct 523 ms 175820 KB Output is correct
16 Correct 516 ms 175832 KB Output is correct
17 Correct 567 ms 181192 KB Output is correct
18 Correct 492 ms 175948 KB Output is correct
19 Correct 869 ms 214008 KB Output is correct
20 Correct 334 ms 177280 KB Output is correct
21 Correct 72 ms 156200 KB Output is correct
22 Correct 566 ms 181964 KB Output is correct
23 Correct 851 ms 225332 KB Output is correct
24 Correct 561 ms 180756 KB Output is correct
25 Correct 503 ms 181572 KB Output is correct
26 Correct 618 ms 187724 KB Output is correct
27 Correct 385 ms 183032 KB Output is correct
28 Correct 726 ms 205408 KB Output is correct
29 Correct 347 ms 182720 KB Output is correct
30 Correct 72 ms 156252 KB Output is correct
31 Correct 75 ms 156612 KB Output is correct
32 Correct 78 ms 156876 KB Output is correct
33 Correct 74 ms 156604 KB Output is correct
34 Correct 76 ms 156500 KB Output is correct
35 Correct 76 ms 156452 KB Output is correct
36 Correct 77 ms 156400 KB Output is correct
37 Correct 73 ms 156496 KB Output is correct
38 Correct 77 ms 156604 KB Output is correct
39 Correct 77 ms 156624 KB Output is correct
40 Correct 76 ms 156360 KB Output is correct
41 Correct 74 ms 156652 KB Output is correct
42 Correct 74 ms 156496 KB Output is correct
43 Correct 74 ms 156752 KB Output is correct
44 Correct 74 ms 156500 KB Output is correct
45 Correct 73 ms 156240 KB Output is correct
46 Correct 576 ms 181824 KB Output is correct
47 Correct 906 ms 225896 KB Output is correct
48 Correct 583 ms 180844 KB Output is correct
49 Correct 598 ms 181860 KB Output is correct
50 Correct 513 ms 180944 KB Output is correct
51 Correct 541 ms 181708 KB Output is correct
52 Correct 534 ms 181200 KB Output is correct
53 Correct 529 ms 181700 KB Output is correct
54 Correct 667 ms 186780 KB Output is correct
55 Correct 612 ms 182108 KB Output is correct
56 Correct 525 ms 181576 KB Output is correct
57 Correct 483 ms 181936 KB Output is correct
58 Correct 756 ms 207184 KB Output is correct
59 Correct 346 ms 183228 KB Output is correct
60 Correct 80 ms 156176 KB Output is correct
61 Correct 602 ms 182352 KB Output is correct
62 Correct 831 ms 219656 KB Output is correct
63 Correct 611 ms 181356 KB Output is correct
64 Correct 579 ms 182352 KB Output is correct
65 Correct 554 ms 181324 KB Output is correct
66 Correct 602 ms 182696 KB Output is correct
67 Correct 594 ms 181132 KB Output is correct
68 Correct 606 ms 182416 KB Output is correct
69 Correct 592 ms 186576 KB Output is correct
70 Correct 635 ms 182856 KB Output is correct
71 Correct 611 ms 181576 KB Output is correct
72 Correct 531 ms 182608 KB Output is correct
73 Correct 769 ms 204104 KB Output is correct
74 Correct 371 ms 183888 KB Output is correct