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 <stdio.h>
#define N 1000000
#define A 2000002
int n, m, a[N], b[N], count[A], next[A], o, succ[A], sussy[A], cyclen, cyc[A], ii, END = A - 1, len = -1, cur = 0;
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i)
scanf("%d%d", a+i, b+i),
++count[a[i]], ++count[b[i]];
for (int i = 0; i < sizeof succ / sizeof *succ; ++i)
succ[i] = -1;
int last = 0;
for (int i = 1; i < END; ++i) {
if (count[i]) {
if (-1 == succ[0])
succ[0] = i;
next[last] = i;
next[last = i] = END;
--count[i];
}
}
for (int i = 0; i < n; ++i)
succ[a[i]] = next[b[i]], succ[b[i]] = next[a[i]];
while (cur != END)
sussy[cur] = 1, ++len, cur = succ[cur];
sussy[END] = 1;
for (int i = 0; i < END; ++i) {
if (-1 < succ[i] && !sussy[i]) {
cyclen = 1;
cur = succ[i];
sussy[i] = -1;
while (cur != i)
++cyclen, sussy[cur] = -1, cur = succ[cur];
++count[cyclen];
}
}
for (int i = END - 1; i >= 1; --i)
while (count[i]) cyc[o++] = i, --count[i];
while (ii < o && m)
--m, len += cyc[ii++] + 2;
len += 4 * (m / 2);
if (m & 1) ++len;
printf("%d", len);
return 0;
}
Compilation message (stderr)
teleporters.c: In function 'main':
teleporters.c:9:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
teleporters.c:11:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | scanf("%d%d", a+i, b+i),
| ^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |