Submission #765175

#TimeUsernameProblemLanguageResultExecution timeMemory
765175vjudge1Roadside Advertisements (NOI17_roadsideadverts)C++14
100 / 100
140 ms20260 KiB
#include <bits/stdc++.h> using namespace std; // #pragma comment(linker, "/stack:2000000000") // #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll,ll> pll; #define precise(a) cout<<fixed<<setprecision(a) #define sz size() #define ff first #define ss second #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back // t1 const ll mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6 const ll MOD = 998244353; // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod const ll N = ll(1e5)+10; const ll inf = 1e9; const ld eps = 1e-15L; const ll B = 400; // const ld pie = acos(-1.0); ll lcm(ll a, ll b){ return (a / __gcd(a,b))*b; } ll binpow(ll a, ll b, ll m){ ll res=1;a%=m; while(b>0){ if(b&1)res=(res*a)%m; a=(a*a)%m; b/=2; } return res%m;} void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); } const int maxq = 10005; ll n, q, dis[N], ver[5], up[30][N], depth[N]; ll sum[N]; vector<pair<ll, ll> >g[N]; void dfs(int v, int p = -1) { if(p != -1) depth[v] = depth[p] + 1; up[0][v] = p; for(int j = 1; j<25; j++) up[j][v] = up[j-1][up[j-1][v]]; for(auto got : g[v]) { int to = got.ff, w = got.ss; if(to == p) continue; dis[to] = dis[v] + w; dfs(to, v); } } int lca(int u, int v) { if(depth[u] < depth[v]) swap(u, v); // u is more under than v int dif = depth[u] - depth[v]; for(int j = 25; j>=0; j--) { if(dif & (1<<j)) { u = up[j][u]; } } if(u == v) return u; for(int j = 25; j>=0; j--) { if(up[j][u] != up[j][v]) { u = up[j][u]; v = up[j][v]; } } return up[0][u]; } ll dist(int u, int v) { return dis[u] + dis[v] - 2 * dis[lca(u, v)]; } void Baizho() { cin>>n; for(int i = 1; i<=n-1; i++) { int u, v, w; cin>>u>>v>>w; g[u].pb({v, w}); g[v].pb({u, w}); } cin>>q; dfs(0); for(int i = 1; i<=q; i++) { vector<int> ver; for(int j = 0; j<5; j++) { int ai; cin>>ai; ver.pb(ai); } int siz = ver.sz; for(int j = 0; j<siz; j++) for(int k = 0; k<siz; k++) ver.pb(lca(ver[j], ver[k])); sort(all(ver)); ver.erase(unique(all(ver)), ver.end()); ll ans = 0, root = -1; vector<int> vec; for(int j = 0; j<ver.sz; j++) { int x = ver[j]; int depy = -inf, py = -1; for(int k = 0; k<ver.sz; k++) { if(j == k) continue; int y = ver[k]; if(lca(x, y) == y) { if(depth[y] > depy) { depy = depth[y]; py = y; } } } if(py == -1) { continue; } // cout<<x<<" "<<py<<endl; ans += dist(x, py); } cout<<ans<<"\n"; // cout<<endl; } } int main() { // Freopen("ladder"); ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); // precalc(); int ttt = 1; // cin>>ttt; for(int i=1; i<=ttt; i++) {Baizho(); } }

Compilation message (stderr)

roadsideadverts.cpp: In function 'void Baizho()':
roadsideadverts.cpp:98:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |   for(int j = 0; j<ver.sz; j++) {
      |                   ^
roadsideadverts.cpp:101:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |    for(int k = 0; k<ver.sz; k++) {
      |                    ^
roadsideadverts.cpp:96:15: warning: unused variable 'root' [-Wunused-variable]
   96 |   ll ans = 0, root = -1;
      |               ^~~~
roadsideadverts.cpp: In function 'void Freopen(std::string)':
roadsideadverts.cpp:33:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:33:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...