답안 #41547

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
41547 2018-02-18T18:17:04 Z IvanC Evacuation plan (IZhO18_plan) C++14
0 / 100
292 ms 35216 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> trinca;
const int INF = 1e9;
const int MAXN = 1e5 + 10;
const int MAXL = 17;
vector<ii> grafo[MAXN],adj[MAXN],arestas;
vector<trinca> Kruskal;
priority_queue<ii, vector<ii>, greater<ii> > pq;
int ancestral[MAXN][MAXL],liga[MAXN][MAXL],nivel[MAXN],processado[MAXN],dist[MAXN],n,m,dsu_parent[MAXN],dsu_weight[MAXN];
int find(int x){
	if(x == dsu_parent[x]) return x;
	return dsu_parent[x] = find(dsu_parent[x]);
}
void join(int x,int y){
	x = find(x);
	y = find(y);
	if(dsu_weight[x] < dsu_weight[y]) swap(x,y);
	dsu_parent[y] = x;
	dsu_weight[x] += dsu_weight[y];
}
void dfs(int v,int p){
	for(int i = 1;i<MAXL;i++){
		int z = ancestral[v][i-1];
		if(z != -1){
			ancestral[v][i] = ancestral[z][i-1];
			liga[v][i] = min(liga[v][i-1],liga[z][i-1]); 
		}
	}
	for(int i = 0;i<adj[v].size();i++){
		int u = adj[v][i].first, w = adj[v][i].second;
		if(u == p) continue;
		nivel[u] = nivel[v] + 1;
		ancestral[u][0] = v;
		liga[u][0] = w;
		dfs(u,v);
	}
}
int LCA(int u,int v){
	int ans = INF;
	if(nivel[u] < nivel[v]) swap(u,v);
	for(int i = MAXL - 1;i>=0;i--){
		int z = ancestral[u][i],w = liga[u][i];
		if(z != -1 && nivel[z] >= nivel[v]){
			ans = min(ans,w);
			u = z;
		}
	}
	if(u == v) return ans;
	for(int i = MAXL - 1;i>=0;i--){
		if(ancestral[u][i] != ancestral[v][i] && ancestral[u][i] != -1 && ancestral[v][i] != -1){
			ans = min(ans,liga[u][i]);
			u = ancestral[u][i];
			v = min(ans,liga[v][i]);
			v = ancestral[v][i];
		}
	}
	ans = min(ans, min(liga[v][0],liga[u][0]) );
	return ans;
}
int main(){
	memset(ancestral,-1,sizeof(ancestral));
	scanf("%d %d",&n,&m);
	for(int i = 1;i<=n;i++){
		dsu_parent[i] = i;
		dsu_weight[i] = 1;
	}
	for(int i = 1;i<=m;i++){
		int u,v,w;
		scanf("%d %d %d",&u,&v,&w);
		grafo[u].push_back(ii(v,w));
		grafo[v].push_back(ii(u,w));
		arestas.push_back(ii(u,v));
	}
	int q;
	scanf("%d",&q);
	for(int i = 1;i<=q;i++){
		int x;
		scanf("%d",&x);
		pq.push(ii(0,x));
	}
	while(!pq.empty()){
		int d = pq.top().first, v = pq.top().second;
		pq.pop();
		if(processado[v]) continue;
		processado[v] = 1;
		dist[v] = d;
		for(int i = 0;i<grafo[v].size();i++){
			int u = grafo[v][i].first, w = grafo[v][i].second;
			if(!processado[u]) pq.push(ii(d+w,u));
		}
	}
	for(int i = 0;i<arestas.size();i++){
		int u = arestas[i].first, v = arestas[i].second;
		Kruskal.push_back(make_tuple(min(dist[u],dist[v]),u,v));
	}
	sort(Kruskal.begin(),Kruskal.end());
	for(int i = (int)Kruskal.size()-1;i>=0;i--){
		int u = get<1>(Kruskal[i]) , v = get<2>(Kruskal[i]), w = get<0>(Kruskal[i]);
		if(find(u) != find(v)){
			join(u,v);
			adj[u].push_back(ii(v,w));
			adj[v].push_back(ii(u,w));
		}
	}
	dfs(1,-1);
	scanf("%d",&q);
	while(q--){
		int u,v;
		scanf("%d %d",&u,&v);
		printf("%d\n",LCA(u,v));
	}
	return 0;
}

Compilation message

plan.cpp: In function 'void dfs(int, int)':
plan.cpp:31:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0;i<adj[v].size();i++){
                ~^~~~~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:89:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0;i<grafo[v].size();i++){
                 ~^~~~~~~~~~~~~~~~
plan.cpp:94:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0;i<arestas.size();i++){
                ~^~~~~~~~~~~~~~~
plan.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
plan.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&u,&v,&w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~
plan.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&q);
  ~~~~~^~~~~~~~~
plan.cpp:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&x);
   ~~~~~^~~~~~~~~
plan.cpp:108:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&q);
  ~~~~~^~~~~~~~~
plan.cpp:111:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u,&v);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 11668 KB Output is correct
2 Correct 13 ms 11896 KB Output is correct
3 Correct 13 ms 11908 KB Output is correct
4 Correct 12 ms 11640 KB Output is correct
5 Correct 13 ms 11912 KB Output is correct
6 Correct 13 ms 11896 KB Output is correct
7 Incorrect 11 ms 11640 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 11668 KB Output is correct
2 Correct 13 ms 11896 KB Output is correct
3 Correct 13 ms 11908 KB Output is correct
4 Correct 12 ms 11640 KB Output is correct
5 Correct 13 ms 11912 KB Output is correct
6 Correct 13 ms 11896 KB Output is correct
7 Incorrect 11 ms 11640 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 11768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 292 ms 35216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 11668 KB Output is correct
2 Correct 13 ms 11896 KB Output is correct
3 Correct 13 ms 11908 KB Output is correct
4 Correct 12 ms 11640 KB Output is correct
5 Correct 13 ms 11912 KB Output is correct
6 Correct 13 ms 11896 KB Output is correct
7 Incorrect 11 ms 11640 KB Output isn't correct
8 Halted 0 ms 0 KB -