This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using ll = long long;
#define int ll
using namespace std;
#define sz(x) (int)(x).size()
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, l, r) for(int i = l; i >= r; i--)
#define fi first
#define se second
#define mod 998244353
#define db(x) cerr << __LINE__ << " " << #x << " " << x << "\n"
using vi = vector<int>;
using pi = pair<int, int>;
const ll N = 200005;
const ll inf = 1e18;
int n, m, w[N], e[N], vis[N], nxt[N];
vi v;
void solve(){
cin >> n >> m;
foru(i, 1, n){
cin >> w[i] >> e[i];
v.push_back(w[i]);
v.push_back(e[i]);
}
sort(v.begin(), v.end());
foru(i, 0, (n << 1) - 1){
nxt[i] = i + 1;
}
nxt[n << 1] = 0;
foru(i, 1, n){
w[i] = lower_bound(v.begin(), v.end(), w[i]) - v.begin() + 1;
e[i] = lower_bound(v.begin(), v.end(), e[i]) - v.begin() + 1;
nxt[w[i] - 1] = e[i];
nxt[e[i] - 1] = w[i];
}
v.clear();
int res;
foru(i, 0, n << 1){
if(vis[i]) continue;
int cnt = 0;
for(int j = i; !vis[j]; j = nxt[j]){
vis[j] = 1;
cnt++;
}
if(!i) res = cnt;
else v.push_back(cnt);
}
sort(v.begin(), v.end()); res--;
while(m){
if(!sz(v)) break;
else{
res += v.back() + 2;
v.pop_back();
}
m--;
}
cout << res - (m >> 1) + (m << 1);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
while(t--){
solve();
}
}
Compilation message (stderr)
teleporters.cpp: In function 'void solve()':
teleporters.cpp:53:31: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
53 | sort(v.begin(), v.end()); res--;
| ~~~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |