Submission #244179

# Submission time Handle Problem Language Result Execution time Memory
244179 2020-07-02T17:50:36 Z AmineWeslati Roadside Advertisements (NOI17_roadsideadverts) C++14
7 / 100
1000 ms 9724 KB
//Never stop trying
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//s.order_of_key(), *s.find_by_order()

using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef string str;
typedef long long ll;
#define int ll
typedef double db;
typedef long double ld;

typedef pair<int,int> pi;
#define fi first
#define se second

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;

#define pb push_back
#define eb emplace_back
#define pf push_front

#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"

const int MOD = 1e9+7; //998244353
const ll INF = 1e18;
const int nx[4]={0,0,1,-1}, ny[4]={1,-1,0,0}; //right left down up

vi p(50000,-1);
vpi adj[50000];

void dfs(int u, int dest, int pr){
   if(u==dest) return;
   for(auto v: adj[u]) if(v.fi!=pr){
      p[v.fi]=u;
      dfs(v.fi,dest,u);
   }
}

int32_t main(){
   boost;
   int V; cin>>V;

   int s=0;
   for(int i=0; i<V-1; i++){
      int u,v,w; cin>>u>>v>>w;
      s+=w;
      adj[u].pb({v,w}),adj[v].pb({u,w});
   }

   int Q; cin>>Q;
	if(Q>100){
      vpi vec;
      int u;
      for(int i=0; i<V; i++) if(sz(adj[i])==1){u=i; break;}
      vec.pb({u,0});
      int p=-1;
      while(1){
         for(auto v: adj[u]) if(v.fi!=p){
            vec.pb(v);
            p=u;
            u=v.fi;
         }
         if(sz(adj[u])==1) break;
      }
      int t[V];
      for(int i=0; i<V; i++){
         t[vec[i].fi]=i;
         if(i) vec[i].se+=vec[i-1].se;
      }
      while(Q--){
         int tab[5];
         for(int i=0; i<5;i++) cin>>tab[i];
         if(V==5){
            cout << s << endl; continue;
         }
         sort(tab,tab+5);
         int l=tab[0], r=tab[4];
         cout << vec[t[r]].se-vec[t[l]].se << endl;
      }
	}
	else{
      while(Q--){
         set <pair<pi,int>> s;
         int t[5];
         for(int i=0; i<5; i++) cin>>t[i];
         for(int i=0; i<5; i++) for(int j=i+1; j<5; j++){
            dfs(t[i],t[j],-1);
            int u=t[j];
            while(t[i]!=u){
               for(auto x: adj[u]) if(x.fi==p[u]) {s.insert({{u,p[u]},x.se}); s.insert({{p[u],u},x.se});}
               u=p[u];
            }
         }
         int sum=0;
         for(auto x: s) sum+=x.se;
         //cout << sum << endl;
         cout << sum/2 << endl;

      }
	}

   return 0;
}

Compilation message

roadsideadverts.cpp: In function 'int32_t main()':
roadsideadverts.cpp:72:11: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
       int u;
           ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 9724 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1084 ms 5240 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1920 KB Output is correct
2 Runtime error 29 ms 9724 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -