Submission #58271

#TimeUsernameProblemLanguageResultExecution timeMemory
58271memikakizakiTeleporters (IOI08_teleporters)C++14
90 / 100
823 ms66560 KiB
#include <bits/stdc++.h> #define long long long using namespace std; const int N = 1e6+3, INF = 1e9; int n, m, lf[N], rg[N], vals[2*N], rev[2*N], nxt[2*N]; int cycles[N], idx; bitset<N<<1> mark; long ans; int main() { scanf("%d %d", &n, &m); for(int i = 0; i < n; i++) { scanf("%d %d", &lf[i], &rg[i]); vals[i<<1] = lf[i]; vals[i<<1|1] = rg[i]; } sort(vals, vals+2*n+1); for(int i = 0; i <= n << 1; i++) rev[vals[i]] = i; for(int i = 0; i < n; i++) { nxt[rev[lf[i]]-1] = rev[rg[i]]; nxt[rev[rg[i]]-1] = rev[lf[i]]; } nxt[2*n] = INF; for(int i = 0; i <= n << 1; i++) if(!mark[i]) { int curr = i, sz = 0; bool cycle = false; while(curr != INF) { if(mark[curr]) { cycle = true; break; } ++sz; mark[curr] = true; curr = nxt[curr]; } if(cycle) cycles[idx++] = sz; else ans = sz-1; } sort(cycles, cycles+idx, greater<int>()); for(int i = 0; i < min(idx, m); i++) ans += cycles[i] + 2; if(m > idx) { m -= idx; ans += ((m - (m & 1)) << 1) + (m & 1); } printf("%lld", ans); }

Compilation message (stderr)

teleporters.cpp: In function 'int main()':
teleporters.cpp:11:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
teleporters.cpp:13:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &lf[i], &rg[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...