Submission #41549

# Submission time Handle Problem Language Result Execution time Memory
41549 2018-02-18T18:30:51 Z IvanC Evacuation plan (IZhO18_plan) C++14
100 / 100
899 ms 40764 KB
#include <bits/stdc++.h>
using namespace std;
knuth_b gen((rand() ^ rand() ^ rand()) ^ (rand() << (rand() % 17) ));
typedef pair<int,int> ii;
typedef tuple<int,int,int> trinca;
const int INF = 1e9;
const int MAXN = 1e5 + 10;
const int BUCKET = 300;
vector<ii> grafo[MAXN],adj[MAXN],arestas;
vector<trinca> Kruskal;
priority_queue<ii, vector<ii>, greater<ii> > pq;
int superancestral[MAXN],superliga[MAXN],ancestral[MAXN],liga[MAXN],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 = 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] = v;
		liga[u] = w;
		dfs(u,v);
	}
}
void superdfs(int v,int p,int sp,int lsp){
	superancestral[v] = sp;
	superliga[v] = lsp;
	if(nivel[v] % BUCKET == 0){
		sp = v;
		lsp = INF;
	}
	for(int i = 0;i<adj[v].size();i++){
		int u = adj[v][i].first, w = adj[v][i].second;
		if(u == p) continue;
		superdfs(u,v,sp, min(lsp,w) );
	}
}
int LCA(int u,int v){
	int ans = INF;
	while(superancestral[u] != superancestral[v]){
		if(nivel[u] < nivel[v]) swap(u,v);
		ans = min(ans,superliga[u]);
		u = superancestral[u];
	}
	while(u != v){
		if(nivel[u] < nivel[v]) swap(u,v);
		ans = min(ans,liga[u]);
		u = ancestral[u];
	}
	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);
	superancestral[1] = 1;
	superdfs(1,-1,1,INF);
	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:25:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0;i<adj[v].size();i++){
                ~^~~~~~~~~~~~~~
plan.cpp: In function 'void superdfs(int, int, int, int)':
plan.cpp:41: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:88:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0;i<grafo[v].size();i++){
                 ~^~~~~~~~~~~~~~~~
plan.cpp:93:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0;i<arestas.size();i++){
                ~^~~~~~~~~~~~~~~
plan.cpp:63: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:70: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:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&q);
  ~~~~~^~~~~~~~~
plan.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&x);
   ~~~~~^~~~~~~~~
plan.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&q);
  ~~~~~^~~~~~~~~
plan.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u,&v);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5468 KB Output is correct
2 Correct 7 ms 5624 KB Output is correct
3 Correct 7 ms 5624 KB Output is correct
4 Correct 6 ms 5484 KB Output is correct
5 Correct 7 ms 5624 KB Output is correct
6 Correct 7 ms 5624 KB Output is correct
7 Correct 6 ms 5496 KB Output is correct
8 Correct 6 ms 5496 KB Output is correct
9 Correct 8 ms 5576 KB Output is correct
10 Correct 8 ms 5640 KB Output is correct
11 Correct 8 ms 5624 KB Output is correct
12 Correct 7 ms 5544 KB Output is correct
13 Correct 7 ms 5632 KB Output is correct
14 Correct 7 ms 5628 KB Output is correct
15 Correct 8 ms 5620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5468 KB Output is correct
2 Correct 7 ms 5624 KB Output is correct
3 Correct 7 ms 5624 KB Output is correct
4 Correct 6 ms 5484 KB Output is correct
5 Correct 7 ms 5624 KB Output is correct
6 Correct 7 ms 5624 KB Output is correct
7 Correct 6 ms 5496 KB Output is correct
8 Correct 6 ms 5496 KB Output is correct
9 Correct 8 ms 5576 KB Output is correct
10 Correct 8 ms 5640 KB Output is correct
11 Correct 8 ms 5624 KB Output is correct
12 Correct 7 ms 5544 KB Output is correct
13 Correct 7 ms 5632 KB Output is correct
14 Correct 7 ms 5628 KB Output is correct
15 Correct 8 ms 5620 KB Output is correct
16 Correct 220 ms 17884 KB Output is correct
17 Correct 828 ms 39844 KB Output is correct
18 Correct 55 ms 9068 KB Output is correct
19 Correct 165 ms 20256 KB Output is correct
20 Correct 826 ms 40104 KB Output is correct
21 Correct 506 ms 24644 KB Output is correct
22 Correct 219 ms 24920 KB Output is correct
23 Correct 899 ms 39920 KB Output is correct
24 Correct 787 ms 39700 KB Output is correct
25 Correct 828 ms 39796 KB Output is correct
26 Correct 161 ms 19804 KB Output is correct
27 Correct 181 ms 19932 KB Output is correct
28 Correct 155 ms 19844 KB Output is correct
29 Correct 162 ms 20248 KB Output is correct
30 Correct 156 ms 20184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5496 KB Output is correct
2 Correct 6 ms 5368 KB Output is correct
3 Correct 6 ms 5496 KB Output is correct
4 Correct 6 ms 5496 KB Output is correct
5 Correct 6 ms 5496 KB Output is correct
6 Correct 6 ms 5464 KB Output is correct
7 Correct 6 ms 5396 KB Output is correct
8 Correct 6 ms 5368 KB Output is correct
9 Correct 7 ms 5496 KB Output is correct
10 Correct 7 ms 5408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 287 ms 23188 KB Output is correct
2 Correct 743 ms 39552 KB Output is correct
3 Correct 708 ms 39448 KB Output is correct
4 Correct 125 ms 20848 KB Output is correct
5 Correct 713 ms 39324 KB Output is correct
6 Correct 686 ms 39248 KB Output is correct
7 Correct 673 ms 39228 KB Output is correct
8 Correct 685 ms 39304 KB Output is correct
9 Correct 671 ms 39252 KB Output is correct
10 Correct 691 ms 39332 KB Output is correct
11 Correct 708 ms 39448 KB Output is correct
12 Correct 687 ms 39260 KB Output is correct
13 Correct 667 ms 39368 KB Output is correct
14 Correct 670 ms 39368 KB Output is correct
15 Correct 700 ms 39496 KB Output is correct
16 Correct 676 ms 39424 KB Output is correct
17 Correct 661 ms 39368 KB Output is correct
18 Correct 678 ms 39144 KB Output is correct
19 Correct 118 ms 23236 KB Output is correct
20 Correct 756 ms 38812 KB Output is correct
21 Correct 710 ms 40392 KB Output is correct
22 Correct 120 ms 19812 KB Output is correct
23 Correct 139 ms 19040 KB Output is correct
24 Correct 121 ms 19484 KB Output is correct
25 Correct 118 ms 19460 KB Output is correct
26 Correct 150 ms 19512 KB Output is correct
27 Correct 132 ms 23220 KB Output is correct
28 Correct 121 ms 19804 KB Output is correct
29 Correct 125 ms 22120 KB Output is correct
30 Correct 120 ms 19932 KB Output is correct
31 Correct 133 ms 22016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5468 KB Output is correct
2 Correct 7 ms 5624 KB Output is correct
3 Correct 7 ms 5624 KB Output is correct
4 Correct 6 ms 5484 KB Output is correct
5 Correct 7 ms 5624 KB Output is correct
6 Correct 7 ms 5624 KB Output is correct
7 Correct 6 ms 5496 KB Output is correct
8 Correct 6 ms 5496 KB Output is correct
9 Correct 8 ms 5576 KB Output is correct
10 Correct 8 ms 5640 KB Output is correct
11 Correct 8 ms 5624 KB Output is correct
12 Correct 7 ms 5544 KB Output is correct
13 Correct 7 ms 5632 KB Output is correct
14 Correct 7 ms 5628 KB Output is correct
15 Correct 8 ms 5620 KB Output is correct
16 Correct 220 ms 17884 KB Output is correct
17 Correct 828 ms 39844 KB Output is correct
18 Correct 55 ms 9068 KB Output is correct
19 Correct 165 ms 20256 KB Output is correct
20 Correct 826 ms 40104 KB Output is correct
21 Correct 506 ms 24644 KB Output is correct
22 Correct 219 ms 24920 KB Output is correct
23 Correct 899 ms 39920 KB Output is correct
24 Correct 787 ms 39700 KB Output is correct
25 Correct 828 ms 39796 KB Output is correct
26 Correct 161 ms 19804 KB Output is correct
27 Correct 181 ms 19932 KB Output is correct
28 Correct 155 ms 19844 KB Output is correct
29 Correct 162 ms 20248 KB Output is correct
30 Correct 156 ms 20184 KB Output is correct
31 Correct 6 ms 5496 KB Output is correct
32 Correct 6 ms 5368 KB Output is correct
33 Correct 6 ms 5496 KB Output is correct
34 Correct 6 ms 5496 KB Output is correct
35 Correct 6 ms 5496 KB Output is correct
36 Correct 6 ms 5464 KB Output is correct
37 Correct 6 ms 5396 KB Output is correct
38 Correct 6 ms 5368 KB Output is correct
39 Correct 7 ms 5496 KB Output is correct
40 Correct 7 ms 5408 KB Output is correct
41 Correct 287 ms 23188 KB Output is correct
42 Correct 743 ms 39552 KB Output is correct
43 Correct 708 ms 39448 KB Output is correct
44 Correct 125 ms 20848 KB Output is correct
45 Correct 713 ms 39324 KB Output is correct
46 Correct 686 ms 39248 KB Output is correct
47 Correct 673 ms 39228 KB Output is correct
48 Correct 685 ms 39304 KB Output is correct
49 Correct 671 ms 39252 KB Output is correct
50 Correct 691 ms 39332 KB Output is correct
51 Correct 708 ms 39448 KB Output is correct
52 Correct 687 ms 39260 KB Output is correct
53 Correct 667 ms 39368 KB Output is correct
54 Correct 670 ms 39368 KB Output is correct
55 Correct 700 ms 39496 KB Output is correct
56 Correct 676 ms 39424 KB Output is correct
57 Correct 661 ms 39368 KB Output is correct
58 Correct 678 ms 39144 KB Output is correct
59 Correct 118 ms 23236 KB Output is correct
60 Correct 756 ms 38812 KB Output is correct
61 Correct 710 ms 40392 KB Output is correct
62 Correct 120 ms 19812 KB Output is correct
63 Correct 139 ms 19040 KB Output is correct
64 Correct 121 ms 19484 KB Output is correct
65 Correct 118 ms 19460 KB Output is correct
66 Correct 150 ms 19512 KB Output is correct
67 Correct 132 ms 23220 KB Output is correct
68 Correct 121 ms 19804 KB Output is correct
69 Correct 125 ms 22120 KB Output is correct
70 Correct 120 ms 19932 KB Output is correct
71 Correct 133 ms 22016 KB Output is correct
72 Correct 443 ms 24256 KB Output is correct
73 Correct 827 ms 39800 KB Output is correct
74 Correct 813 ms 39700 KB Output is correct
75 Correct 827 ms 39696 KB Output is correct
76 Correct 821 ms 39840 KB Output is correct
77 Correct 801 ms 39632 KB Output is correct
78 Correct 828 ms 39592 KB Output is correct
79 Correct 830 ms 39404 KB Output is correct
80 Correct 825 ms 39368 KB Output is correct
81 Correct 849 ms 39480 KB Output is correct
82 Correct 850 ms 39436 KB Output is correct
83 Correct 830 ms 39624 KB Output is correct
84 Correct 844 ms 39548 KB Output is correct
85 Correct 828 ms 39400 KB Output is correct
86 Correct 830 ms 39428 KB Output is correct
87 Correct 799 ms 39464 KB Output is correct
88 Correct 803 ms 39624 KB Output is correct
89 Correct 687 ms 23400 KB Output is correct
90 Correct 859 ms 40076 KB Output is correct
91 Correct 846 ms 40764 KB Output is correct
92 Correct 156 ms 20824 KB Output is correct
93 Correct 492 ms 20716 KB Output is correct
94 Correct 163 ms 20860 KB Output is correct
95 Correct 160 ms 20604 KB Output is correct
96 Correct 534 ms 20216 KB Output is correct
97 Correct 691 ms 22808 KB Output is correct
98 Correct 160 ms 20444 KB Output is correct
99 Correct 687 ms 24400 KB Output is correct
100 Correct 170 ms 20848 KB Output is correct