Submission #918427

# Submission time Handle Problem Language Result Execution time Memory
918427 2024-01-29T19:59:08 Z amirhoseinfar1385 Designated Cities (JOI19_designated_cities) C++17
23 / 100
211 ms 32340 KB
#include<bits/stdc++.h>
using namespace std;
const long long maxn=2000+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],now;
long long resr[maxn][maxn],kaf=0,res[maxn],suma,n,cnt=0,val[maxn];

set<pair<long long,long long>>dp[maxn];
void merge(long long u,long long v){
	long long fu=u;
	if((long long)dp[u].size()<(long long)dp[v].size()){
		swap(u,v);
	}
	for(auto x:dp[v]){
		dp[u].insert(x);
	}
	if(fu!=u){
		swap(dp[fu],dp[u]);
	}
}
 
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);
	resr[u][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++){
		resr[u][i]=resr[u][i-1];
		if((long long)tof.size()>0){
			resr[u][i]+=(tof.back());
			tof.pop_back();
		}
		if(i==2){
			if(res[i]<resr[u][i-1]){
				now.clear();
				now.push_back(u);
			}
			else if(res[i]==resr[u][i-1]){
				now.push_back(u);	
			}
		}
		res[i]=max(res[i],resr[u][i-1]);
	}
}

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);
	}
	for(long long i=1;i<=n;i++){
		solve(i);
	}
	for(auto x:now){
		long long f=1;
		for(long long j=2;j<=n;j++){
			if(resr[x][j-1]!=res[j]){
				f=0;
			}
		}
		if(f==0){
			cnt++;
		}
	}
	if(cnt>0){
		assert(0);
	}
	long long q;
	cin>>q;
	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";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Runtime error 1 ms 860 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Runtime error 1 ms 860 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 760 KB Output is correct
13 Correct 195 ms 32124 KB Output is correct
14 Correct 128 ms 32268 KB Output is correct
15 Correct 194 ms 32092 KB Output is correct
16 Correct 179 ms 32124 KB Output is correct
17 Correct 211 ms 32104 KB Output is correct
18 Correct 180 ms 32076 KB Output is correct
19 Correct 186 ms 32092 KB Output is correct
20 Correct 193 ms 32076 KB Output is correct
21 Correct 199 ms 32112 KB Output is correct
22 Correct 187 ms 32124 KB Output is correct
23 Correct 191 ms 32080 KB Output is correct
24 Correct 210 ms 32092 KB Output is correct
25 Correct 153 ms 32340 KB Output is correct
26 Correct 211 ms 32096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Runtime error 1 ms 860 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Runtime error 1 ms 860 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -