#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 MAXL = 2;
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;
while(u != v){
if(nivel[u] < nivel[v]) swap(u,v);
ans = min(ans,liga[u][0]);
u = ancestral[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((gen() % n)+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:32: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:77:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<grafo[v].size();i++){
~^~~~~~~~~~~~~~~~
plan.cpp:82:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<arestas.size();i++){
~^~~~~~~~~~~~~~~
plan.cpp:52: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:59: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:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
plan.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&x);
~~~~~^~~~~~~~~
plan.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
plan.cpp:99: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 |
6 ms |
5752 KB |
Output is correct |
2 |
Correct |
7 ms |
5884 KB |
Output is correct |
3 |
Correct |
7 ms |
5880 KB |
Output is correct |
4 |
Correct |
6 ms |
5752 KB |
Output is correct |
5 |
Correct |
8 ms |
5880 KB |
Output is correct |
6 |
Correct |
7 ms |
5880 KB |
Output is correct |
7 |
Correct |
6 ms |
5880 KB |
Output is correct |
8 |
Correct |
7 ms |
5884 KB |
Output is correct |
9 |
Correct |
8 ms |
6008 KB |
Output is correct |
10 |
Correct |
8 ms |
6008 KB |
Output is correct |
11 |
Correct |
8 ms |
6008 KB |
Output is correct |
12 |
Correct |
8 ms |
6008 KB |
Output is correct |
13 |
Correct |
8 ms |
5884 KB |
Output is correct |
14 |
Correct |
8 ms |
6008 KB |
Output is correct |
15 |
Correct |
8 ms |
6008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5752 KB |
Output is correct |
2 |
Correct |
7 ms |
5884 KB |
Output is correct |
3 |
Correct |
7 ms |
5880 KB |
Output is correct |
4 |
Correct |
6 ms |
5752 KB |
Output is correct |
5 |
Correct |
8 ms |
5880 KB |
Output is correct |
6 |
Correct |
7 ms |
5880 KB |
Output is correct |
7 |
Correct |
6 ms |
5880 KB |
Output is correct |
8 |
Correct |
7 ms |
5884 KB |
Output is correct |
9 |
Correct |
8 ms |
6008 KB |
Output is correct |
10 |
Correct |
8 ms |
6008 KB |
Output is correct |
11 |
Correct |
8 ms |
6008 KB |
Output is correct |
12 |
Correct |
8 ms |
6008 KB |
Output is correct |
13 |
Correct |
8 ms |
5884 KB |
Output is correct |
14 |
Correct |
8 ms |
6008 KB |
Output is correct |
15 |
Correct |
8 ms |
6008 KB |
Output is correct |
16 |
Correct |
206 ms |
17384 KB |
Output is correct |
17 |
Correct |
786 ms |
40052 KB |
Output is correct |
18 |
Correct |
54 ms |
9328 KB |
Output is correct |
19 |
Correct |
162 ms |
19948 KB |
Output is correct |
20 |
Correct |
800 ms |
39964 KB |
Output is correct |
21 |
Correct |
411 ms |
24588 KB |
Output is correct |
22 |
Correct |
160 ms |
21580 KB |
Output is correct |
23 |
Correct |
798 ms |
40216 KB |
Output is correct |
24 |
Correct |
832 ms |
40864 KB |
Output is correct |
25 |
Correct |
828 ms |
40764 KB |
Output is correct |
26 |
Correct |
169 ms |
20444 KB |
Output is correct |
27 |
Correct |
159 ms |
20548 KB |
Output is correct |
28 |
Correct |
154 ms |
20412 KB |
Output is correct |
29 |
Correct |
151 ms |
20664 KB |
Output is correct |
30 |
Correct |
153 ms |
20752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5856 KB |
Output is correct |
2 |
Correct |
8 ms |
5872 KB |
Output is correct |
3 |
Correct |
7 ms |
5752 KB |
Output is correct |
4 |
Correct |
7 ms |
5880 KB |
Output is correct |
5 |
Correct |
7 ms |
5756 KB |
Output is correct |
6 |
Correct |
7 ms |
5756 KB |
Output is correct |
7 |
Correct |
7 ms |
5852 KB |
Output is correct |
8 |
Correct |
7 ms |
5752 KB |
Output is correct |
9 |
Correct |
7 ms |
5752 KB |
Output is correct |
10 |
Correct |
7 ms |
5824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
307 ms |
22984 KB |
Output is correct |
2 |
Correct |
682 ms |
39988 KB |
Output is correct |
3 |
Correct |
692 ms |
39996 KB |
Output is correct |
4 |
Correct |
123 ms |
21388 KB |
Output is correct |
5 |
Correct |
682 ms |
39964 KB |
Output is correct |
6 |
Correct |
690 ms |
40028 KB |
Output is correct |
7 |
Correct |
667 ms |
39848 KB |
Output is correct |
8 |
Correct |
727 ms |
39944 KB |
Output is correct |
9 |
Correct |
681 ms |
39808 KB |
Output is correct |
10 |
Correct |
691 ms |
39992 KB |
Output is correct |
11 |
Correct |
686 ms |
39880 KB |
Output is correct |
12 |
Correct |
700 ms |
40028 KB |
Output is correct |
13 |
Correct |
709 ms |
39948 KB |
Output is correct |
14 |
Correct |
705 ms |
39960 KB |
Output is correct |
15 |
Correct |
688 ms |
39992 KB |
Output is correct |
16 |
Correct |
702 ms |
39932 KB |
Output is correct |
17 |
Correct |
688 ms |
39972 KB |
Output is correct |
18 |
Correct |
684 ms |
39876 KB |
Output is correct |
19 |
Correct |
113 ms |
21196 KB |
Output is correct |
20 |
Correct |
696 ms |
40144 KB |
Output is correct |
21 |
Correct |
702 ms |
41320 KB |
Output is correct |
22 |
Correct |
113 ms |
20352 KB |
Output is correct |
23 |
Correct |
130 ms |
19240 KB |
Output is correct |
24 |
Correct |
115 ms |
20160 KB |
Output is correct |
25 |
Correct |
122 ms |
20196 KB |
Output is correct |
26 |
Correct |
139 ms |
19664 KB |
Output is correct |
27 |
Correct |
124 ms |
21768 KB |
Output is correct |
28 |
Correct |
121 ms |
20392 KB |
Output is correct |
29 |
Correct |
131 ms |
20632 KB |
Output is correct |
30 |
Correct |
124 ms |
20348 KB |
Output is correct |
31 |
Correct |
126 ms |
20972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5752 KB |
Output is correct |
2 |
Correct |
7 ms |
5884 KB |
Output is correct |
3 |
Correct |
7 ms |
5880 KB |
Output is correct |
4 |
Correct |
6 ms |
5752 KB |
Output is correct |
5 |
Correct |
8 ms |
5880 KB |
Output is correct |
6 |
Correct |
7 ms |
5880 KB |
Output is correct |
7 |
Correct |
6 ms |
5880 KB |
Output is correct |
8 |
Correct |
7 ms |
5884 KB |
Output is correct |
9 |
Correct |
8 ms |
6008 KB |
Output is correct |
10 |
Correct |
8 ms |
6008 KB |
Output is correct |
11 |
Correct |
8 ms |
6008 KB |
Output is correct |
12 |
Correct |
8 ms |
6008 KB |
Output is correct |
13 |
Correct |
8 ms |
5884 KB |
Output is correct |
14 |
Correct |
8 ms |
6008 KB |
Output is correct |
15 |
Correct |
8 ms |
6008 KB |
Output is correct |
16 |
Correct |
206 ms |
17384 KB |
Output is correct |
17 |
Correct |
786 ms |
40052 KB |
Output is correct |
18 |
Correct |
54 ms |
9328 KB |
Output is correct |
19 |
Correct |
162 ms |
19948 KB |
Output is correct |
20 |
Correct |
800 ms |
39964 KB |
Output is correct |
21 |
Correct |
411 ms |
24588 KB |
Output is correct |
22 |
Correct |
160 ms |
21580 KB |
Output is correct |
23 |
Correct |
798 ms |
40216 KB |
Output is correct |
24 |
Correct |
832 ms |
40864 KB |
Output is correct |
25 |
Correct |
828 ms |
40764 KB |
Output is correct |
26 |
Correct |
169 ms |
20444 KB |
Output is correct |
27 |
Correct |
159 ms |
20548 KB |
Output is correct |
28 |
Correct |
154 ms |
20412 KB |
Output is correct |
29 |
Correct |
151 ms |
20664 KB |
Output is correct |
30 |
Correct |
153 ms |
20752 KB |
Output is correct |
31 |
Correct |
7 ms |
5856 KB |
Output is correct |
32 |
Correct |
8 ms |
5872 KB |
Output is correct |
33 |
Correct |
7 ms |
5752 KB |
Output is correct |
34 |
Correct |
7 ms |
5880 KB |
Output is correct |
35 |
Correct |
7 ms |
5756 KB |
Output is correct |
36 |
Correct |
7 ms |
5756 KB |
Output is correct |
37 |
Correct |
7 ms |
5852 KB |
Output is correct |
38 |
Correct |
7 ms |
5752 KB |
Output is correct |
39 |
Correct |
7 ms |
5752 KB |
Output is correct |
40 |
Correct |
7 ms |
5824 KB |
Output is correct |
41 |
Correct |
307 ms |
22984 KB |
Output is correct |
42 |
Correct |
682 ms |
39988 KB |
Output is correct |
43 |
Correct |
692 ms |
39996 KB |
Output is correct |
44 |
Correct |
123 ms |
21388 KB |
Output is correct |
45 |
Correct |
682 ms |
39964 KB |
Output is correct |
46 |
Correct |
690 ms |
40028 KB |
Output is correct |
47 |
Correct |
667 ms |
39848 KB |
Output is correct |
48 |
Correct |
727 ms |
39944 KB |
Output is correct |
49 |
Correct |
681 ms |
39808 KB |
Output is correct |
50 |
Correct |
691 ms |
39992 KB |
Output is correct |
51 |
Correct |
686 ms |
39880 KB |
Output is correct |
52 |
Correct |
700 ms |
40028 KB |
Output is correct |
53 |
Correct |
709 ms |
39948 KB |
Output is correct |
54 |
Correct |
705 ms |
39960 KB |
Output is correct |
55 |
Correct |
688 ms |
39992 KB |
Output is correct |
56 |
Correct |
702 ms |
39932 KB |
Output is correct |
57 |
Correct |
688 ms |
39972 KB |
Output is correct |
58 |
Correct |
684 ms |
39876 KB |
Output is correct |
59 |
Correct |
113 ms |
21196 KB |
Output is correct |
60 |
Correct |
696 ms |
40144 KB |
Output is correct |
61 |
Correct |
702 ms |
41320 KB |
Output is correct |
62 |
Correct |
113 ms |
20352 KB |
Output is correct |
63 |
Correct |
130 ms |
19240 KB |
Output is correct |
64 |
Correct |
115 ms |
20160 KB |
Output is correct |
65 |
Correct |
122 ms |
20196 KB |
Output is correct |
66 |
Correct |
139 ms |
19664 KB |
Output is correct |
67 |
Correct |
124 ms |
21768 KB |
Output is correct |
68 |
Correct |
121 ms |
20392 KB |
Output is correct |
69 |
Correct |
131 ms |
20632 KB |
Output is correct |
70 |
Correct |
124 ms |
20348 KB |
Output is correct |
71 |
Correct |
126 ms |
20972 KB |
Output is correct |
72 |
Correct |
449 ms |
25564 KB |
Output is correct |
73 |
Correct |
840 ms |
41220 KB |
Output is correct |
74 |
Correct |
863 ms |
41208 KB |
Output is correct |
75 |
Correct |
870 ms |
41260 KB |
Output is correct |
76 |
Correct |
883 ms |
41392 KB |
Output is correct |
77 |
Correct |
825 ms |
41328 KB |
Output is correct |
78 |
Correct |
852 ms |
40356 KB |
Output is correct |
79 |
Correct |
823 ms |
39756 KB |
Output is correct |
80 |
Correct |
815 ms |
39604 KB |
Output is correct |
81 |
Correct |
837 ms |
39524 KB |
Output is correct |
82 |
Correct |
840 ms |
39488 KB |
Output is correct |
83 |
Correct |
836 ms |
39584 KB |
Output is correct |
84 |
Correct |
804 ms |
39392 KB |
Output is correct |
85 |
Correct |
813 ms |
39376 KB |
Output is correct |
86 |
Correct |
830 ms |
39112 KB |
Output is correct |
87 |
Correct |
798 ms |
39380 KB |
Output is correct |
88 |
Correct |
807 ms |
39516 KB |
Output is correct |
89 |
Execution timed out |
4091 ms |
20176 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |