Submission #200378

# Submission time Handle Problem Language Result Execution time Memory
200378 2020-02-06T13:01:30 Z arnold518 Designated Cities (JOI19_designated_cities) C++14
100 / 100
1020 ms 62692 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int MAXN = 2e5;
 
int N, Q, lf;
map<int, int> adj[MAXN+10];
set<int> leaf[MAXN+10];
 
int pos[MAXN+10];
ll dist[MAXN+10], ans[MAXN+10];
 
void dfs(int now, int bef)
{
	for(auto nxt : adj[now])
	{
		if(nxt.first==bef) continue;
		ans[1]+=nxt.second;
		dfs(nxt.first, now);
	}
}
 
void dfs2(int now, int bef, ll val)
{
	ans[1]=min(ans[1], val);
	for(auto nxt : adj[now])
	{
		if(nxt.first==bef) continue;
		dfs2(nxt.first, now, val-adj[now][nxt.first]+adj[nxt.first][now]);
	}
}
 
pll propa(int u)
{
	int now=u, nxt; ll ret=0;
	while(adj[now].size()==1 && leaf[now].size()==0)
	{
		nxt=adj[now].begin()->first;
		ret+=adj[nxt][now];
		adj[now].erase(nxt); adj[nxt].erase(now);
		now=nxt;
	}
	return {now, ret};
}
 
int main()
{
	int i, j;
 
	scanf("%d", &N);
	for(i=1; i<N; i++)
	{
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		adj[a][b]=c;
		adj[b][a]=d;
	}
 
	dfs2(1, 1, 0);
	dfs(1, 1);
 
	set<pll> PQ;
	vector<int> V;
	for(i=1; i<=N; i++) if(adj[i].size()==1) V.push_back(i);
	for(auto i : V)
	{
		pll t=propa(i);
		leaf[t.first].insert(i);
		dist[i]=t.second;
		pos[i]=t.first;
		PQ.insert({dist[i], i});
		//printf("%d %lld\n", i, dist[i]);
		lf++;
	}
 
	ll cost=0;
	for(i=lf-1; i>=2; i--)
	{
		pll now=*PQ.begin(); PQ.erase(PQ.begin());
		cost+=now.first;
		//printf("!%lld %lld\n", now.first, now.second);
		assert(adj[now.second].size()==0);
		leaf[pos[now.second]].erase(now.second);
		if(leaf[pos[now.second]].size()==1)
		{
			int u=*leaf[pos[now.second]].begin();
			leaf[pos[now.second]].erase(leaf[pos[now.second]].begin());
			PQ.erase({dist[u], u});
			pll t=propa(pos[now.second]);
			leaf[t.first].insert(u);
			dist[u]+=t.second;
			pos[u]=t.first;
			PQ.insert({dist[u], u});
		}
		ans[i]=cost;
	}
 
	scanf("%d", &Q);
	while(Q--)
	{
		int p;
		scanf("%d", &p);
		printf("%lld\n", ans[p]);
	}
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:52:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
designated_cities.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &a, &b, &c, &d);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:102:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:106:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &p);
   ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 18 ms 19064 KB Output is correct
3 Correct 18 ms 19192 KB Output is correct
4 Correct 17 ms 19064 KB Output is correct
5 Correct 17 ms 19192 KB Output is correct
6 Correct 17 ms 19192 KB Output is correct
7 Correct 18 ms 19068 KB Output is correct
8 Correct 17 ms 19064 KB Output is correct
9 Correct 17 ms 19064 KB Output is correct
10 Correct 17 ms 19192 KB Output is correct
11 Correct 17 ms 19064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 722 ms 42220 KB Output is correct
3 Correct 503 ms 53240 KB Output is correct
4 Correct 673 ms 42048 KB Output is correct
5 Correct 809 ms 42280 KB Output is correct
6 Correct 723 ms 44396 KB Output is correct
7 Correct 894 ms 43628 KB Output is correct
8 Correct 523 ms 56824 KB Output is correct
9 Correct 969 ms 52048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 733 ms 42348 KB Output is correct
3 Correct 504 ms 62072 KB Output is correct
4 Correct 641 ms 47212 KB Output is correct
5 Correct 793 ms 48748 KB Output is correct
6 Correct 718 ms 51200 KB Output is correct
7 Correct 983 ms 55872 KB Output is correct
8 Correct 614 ms 58992 KB Output is correct
9 Correct 992 ms 58096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 18 ms 19064 KB Output is correct
3 Correct 18 ms 19192 KB Output is correct
4 Correct 17 ms 19064 KB Output is correct
5 Correct 17 ms 19192 KB Output is correct
6 Correct 17 ms 19192 KB Output is correct
7 Correct 18 ms 19068 KB Output is correct
8 Correct 17 ms 19064 KB Output is correct
9 Correct 17 ms 19064 KB Output is correct
10 Correct 17 ms 19192 KB Output is correct
11 Correct 17 ms 19064 KB Output is correct
12 Correct 18 ms 19192 KB Output is correct
13 Correct 21 ms 19448 KB Output is correct
14 Correct 20 ms 19448 KB Output is correct
15 Correct 21 ms 19448 KB Output is correct
16 Correct 21 ms 19448 KB Output is correct
17 Correct 20 ms 19448 KB Output is correct
18 Correct 20 ms 19448 KB Output is correct
19 Correct 21 ms 19448 KB Output is correct
20 Correct 20 ms 19448 KB Output is correct
21 Correct 21 ms 19576 KB Output is correct
22 Correct 20 ms 19448 KB Output is correct
23 Correct 21 ms 19576 KB Output is correct
24 Correct 22 ms 19576 KB Output is correct
25 Correct 20 ms 19576 KB Output is correct
26 Correct 21 ms 19576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 722 ms 42220 KB Output is correct
3 Correct 503 ms 53240 KB Output is correct
4 Correct 673 ms 42048 KB Output is correct
5 Correct 809 ms 42280 KB Output is correct
6 Correct 723 ms 44396 KB Output is correct
7 Correct 894 ms 43628 KB Output is correct
8 Correct 523 ms 56824 KB Output is correct
9 Correct 969 ms 52048 KB Output is correct
10 Correct 17 ms 19064 KB Output is correct
11 Correct 733 ms 42348 KB Output is correct
12 Correct 504 ms 62072 KB Output is correct
13 Correct 641 ms 47212 KB Output is correct
14 Correct 793 ms 48748 KB Output is correct
15 Correct 718 ms 51200 KB Output is correct
16 Correct 983 ms 55872 KB Output is correct
17 Correct 614 ms 58992 KB Output is correct
18 Correct 992 ms 58096 KB Output is correct
19 Correct 17 ms 19064 KB Output is correct
20 Correct 777 ms 48688 KB Output is correct
21 Correct 525 ms 62328 KB Output is correct
22 Correct 658 ms 47340 KB Output is correct
23 Correct 724 ms 48708 KB Output is correct
24 Correct 710 ms 47596 KB Output is correct
25 Correct 722 ms 48492 KB Output is correct
26 Correct 704 ms 47720 KB Output is correct
27 Correct 810 ms 48876 KB Output is correct
28 Correct 723 ms 50796 KB Output is correct
29 Correct 711 ms 47980 KB Output is correct
30 Correct 736 ms 49384 KB Output is correct
31 Correct 920 ms 51304 KB Output is correct
32 Correct 605 ms 59836 KB Output is correct
33 Correct 963 ms 58596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 18 ms 19064 KB Output is correct
3 Correct 18 ms 19192 KB Output is correct
4 Correct 17 ms 19064 KB Output is correct
5 Correct 17 ms 19192 KB Output is correct
6 Correct 17 ms 19192 KB Output is correct
7 Correct 18 ms 19068 KB Output is correct
8 Correct 17 ms 19064 KB Output is correct
9 Correct 17 ms 19064 KB Output is correct
10 Correct 17 ms 19192 KB Output is correct
11 Correct 17 ms 19064 KB Output is correct
12 Correct 17 ms 19064 KB Output is correct
13 Correct 722 ms 42220 KB Output is correct
14 Correct 503 ms 53240 KB Output is correct
15 Correct 673 ms 42048 KB Output is correct
16 Correct 809 ms 42280 KB Output is correct
17 Correct 723 ms 44396 KB Output is correct
18 Correct 894 ms 43628 KB Output is correct
19 Correct 523 ms 56824 KB Output is correct
20 Correct 969 ms 52048 KB Output is correct
21 Correct 17 ms 19064 KB Output is correct
22 Correct 733 ms 42348 KB Output is correct
23 Correct 504 ms 62072 KB Output is correct
24 Correct 641 ms 47212 KB Output is correct
25 Correct 793 ms 48748 KB Output is correct
26 Correct 718 ms 51200 KB Output is correct
27 Correct 983 ms 55872 KB Output is correct
28 Correct 614 ms 58992 KB Output is correct
29 Correct 992 ms 58096 KB Output is correct
30 Correct 18 ms 19192 KB Output is correct
31 Correct 21 ms 19448 KB Output is correct
32 Correct 20 ms 19448 KB Output is correct
33 Correct 21 ms 19448 KB Output is correct
34 Correct 21 ms 19448 KB Output is correct
35 Correct 20 ms 19448 KB Output is correct
36 Correct 20 ms 19448 KB Output is correct
37 Correct 21 ms 19448 KB Output is correct
38 Correct 20 ms 19448 KB Output is correct
39 Correct 21 ms 19576 KB Output is correct
40 Correct 20 ms 19448 KB Output is correct
41 Correct 21 ms 19576 KB Output is correct
42 Correct 22 ms 19576 KB Output is correct
43 Correct 20 ms 19576 KB Output is correct
44 Correct 21 ms 19576 KB Output is correct
45 Correct 17 ms 19064 KB Output is correct
46 Correct 777 ms 48688 KB Output is correct
47 Correct 525 ms 62328 KB Output is correct
48 Correct 658 ms 47340 KB Output is correct
49 Correct 724 ms 48708 KB Output is correct
50 Correct 710 ms 47596 KB Output is correct
51 Correct 722 ms 48492 KB Output is correct
52 Correct 704 ms 47720 KB Output is correct
53 Correct 810 ms 48876 KB Output is correct
54 Correct 723 ms 50796 KB Output is correct
55 Correct 711 ms 47980 KB Output is correct
56 Correct 736 ms 49384 KB Output is correct
57 Correct 920 ms 51304 KB Output is correct
58 Correct 605 ms 59836 KB Output is correct
59 Correct 963 ms 58596 KB Output is correct
60 Correct 17 ms 19064 KB Output is correct
61 Correct 770 ms 51340 KB Output is correct
62 Correct 567 ms 61304 KB Output is correct
63 Correct 707 ms 49772 KB Output is correct
64 Correct 787 ms 51308 KB Output is correct
65 Correct 767 ms 49900 KB Output is correct
66 Correct 766 ms 51052 KB Output is correct
67 Correct 776 ms 50084 KB Output is correct
68 Correct 853 ms 51436 KB Output is correct
69 Correct 763 ms 52968 KB Output is correct
70 Correct 746 ms 50284 KB Output is correct
71 Correct 803 ms 52332 KB Output is correct
72 Correct 974 ms 53736 KB Output is correct
73 Correct 668 ms 60528 KB Output is correct
74 Correct 1020 ms 62692 KB Output is correct