Submission #200346

# Submission time Handle Problem Language Result Execution time Memory
200346 2020-02-06T11:31:11 Z arnold518 Designated Cities (JOI19_designated_cities) C++14
7 / 100
839 ms 63096 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);
		nxt=now;
	}
	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;
	for(i=1; i<=N; i++) if(adj[i].size()==1 && leaf[i].size()==0)
	{
		pll t=propa(i);
		leaf[t.first].insert(i);
		dist[i]=t.second;
		pos[i]=t.first;
		PQ.insert({dist[i], i});
		lf++;
	}

	ll cost=0;
	for(i=lf-1; i>=2; i--)
	{
		pll now=*PQ.begin(); PQ.erase(PQ.begin());
		cost+=now.first;
		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(u);
			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:53:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
designated_cities.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:59: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:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:102: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 Incorrect 18 ms 19192 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 19156 KB Output is correct
2 Correct 775 ms 52968 KB Output is correct
3 Correct 550 ms 59676 KB Output is correct
4 Correct 687 ms 51504 KB Output is correct
5 Correct 779 ms 53860 KB Output is correct
6 Correct 704 ms 55120 KB Output is correct
7 Correct 763 ms 55672 KB Output is correct
8 Correct 503 ms 63096 KB Output is correct
9 Correct 839 ms 57848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Incorrect 624 ms 53088 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Incorrect 18 ms 19192 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 19156 KB Output is correct
2 Correct 775 ms 52968 KB Output is correct
3 Correct 550 ms 59676 KB Output is correct
4 Correct 687 ms 51504 KB Output is correct
5 Correct 779 ms 53860 KB Output is correct
6 Correct 704 ms 55120 KB Output is correct
7 Correct 763 ms 55672 KB Output is correct
8 Correct 503 ms 63096 KB Output is correct
9 Correct 839 ms 57848 KB Output is correct
10 Correct 17 ms 19064 KB Output is correct
11 Incorrect 624 ms 53088 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Incorrect 18 ms 19192 KB Output isn't correct
3 Halted 0 ms 0 KB -