답안 #253210

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253210 2020-07-27T12:13:22 Z nandonathaniel Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
232 ms 10372 KB
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=5e4+5,LOG=16;
int depth[MAXN],pa[LOG][MAXN],a[5],par[MAXN],pref[MAXN];
vector<pii> adj[MAXN];

int find(int a){
	if(par[a]==a)return par[a];
	return par[a]=find(par[a]);
}

void join(int a,int b){
	int ortua=find(a);
	int ortub=find(b);
	par[ortub]=ortua;
}

void dfs(int now,int par){
	for(auto nxt : adj[now]){
		if(nxt.first==par)continue;
		pa[0][nxt.first]=now;
		for(int i=1;i<LOG;i++)pa[i][nxt.first]=pa[i-1][pa[i-1][nxt.first]];
		pref[nxt.first]=pref[now]+nxt.second;
		depth[nxt.first]=depth[now]+1;
		dfs(nxt.first,now);
	}
}

int LCA(int u,int v){
	if(depth[u]<depth[v])swap(u,v);
	int diff=depth[u]-depth[v];
	for(int i=LOG-1;i>=0;i--){
		if(diff&(1<<i))u=pa[i][u];
	}
	if(u==v)return u;
	for(int i=LOG-1;i>=0;i--){
		if(pa[i][u]!=pa[i][v]){
			u=pa[i][u];
			v=pa[i][v];
		}
	}
	return pa[0][u];
}

int dist(int u,int v){
	return pref[u]+pref[v]-2*pref[LCA(u,v)];
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int n,q,u,v,w;
	cin >> n;
	for(int i=1;i<n;i++){
		cin >> u >> v >> w;
		u++;v++;
		adj[u].push_back({v,w});
		adj[v].push_back({u,w});
	}
	dfs(1,-1);
	cin >> q;
	while(q--){
		vector<int> node;
		vector<pair<int,pii> > edge;
		for(int i=0;i<5;i++){
			cin >> a[i];
			a[i]++;
			node.push_back(a[i]);
			par[a[i]]=a[i];
		}
		for(int i=0;i<5;i++){
			for(int j=i+1;j<5;j++){
				int b=LCA(a[i],a[j]);
				node.push_back(b);
				par[b]=b;
			}
		}
		for(int i=0;i<node.size();i++){
			for(int j=i+1;j<node.size();j++){
				edge.push_back({dist(node[i],node[j]),{node[i],node[j]}});
			}
		}
		sort(edge.begin(),edge.end());
		int mstcost=0;
		for(auto isi : edge){
			int ortua=find(isi.second.first),ortub=find(isi.second.second);
			if(ortua!=ortub){
				join(ortua,ortub);
				mstcost+=isi.first;
			}
		}
		cout << mstcost << '\n';
	}
	return 0;
}

Compilation message

roadsideadverts.cpp: In function 'int main()':
roadsideadverts.cpp:78:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<node.size();i++){
               ~^~~~~~~~~~~~
roadsideadverts.cpp:79:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j=i+1;j<node.size();j++){
                  ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 232 ms 9268 KB Output is correct
2 Correct 217 ms 10232 KB Output is correct
3 Correct 211 ms 10232 KB Output is correct
4 Correct 218 ms 10232 KB Output is correct
5 Correct 221 ms 10232 KB Output is correct
6 Correct 207 ms 10232 KB Output is correct
7 Correct 213 ms 10232 KB Output is correct
8 Correct 210 ms 10360 KB Output is correct
9 Correct 214 ms 10372 KB Output is correct
10 Correct 225 ms 10340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 7800 KB Output is correct
2 Correct 46 ms 9592 KB Output is correct
3 Correct 66 ms 9592 KB Output is correct
4 Correct 43 ms 9592 KB Output is correct
5 Correct 56 ms 9592 KB Output is correct
6 Correct 57 ms 9592 KB Output is correct
7 Correct 45 ms 9592 KB Output is correct
8 Correct 37 ms 9592 KB Output is correct
9 Correct 38 ms 9592 KB Output is correct
10 Correct 39 ms 9592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1536 KB Output is correct
2 Correct 232 ms 9268 KB Output is correct
3 Correct 217 ms 10232 KB Output is correct
4 Correct 211 ms 10232 KB Output is correct
5 Correct 218 ms 10232 KB Output is correct
6 Correct 221 ms 10232 KB Output is correct
7 Correct 207 ms 10232 KB Output is correct
8 Correct 213 ms 10232 KB Output is correct
9 Correct 210 ms 10360 KB Output is correct
10 Correct 214 ms 10372 KB Output is correct
11 Correct 225 ms 10340 KB Output is correct
12 Correct 40 ms 7800 KB Output is correct
13 Correct 46 ms 9592 KB Output is correct
14 Correct 66 ms 9592 KB Output is correct
15 Correct 43 ms 9592 KB Output is correct
16 Correct 56 ms 9592 KB Output is correct
17 Correct 57 ms 9592 KB Output is correct
18 Correct 45 ms 9592 KB Output is correct
19 Correct 37 ms 9592 KB Output is correct
20 Correct 38 ms 9592 KB Output is correct
21 Correct 39 ms 9592 KB Output is correct
22 Correct 231 ms 9236 KB Output is correct
23 Correct 171 ms 8184 KB Output is correct
24 Correct 213 ms 9976 KB Output is correct
25 Correct 216 ms 9976 KB Output is correct
26 Correct 212 ms 9976 KB Output is correct
27 Correct 209 ms 9976 KB Output is correct
28 Correct 214 ms 10104 KB Output is correct
29 Correct 210 ms 9976 KB Output is correct
30 Correct 210 ms 9976 KB Output is correct
31 Correct 217 ms 10160 KB Output is correct