답안 #200362

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
200362 2020-02-06T11:51:10 Z arnold518 Designated Cities (JOI19_designated_cities) C++14
7 / 100
948 ms 56696 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);
		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: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);
   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 16 ms 19064 KB Output is correct
3 Correct 21 ms 19064 KB Output is correct
4 Incorrect 17 ms 19064 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 19064 KB Output is correct
2 Correct 622 ms 42240 KB Output is correct
3 Correct 509 ms 53240 KB Output is correct
4 Correct 557 ms 42220 KB Output is correct
5 Correct 688 ms 42476 KB Output is correct
6 Correct 639 ms 44360 KB Output is correct
7 Correct 815 ms 43628 KB Output is correct
8 Correct 461 ms 56696 KB Output is correct
9 Correct 948 ms 52200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 19196 KB Output is correct
2 Incorrect 618 ms 42308 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 16 ms 19064 KB Output is correct
3 Correct 21 ms 19064 KB Output is correct
4 Incorrect 17 ms 19064 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 19064 KB Output is correct
2 Correct 622 ms 42240 KB Output is correct
3 Correct 509 ms 53240 KB Output is correct
4 Correct 557 ms 42220 KB Output is correct
5 Correct 688 ms 42476 KB Output is correct
6 Correct 639 ms 44360 KB Output is correct
7 Correct 815 ms 43628 KB Output is correct
8 Correct 461 ms 56696 KB Output is correct
9 Correct 948 ms 52200 KB Output is correct
10 Correct 16 ms 19196 KB Output is correct
11 Incorrect 618 ms 42308 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 16 ms 19064 KB Output is correct
3 Correct 21 ms 19064 KB Output is correct
4 Incorrect 17 ms 19064 KB Output isn't correct
5 Halted 0 ms 0 KB -