Submission #946852

# Submission time Handle Problem Language Result Execution time Memory
946852 2024-03-15T06:33:44 Z pan Islands (IOI08_islands) C++17
100 / 100
263 ms 63120 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
// IDEA:
// n vertice and n edges not n-1 (fuk)
// special graph is tree with loop (fagain!)
// q. ask for sum of all tree's diameter
// visually, consider loop as centre and subtree under each vertice loop
// diameter is max(dp[i] for all vertice, dp[i] + dp[j] + max(loop from i to j) where i, j in loop)
// The rest is implementation
ll const INF = 1e13;
ll n, a, b, ans = 0;
ll par[1000005], len[1000005], degree[1000005], dia[1000005], line[1000005];
// dia[x] - longest diameter if lca is in x subtree
// line[x] - longest line from x to any leaf

void bfs(ll from)
{
	//show(from);
	ll cw = line[from], acw = line[from], anscw = -INF, ansacw = -INF, sum = 0, now = from, maxx = dia[from];
	degree[from] = 0;
	while (par[now]!=from)
	{
		//show2(par[now], from);
		sum+= len[now];
		now = par[now];
		degree[now] = 0;
		anscw = max(anscw, cw + sum + line[now]);
		ansacw = max(ansacw, acw -sum + line[now]);
		cw = max(cw, line[now]-sum);
		acw = max(acw, line[now]+sum);
		maxx = max(maxx, dia[now]);
		//show2(anscw,ansacw);
	}
	sum += len[now];
	ans += max(maxx, max(anscw, ansacw + sum));
	
	
}
int main()
{
	input(n);
	fill(degree+1, degree+1+n, 0);
	fill(dia+1, dia+1+n, 0);
	fill(line+1, line+1+n, 0);
	for (ll i=1; i<=n; ++i)
	{
		input2(par[i],len[i]);
		degree[par[i]]++;
	}
	deque<ll> leaf;
	for (ll i=1; i<=n; ++i) if (degree[i]==0) leaf.pb(i);
	// Process all subtree of vertice in loop
	while (leaf.size())
	{
		ll now = leaf.front(), parent = par[now], dis = len[now];
		leaf.pop_front();
		ll newline = line[now]+dis;
		ll newdia = max(line[parent] + newline, dia[now]);
		line[parent] = max(line[parent], newline);
		dia[parent] = max(dia[parent], newdia);
		degree[parent]--;
		if (degree[parent]==0) leaf.pb(parent);
	}
	//or (ll i=1; i<=n; ++i) show2(dia[i], line[i]);
	// Leftovers with degree[x]>0 is in loop
	for (ll i=1; i<=n; ++i) if (degree[i]) {bfs(i);}
	print(ans, '\n');

	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define input(x) scanf("%lld", &x);
      |                  ~~~~~^~~~~~~~~~~~
islands.cpp:68:2: note: in expansion of macro 'input'
   68 |  input(n);
      |  ^~~~~
islands.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~
islands.cpp:74:3: note: in expansion of macro 'input2'
   74 |   input2(par[i],len[i]);
      |   ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8636 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8796 KB Output is correct
2 Correct 8 ms 11408 KB Output is correct
3 Correct 5 ms 9052 KB Output is correct
4 Correct 3 ms 8844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11608 KB Output is correct
2 Correct 16 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 20048 KB Output is correct
2 Correct 34 ms 21228 KB Output is correct
3 Correct 46 ms 23528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 31572 KB Output is correct
2 Correct 77 ms 36976 KB Output is correct
3 Correct 84 ms 38996 KB Output is correct
4 Correct 101 ms 44676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 63120 KB Output is correct
2 Correct 170 ms 58960 KB Output is correct
3 Correct 121 ms 51284 KB Output is correct
4 Correct 153 ms 55632 KB Output is correct
5 Correct 158 ms 55696 KB Output is correct
6 Correct 263 ms 57028 KB Output is correct
7 Correct 157 ms 57940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 54864 KB Output is correct
2 Correct 152 ms 54820 KB Output is correct
3 Correct 151 ms 54868 KB Output is correct
4 Correct 153 ms 54368 KB Output is correct
5 Correct 154 ms 56840 KB Output is correct
6 Correct 165 ms 54608 KB Output is correct
7 Correct 245 ms 57224 KB Output is correct