#include<bits/stdc++.h>
using namespace std;
vector<int> group[100010];
vector<pair<int, int> > pa[100010];
vector<pair<int, pair<int, int> > > mv;
int p[100010], cycle[100010], outd[100010], sizep[100010], cnt[100010];
int find(int x){
if(p[x] == x) return x;
return find(p[x]);
}
int init(int n, int m, vector<int> u, vector<int> v, vector<int> w){
for(int i = 1;i <= n;i++){
sizep[i] = 1; p[i] = i;
group[i].push_back(i);
pa[i].push_back({0, i});
}
for(int i = 0;i < m;i++){
mv.push_back({w[i], {u[i], v[i]}});
}
sort(mv.begin(), mv.end());
for(int i = 0;i < m;i++){
int u1 = find(mv[i].second.first);
int u2 = find(mv[i].second.second);
cnt[mv[i].second.first]++; cnt[mv[i].second.second]++;
if(u1 == u2){
if(cycle[u1] == 0) cycle[u1] = mv[i].first;
if(cnt[mv[i].second.first] >= 3 || cnt[mv[i].second.second] >= 3){
if(outd[u1] == 0) outd[u1] = mv[i].first;
}
}
else{
if(sizep[u1] < sizep[u2]) swap(u1, u2);
for(int i = 0;i < group[u2].size();i++){
pa[group[u2][i]].push_back({mv[i].first, u1});
p[group[u2][i]] = u1;
}
sizep[u1] += sizep[u2];
if(cycle[u1] || cycle[u2]){
if(cycle[u2] == 0) cycle[u2] = mv[i].first;
if(cycle[u1] == 0) cycle[u1] = mv[i].first;
}
if(outd[u1] || outd[u2] || cnt[mv[i].second.first] >= 3 || cnt[mv[i].second.second] >= 3){
if(outd[u1] == 0) outd[u1] = mv[i].first;
if(outd[u2] == 0) outd[u2] = mv[i].first;
}
}
}
}
int getMinimumFuelCapacity(int X, int Y){
int x = find(X), y = find(Y), ans;
int s1 = 0, e1 = pa[x].size() - 1;
while(s1 < e1){
int mid = (s1+e1)/2;
if(cycle[pa[x][mid].second]||outd[pa[x][mid].second]) e1 = mid;
else s1 = mid+1;
}
int s2 = 0, e2 = pa[y].size() - 1;
while(s2 < e2){
int mid = (s2+e2)/2;
if(cycle[pa[y][mid].second]||outd[pa[y][mid].second]) e2 = mid;
else s2 = mid+1;
}
ans = min(min(cycle[pa[x][s1].second], outd[pa[x][s1].second]), min(cycle[pa[y][s2].second], outd[pa[y][s2].second]));
int s = 0, e = 1000000001;
while(s < e){
int mid = (s+e)/2;
s1 = 0, e1 = pa[x].size() - 1;
while(s1 < e1){
int mid1 = (s1+e1)/2;
if(pa[x][mid1].first < mid) s1 = mid1+1;
else e1 = mid1;
}
s2 = 0, e2 = pa[y].size() - 1;
while(s2 < e2){
int mid2 = (s2+e2)/2;
if(pa[y][mid2].first < mid) s2 = mid2+1;
else e2 = mid2;
}
if(pa[x][s1].second != pa[y][s2].second) s = mid+1;
else e = mid;
}
if(e == 1000000001) return -1;
else return max(ans, s);
}
Compilation message
swap.cpp: In function 'int init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:35:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0;i < group[u2].size();i++){
| ~~^~~~~~~~~~~~~~~~~~
swap.cpp:50:1: warning: no return statement in function returning non-void [-Wreturn-type]
50 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
9984 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
9984 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
11 ms |
9984 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
11 ms |
9984 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
9984 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
11 ms |
9984 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |