Submission #765175

# Submission time Handle Problem Language Result Execution time Memory
765175 2023-06-24T09:04:39 Z vjudge1 Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
140 ms 20260 KB
#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

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 time Memory Grader output
1 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 18436 KB Output is correct
2 Correct 93 ms 20092 KB Output is correct
3 Correct 87 ms 20196 KB Output is correct
4 Correct 88 ms 20080 KB Output is correct
5 Correct 90 ms 20084 KB Output is correct
6 Correct 97 ms 20160 KB Output is correct
7 Correct 89 ms 20172 KB Output is correct
8 Correct 88 ms 20108 KB Output is correct
9 Correct 105 ms 20260 KB Output is correct
10 Correct 124 ms 20172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 16472 KB Output is correct
2 Correct 36 ms 18824 KB Output is correct
3 Correct 35 ms 18516 KB Output is correct
4 Correct 38 ms 18516 KB Output is correct
5 Correct 37 ms 18468 KB Output is correct
6 Correct 38 ms 18524 KB Output is correct
7 Correct 52 ms 18592 KB Output is correct
8 Correct 38 ms 18496 KB Output is correct
9 Correct 38 ms 18476 KB Output is correct
10 Correct 38 ms 18532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 120 ms 18436 KB Output is correct
3 Correct 93 ms 20092 KB Output is correct
4 Correct 87 ms 20196 KB Output is correct
5 Correct 88 ms 20080 KB Output is correct
6 Correct 90 ms 20084 KB Output is correct
7 Correct 97 ms 20160 KB Output is correct
8 Correct 89 ms 20172 KB Output is correct
9 Correct 88 ms 20108 KB Output is correct
10 Correct 105 ms 20260 KB Output is correct
11 Correct 124 ms 20172 KB Output is correct
12 Correct 30 ms 16472 KB Output is correct
13 Correct 36 ms 18824 KB Output is correct
14 Correct 35 ms 18516 KB Output is correct
15 Correct 38 ms 18516 KB Output is correct
16 Correct 37 ms 18468 KB Output is correct
17 Correct 38 ms 18524 KB Output is correct
18 Correct 52 ms 18592 KB Output is correct
19 Correct 38 ms 18496 KB Output is correct
20 Correct 38 ms 18476 KB Output is correct
21 Correct 38 ms 18532 KB Output is correct
22 Correct 120 ms 18420 KB Output is correct
23 Correct 120 ms 16932 KB Output is correct
24 Correct 113 ms 19844 KB Output is correct
25 Correct 101 ms 19604 KB Output is correct
26 Correct 140 ms 19652 KB Output is correct
27 Correct 101 ms 19644 KB Output is correct
28 Correct 100 ms 19612 KB Output is correct
29 Correct 114 ms 19840 KB Output is correct
30 Correct 107 ms 19608 KB Output is correct
31 Correct 106 ms 19684 KB Output is correct