답안 #118350

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
118350 2019-06-18T17:11:00 Z eriksuenderhauf Islands (IOI08_islands) C++11
21 / 100
635 ms 82504 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e18 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int par[MAXN], w[MAXN], deg[MAXN];
vi adj[MAXN];
ll dp[MAXN], pre[MAXN], depth[MAXN];

ll diam(int u) {
	if (adj[u].size() < 2)
		return 0;
	ll mx1 = 0, mx2 = 0;
	for (int v: adj[u]) {
		if (mx1 < depth[v] + w[v]) {
			mx2 = mx1;
			mx1 = depth[v] + w[v];
		} else if (mx2 < depth[v] + w[v])
			mx2 = depth[v] + w[v];
	}
	return mx1 + mx2;
}

int main() {
	int n;
	ni(n);
	for (int i = 1; i <= n; i++) {
		scanf("%d %d", &par[i], &w[i]);
		deg[par[i]]++;
		adj[par[i]].pb(i);
	}
	deque<int> pq;
	for (int i = 1; i <= n; i++)
		if (deg[i] == 0)
			pq.pb(i);
	while (!pq.empty()) {
		int u = pq.front(); pq.pop_front();
		dp[u] = max(dp[u], diam(u));
		dp[par[u]] = max(dp[par[u]], dp[u]);
		depth[par[u]] = max(depth[par[u]], depth[u] + w[u]);
		if (--deg[par[u]] == 0)
			pq.pb(par[u]);
		par[u] = 0;
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		if (par[i] == 0)
			continue;
		ll sm = 0, m1 = -INF, m2 = -INF, cur = 0;
		vll act;
		act.pb(mp(0, 0));
		int u = i;
		while (par[u] != 0) {
			act.pb(mp(depth[u], w[u]));
			cur = max(cur, max(dp[u], diam(u)));
			sm += w[u];
			int nx = u;
			u = par[u];
			par[nx] = 0;
		}
		for (int i = 1; i < act.size(); i++)
			pre[i] = pre[i - 1] + act[i].se;
		for (int i = 1; i < act.size(); i++) {
			cur = max(cur, max(act[i].fi + pre[i - 1] + m1, act[i].fi + sm - pre[i - 1] + m2));
			m1 = max(m1, act[i].fi - pre[i - 1]);
			m2 = max(m2, act[i].fi + pre[i - 1]);
		}
		ans += cur;
	}
	prl(ans);
    return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:92:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 1; i < act.size(); i++)
                   ~~^~~~~~~~~~~~
islands.cpp:94:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 1; i < act.size(); i++) {
                   ~~^~~~~~~~~~~~
islands.cpp:14:37: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll {aka long long int}' [-Wformat=]
 #define prl(n) printf("%I64d\n", (n))
                                     ^
islands.cpp:101:2: note: in expansion of macro 'prl'
  prl(ans);
  ^~~
islands.cpp:9:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
islands.cpp:57:2: note: in expansion of macro 'ni'
  ni(n);
  ^~
islands.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &par[i], &w[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 23 ms 23808 KB Output is correct
3 Incorrect 22 ms 23936 KB Output isn't correct
4 Correct 21 ms 23808 KB Output is correct
5 Correct 21 ms 23808 KB Output is correct
6 Correct 21 ms 23808 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Incorrect 22 ms 23936 KB Output isn't correct
9 Incorrect 22 ms 23780 KB Output isn't correct
10 Correct 23 ms 23808 KB Output is correct
11 Incorrect 25 ms 23800 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 23936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 22 ms 23936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 28 ms 24696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 43 ms 26740 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 96 ms 33724 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 159 ms 43004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 287 ms 47480 KB Output is correct
2 Incorrect 635 ms 75280 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 354 ms 82504 KB Output isn't correct
2 Halted 0 ms 0 KB -