Submission #382178

# Submission time Handle Problem Language Result Execution time Memory
382178 2021-03-26T16:16:13 Z mohamedsobhi777 Cheap flights (LMIO18_pigus_skrydziai) C++14
0 / 100
3000 ms 89444 KB
#include <bits/stdc++.h>

using namespace std;

#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define loop(_) for (int __ = 0; __ < (_); ++__)
#define pb push_back
#define f first
#define s second
#define sz(_) ((int)_.size())
#define all(_) _.begin(), _.end()
#define lb lower_bound
#define ub upper_bound

using ll = long long;
using ld = long double;

const int N = 3e5 + 7;
const ll mod = 1e9 + 7;

int n, m;
ll a[N];
int viz[N], col = 1;
ll ans;
map<pii, ll> cost;
vi adj[N];
vi adj2[N];
vii e, e2;
ll disto[N];
int fat[N];

void dfs(int x, int p = 0, int pp = 0)
{
       viz[x] = col;
       fat[x] = p;
       for (auto u : adj[x])
       {
              if (u == p)
                     continue;
              if (viz[u] != col)
              {
                     dfs(u, x, p);
              }
              else
              {
                     e2.pb({u, x});
                     if (p && pp && u == pp)
                     {
                            ans = max(ans, cost[{p, pp}] + cost[{x, p}] + cost[{x, pp}]);
                     }
                     adj2[x].pb(u);
              }
       }
}

void dfz(int x, int p)
{
       if (p)
       {
              for (auto u : adj2[x])
              {
                     ans = max(ans, cost[{x, p}] + disto[u] + cost[{x, u}]);
              }
              for (auto u : adj2[p])
              {
                     disto[u] = -2e17;
              }
       }
       for (auto u : adj2[x])
       {
              disto[u] = cost[{x, u}];
       }

       for (auto u : adj[x])
       {
              if (fat[u] == x)
                     dfz(u, x);
       }
}

void prep(vi &act, vii &e)
{
       act.clear();
       for (auto u : e)
       {
              act.pb(u.f);
              act.pb(u.s);
       }
       for (auto u : act)
       {
              adj[u].clear();
              adj2[u].clear();
       }
       for (auto u : e)
       {
              adj[u.f].pb(u.s);
              adj[u.s].pb(u.f);
       }
}

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
#endif
       cin >> n >> m;
       for (int i = 0; i < N; ++i)
              disto[i] = -2e17;

       vi act;

       for (int i = 0; i < m; ++i)
       {
              int u, v, w;
              cin >> u >> v >> w;
              a[u] += w;
              a[v] += w;
              ans = max({ans, a[u], a[v]});
              e.pb({u, v});
              cost[{u, v}] = cost[{v, u}] = w;
       }

       while (sz(e))
       {
              prep(act, e);
              for (auto u : act)
              {
                     if (viz[u] != col)
                     {
                            dfs(u, 0, 0);
                            dfz(u, 0);
                     }
              }
              e = e2;
              e2.clear();
              ++col;
       }

       cout << ans;
       return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 16748 KB Output is correct
2 Correct 10 ms 16748 KB Output is correct
3 Correct 11 ms 16748 KB Output is correct
4 Correct 11 ms 16748 KB Output is correct
5 Correct 11 ms 16748 KB Output is correct
6 Execution timed out 3066 ms 21788 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 16748 KB Output is correct
2 Correct 10 ms 16748 KB Output is correct
3 Correct 11 ms 16748 KB Output is correct
4 Correct 11 ms 16748 KB Output is correct
5 Correct 11 ms 16748 KB Output is correct
6 Execution timed out 3066 ms 21788 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 314 ms 52144 KB Output is correct
2 Correct 961 ms 78032 KB Output is correct
3 Correct 237 ms 37220 KB Output is correct
4 Correct 390 ms 55008 KB Output is correct
5 Execution timed out 3052 ms 89444 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 314 ms 52144 KB Output is correct
2 Correct 961 ms 78032 KB Output is correct
3 Correct 237 ms 37220 KB Output is correct
4 Correct 390 ms 55008 KB Output is correct
5 Execution timed out 3052 ms 89444 KB Time limit exceeded
6 Halted 0 ms 0 KB -