Submission #946852

#TimeUsernameProblemLanguageResultExecution timeMemory
946852panIslands (IOI08_islands)C++17
100 / 100
263 ms63120 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...