Submission #337221

# Submission time Handle Problem Language Result Execution time Memory
337221 2020-12-19T01:23:49 Z alishahali1382 Designated Cities (JOI19_designated_cities) C++14
100 / 100
421 ms 35820 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=300010, LOG=20;

int n, m, k, u, v, x, y, t, a, b;
int deg[MAXN];
bool dead[MAXN];
ll ans[MAXN], sum;
vector<piii> G[MAXN];
priority_queue<pll, vector<pll>, greater<pll>> pq;

piii getpar(int v){
	for (auto p:G[v]) if (!dead[p.first]) return p;
	debug("shit")
	cout<<1/0;
}
void dfs1(int node, int par){
	for (auto p:G[node]) if (p.first!=par){
		dfs1(p.first, node);
		sum+=p.second.first;
	}
}
void dfs2(int node, int par){
	ans[1]=min(ans[1], sum);
	for (auto p:G[node]) if (p.first!=par){
		sum-=p.second.first;
		sum+=p.second.second;
		dfs2(p.first, node);
		sum+=p.second.first;
		sum-=p.second.second;
	}
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n;
	for (int i=1; i<n; i++){
		cin>>u>>v>>x>>y;
		G[u].pb({v, {x, y}});
		G[v].pb({u, {y, x}});
		deg[u]++;
		deg[v]++;
	}
	for (int i=1; i<=n; i++) if (deg[i]==1){
		piii p=getpar(i);
//		debug2(i, p.second.second)
		pq.push({p.second.second, i});
	}
	ans[1]=INF;
	dfs1(1, 0);
//	debug(sum)
	dfs2(1, 0);
	sum=0;
	while (pq.size()>2){
		pll p=pq.top();
		pq.pop();
		int v=p.second;
		dead[v]=1;
		piii pp=getpar(v);
		int u=pp.first;
//		debug2(v, u)
//		debug(p.first)
//		cerr<<"\n";
		deg[u]--;
		if (deg[u]>1){
			sum+=p.first;
			ans[pq.size()]=sum;
			continue ;
		}
		pp=getpar(u);
		pq.push({p.first+pp.second.second, u});
	}
	cin>>m;
	while (m--){
		cin>>x;
		cout<<ans[x]<<"\n";
	}
	
	return 0;
}

Compilation message

designated_cities.cpp: In function 'piii getpar(int)':
designated_cities.cpp:35:9: warning: division by zero [-Wdiv-by-zero]
   35 |  cout<<1/0;
      |        ~^~
designated_cities.cpp:35:10: warning: control reaches end of non-void function [-Wreturn-type]
   35 |  cout<<1/0;
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 6 ms 7404 KB Output is correct
10 Correct 6 ms 7404 KB Output is correct
11 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 391 ms 25184 KB Output is correct
3 Correct 301 ms 33516 KB Output is correct
4 Correct 365 ms 23928 KB Output is correct
5 Correct 379 ms 25692 KB Output is correct
6 Correct 386 ms 26796 KB Output is correct
7 Correct 348 ms 27056 KB Output is correct
8 Correct 313 ms 34800 KB Output is correct
9 Correct 277 ms 30232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 387 ms 25060 KB Output is correct
3 Correct 305 ms 35820 KB Output is correct
4 Correct 364 ms 23904 KB Output is correct
5 Correct 369 ms 25628 KB Output is correct
6 Correct 369 ms 27100 KB Output is correct
7 Correct 288 ms 29600 KB Output is correct
8 Correct 337 ms 32096 KB Output is correct
9 Correct 260 ms 29980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 6 ms 7404 KB Output is correct
10 Correct 6 ms 7404 KB Output is correct
11 Correct 5 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 7 ms 7660 KB Output is correct
14 Correct 6 ms 7660 KB Output is correct
15 Correct 8 ms 7660 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
17 Correct 7 ms 7660 KB Output is correct
18 Correct 8 ms 7660 KB Output is correct
19 Correct 9 ms 7660 KB Output is correct
20 Correct 7 ms 7660 KB Output is correct
21 Correct 7 ms 7680 KB Output is correct
22 Correct 8 ms 7660 KB Output is correct
23 Correct 8 ms 7660 KB Output is correct
24 Correct 7 ms 7660 KB Output is correct
25 Correct 7 ms 7660 KB Output is correct
26 Correct 7 ms 7660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 391 ms 25184 KB Output is correct
3 Correct 301 ms 33516 KB Output is correct
4 Correct 365 ms 23928 KB Output is correct
5 Correct 379 ms 25692 KB Output is correct
6 Correct 386 ms 26796 KB Output is correct
7 Correct 348 ms 27056 KB Output is correct
8 Correct 313 ms 34800 KB Output is correct
9 Correct 277 ms 30232 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 387 ms 25060 KB Output is correct
12 Correct 305 ms 35820 KB Output is correct
13 Correct 364 ms 23904 KB Output is correct
14 Correct 369 ms 25628 KB Output is correct
15 Correct 369 ms 27100 KB Output is correct
16 Correct 288 ms 29600 KB Output is correct
17 Correct 337 ms 32096 KB Output is correct
18 Correct 260 ms 29980 KB Output is correct
19 Correct 5 ms 7404 KB Output is correct
20 Correct 370 ms 25060 KB Output is correct
21 Correct 295 ms 35820 KB Output is correct
22 Correct 364 ms 23824 KB Output is correct
23 Correct 385 ms 25056 KB Output is correct
24 Correct 373 ms 23924 KB Output is correct
25 Correct 381 ms 25056 KB Output is correct
26 Correct 371 ms 24028 KB Output is correct
27 Correct 374 ms 25244 KB Output is correct
28 Correct 369 ms 26836 KB Output is correct
29 Correct 381 ms 24988 KB Output is correct
30 Correct 360 ms 25120 KB Output is correct
31 Correct 330 ms 27932 KB Output is correct
32 Correct 327 ms 32488 KB Output is correct
33 Correct 273 ms 30232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 6 ms 7404 KB Output is correct
10 Correct 6 ms 7404 KB Output is correct
11 Correct 5 ms 7404 KB Output is correct
12 Correct 6 ms 7404 KB Output is correct
13 Correct 391 ms 25184 KB Output is correct
14 Correct 301 ms 33516 KB Output is correct
15 Correct 365 ms 23928 KB Output is correct
16 Correct 379 ms 25692 KB Output is correct
17 Correct 386 ms 26796 KB Output is correct
18 Correct 348 ms 27056 KB Output is correct
19 Correct 313 ms 34800 KB Output is correct
20 Correct 277 ms 30232 KB Output is correct
21 Correct 5 ms 7404 KB Output is correct
22 Correct 387 ms 25060 KB Output is correct
23 Correct 305 ms 35820 KB Output is correct
24 Correct 364 ms 23904 KB Output is correct
25 Correct 369 ms 25628 KB Output is correct
26 Correct 369 ms 27100 KB Output is correct
27 Correct 288 ms 29600 KB Output is correct
28 Correct 337 ms 32096 KB Output is correct
29 Correct 260 ms 29980 KB Output is correct
30 Correct 5 ms 7404 KB Output is correct
31 Correct 7 ms 7660 KB Output is correct
32 Correct 6 ms 7660 KB Output is correct
33 Correct 8 ms 7660 KB Output is correct
34 Correct 7 ms 7660 KB Output is correct
35 Correct 7 ms 7660 KB Output is correct
36 Correct 8 ms 7660 KB Output is correct
37 Correct 9 ms 7660 KB Output is correct
38 Correct 7 ms 7660 KB Output is correct
39 Correct 7 ms 7680 KB Output is correct
40 Correct 8 ms 7660 KB Output is correct
41 Correct 8 ms 7660 KB Output is correct
42 Correct 7 ms 7660 KB Output is correct
43 Correct 7 ms 7660 KB Output is correct
44 Correct 7 ms 7660 KB Output is correct
45 Correct 5 ms 7404 KB Output is correct
46 Correct 370 ms 25060 KB Output is correct
47 Correct 295 ms 35820 KB Output is correct
48 Correct 364 ms 23824 KB Output is correct
49 Correct 385 ms 25056 KB Output is correct
50 Correct 373 ms 23924 KB Output is correct
51 Correct 381 ms 25056 KB Output is correct
52 Correct 371 ms 24028 KB Output is correct
53 Correct 374 ms 25244 KB Output is correct
54 Correct 369 ms 26836 KB Output is correct
55 Correct 381 ms 24988 KB Output is correct
56 Correct 360 ms 25120 KB Output is correct
57 Correct 330 ms 27932 KB Output is correct
58 Correct 327 ms 32488 KB Output is correct
59 Correct 273 ms 30232 KB Output is correct
60 Correct 5 ms 7404 KB Output is correct
61 Correct 410 ms 27748 KB Output is correct
62 Correct 325 ms 35308 KB Output is correct
63 Correct 401 ms 26468 KB Output is correct
64 Correct 410 ms 27740 KB Output is correct
65 Correct 398 ms 26332 KB Output is correct
66 Correct 411 ms 27744 KB Output is correct
67 Correct 394 ms 26728 KB Output is correct
68 Correct 421 ms 28024 KB Output is correct
69 Correct 420 ms 29276 KB Output is correct
70 Correct 410 ms 27228 KB Output is correct
71 Correct 406 ms 27964 KB Output is correct
72 Correct 377 ms 30448 KB Output is correct
73 Correct 370 ms 33888 KB Output is correct
74 Correct 325 ms 34468 KB Output is correct