답안 #153056

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
153056 2019-09-11T20:06:46 Z luciocf Transport (COCI19_transport) C++14
0 / 130
526 ms 14056 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int maxn = 1e5+10;

int n;
ll ans;

int a[maxn];

int bit[maxn], id[maxn];

int sz[maxn];

bool mark[maxn];

vector<pii> grafo[maxn]; 
vector<pair<ll, int>> tot;

void upd(int x, int v)
{
	for (; x < maxn; x += (x&-x))
		bit[x] += v;
}

int soma(int x)
{
	int ans = 0;
	for (; x > 0; x -= (x&-x))
		ans += bit[x];
	return ans;
}

void dfs(int u, int p)
{
	sz[u] = 1;

	for (auto pp: grafo[u])
	{
		int v = pp.first, w = pp.second;
		if (v == p || mark[v]) continue;

		dfs(v, u);

		sz[u] += sz[v];
	}
}

int centroid(int u, int p, int S)
{
	for (auto pp: grafo[u])
	{
		int v = pp.first;
		if (v == p || mark[v]) continue;

		if (sz[v] > S/2) return centroid(v, u, S);
	}

	return u;
}

void dfs_up(int u, int p, ll cost)
{
	if (cost >= 0)
		tot.push_back({cost, u});

	for (auto pp: grafo[u])
	{
		int v = pp.first, w = pp.second;
		if (v == p || mark[v]) continue;

		dfs_up(v, u, min(cost+1ll*a[v]-1ll*w, 1ll*a[v]-1ll*w));
	}
}

void dfs_add(int u, int p, ll cost, int add)
{
	if (cost >= 0)
		upd(id[u], add);

	for (auto pp: grafo[u])
	{
		int v = pp.first, w = pp.second;
		if (v == p || mark[v]) continue;

		dfs_add(v, u, min(cost+1ll*a[v]-1ll*w, 1ll*a[v]-1ll*w), add);
	}
}

void dfs_down(int u, int p, ll cost)
{
	if (cost >= 0)
		ans++;

	int pos = (int) (lower_bound(tot.begin(), tot.end(), make_pair(-cost, -1))-tot.begin())+1;
	ans += 1ll*(soma(maxn-1)-soma(pos-1));

	for (auto pp: grafo[u])
	{
		int v = pp.first, w = pp.second;
		if (v == p || mark[v]) continue;

		dfs_down(v, u, min(cost, cost+1ll*a[u]-1ll*w));
	}
}

void decompose(int u)
{
	tot.clear();

	dfs(u, 0);

	int c = centroid(u, 0, sz[u]);
	mark[c] = 1;

	for (auto v: grafo[c])
		if (!mark[v.first])
			dfs_up(v.first, c, 1ll*(a[v.first]-v.second));

	sort(tot.begin(), tot.end());
	for (int i = 0; i < tot.size(); i++)
		id[tot[i].second] = i+1;

	ans += 1ll*tot.size();

	for (auto v: grafo[c])
		if (!mark[v.first])
			dfs_add(v.first, c, 1ll*(a[v.first]-v.second), 1);

	for (auto pp: grafo[c])
	{
		int v = pp.first, w = pp.second;
		if (mark[v]) continue;

		dfs_add(v, c, 1ll*(a[v]-w), -1);

		dfs_down(v, c, 1ll*(a[c]-w));

		dfs_add(v, c, 1ll*(a[v]-w), 1);
	}

	for (auto v: grafo[c])
		if (!mark[v.first])
			dfs_add(v.first, c, 1ll*(a[v.first]-v.second), -1);

	for (auto v: grafo[c])
		if (!mark[v.first])
			decompose(v.first);
}

int main(void)
{
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);

	for (int i = 1; i < n; i++)
	{
		int u, v, w;
		scanf("%d %d %d", &u, &v, &w);

		grafo[u].push_back({v, w});
		grafo[v].push_back({u, w});
	}

	decompose(1);

	printf("%lld\n", ans);
}

Compilation message

transport.cpp: In function 'void dfs(int, int)':
transport.cpp:44:21: warning: unused variable 'w' [-Wunused-variable]
   int v = pp.first, w = pp.second;
                     ^
transport.cpp: In function 'void decompose(int)':
transport.cpp:125:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < tot.size(); i++)
                  ~~^~~~~~~~~~~~
transport.cpp: In function 'int main()':
transport.cpp:157:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
transport.cpp:160:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
transport.cpp:165:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &u, &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 2936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 3320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 154 ms 9064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 185 ms 11132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 272 ms 14056 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 143 ms 5196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 105 ms 6772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 287 ms 7780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 374 ms 9076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 526 ms 11248 KB Output isn't correct
2 Halted 0 ms 0 KB -