Submission #58272

# Submission time Handle Problem Language Result Execution time Memory
58272 2018-07-17T10:37:39 Z memikakizaki Teleporters (IOI08_teleporters) C++14
100 / 100
855 ms 54844 KB
#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], 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]);
		nxt[i<<1] = lf[i];
		nxt[i<<1|1] = rg[i];
	}
	sort(nxt, nxt+2*n+1); // use nxt as val array
	for(int i = 0; i <= n << 1; i++) rev[nxt[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

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 time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 740 KB Output is correct
2 Correct 6 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 980 KB Output is correct
2 Correct 8 ms 1388 KB Output is correct
3 Correct 19 ms 1852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 1852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 5248 KB Output is correct
2 Correct 198 ms 12740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 12740 KB Output is correct
2 Correct 346 ms 16596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 466 ms 20708 KB Output is correct
2 Correct 536 ms 22984 KB Output is correct
3 Correct 622 ms 24060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 764 ms 24548 KB Output is correct
2 Correct 822 ms 37320 KB Output is correct
3 Correct 684 ms 37320 KB Output is correct
4 Correct 729 ms 37320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 855 ms 38668 KB Output is correct
2 Correct 702 ms 38884 KB Output is correct
3 Correct 514 ms 54696 KB Output is correct
4 Correct 705 ms 54844 KB Output is correct