Submission #19289118


Source Code Expand

#include <cstdio>
#include <cctype>
#include <cstring>
using namespace std;

const int max_n = 200050, max_m = 400050;
int l[2][max_n], mat[max_m];

inline int cm(int x) { return l[1][mat[x]] == x; }

inline int my_min(int a, int b) { return (a < b)? a:b; }
inline int my_max(int a, int b) { return (a > b)? a:b; }

#define gc getchar
inline int read()
{
	int c = gc(), t = 1, n = 0;
	while (isspace(c)) { c = gc(); }
	if (c == '-') { t = -1, c = gc(); }
	while (isdigit(c)) { n = n * 10 + c - '0', c = gc(); }
	return n * t;
}
#undef gc

bool try_match(int i, int t)
{
//	fprintf(stderr, "Trying match %d to %d.\n", i, t);
	bool ret = true, rov = false;

	if (mat[l[cm(t)^1][i]] == i)
		mat[l[cm(t)^1][i]] = -1, rov = true;
	
	if (mat[t] == -1)
		mat[t] = i;
	else if (mat[l[0][mat[t]]] != -1 && mat[l[1][mat[t]]] != -1)
		ret = false;
	else
	{
		if (try_match(mat[t], l[cm(t)^1][mat[t]]))
			mat[t] = i;
		else
			ret = false;
	}
	
	if (!ret && rov)
		mat[l[cm(t)^1][i]] = i;

	return ret;
}

int main()
{
//	freopen("B3.in", "r", stdin);

	memset(mat, -1, sizeof(mat));

	int n = read(), ans = 0, ta, tb;

	for (int i = 0; i < n; i++)
	{
		ta = read(), tb = read();
		l[0][i] = my_min(ta, tb) - 1, l[1][i] = my_max(ta, tb) - 1;
	}
	
	for (int i = 0; i < n; i++)
	{
		if (try_match(i, l[0][i]))
			ans++;
		else if (try_match(i, l[1][i]))
			ans++;
	}

	printf("%d\n", ans);

	return 0;
}

/*
4
1 2
2 3
3 1
1 4
*/

Submission Info

Submission Time
Task B - Reversible Cards
User fiveAB
Language C++ (GCC 9.2.1)
Score 0
Code Size 1498 Byte
Status WA
Exec Time 30 ms
Memory 4792 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 14
WA × 13
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt
Case Name Status Exec Time Memory
0_000.txt AC 11 ms 3228 KiB
0_001.txt AC 2 ms 3176 KiB
0_002.txt AC 2 ms 3132 KiB
1_003.txt AC 2 ms 3144 KiB
1_004.txt AC 3 ms 3112 KiB
1_005.txt AC 17 ms 4692 KiB
1_006.txt WA 2 ms 3188 KiB
1_007.txt AC 3 ms 3148 KiB
1_008.txt AC 18 ms 4784 KiB
1_009.txt WA 26 ms 4740 KiB
1_010.txt WA 29 ms 4712 KiB
1_011.txt WA 30 ms 4736 KiB
1_012.txt AC 19 ms 4792 KiB
1_013.txt AC 16 ms 4696 KiB
1_014.txt AC 26 ms 4716 KiB
1_015.txt AC 17 ms 4672 KiB
1_016.txt WA 2 ms 3172 KiB
1_017.txt WA 7 ms 3348 KiB
1_018.txt WA 14 ms 3752 KiB
1_019.txt WA 29 ms 4680 KiB
1_020.txt WA 26 ms 4692 KiB
1_021.txt AC 3 ms 3176 KiB
1_022.txt AC 2 ms 3232 KiB
1_023.txt WA 24 ms 4344 KiB
1_024.txt WA 13 ms 3840 KiB
1_025.txt WA 28 ms 4688 KiB
1_026.txt WA 14 ms 3916 KiB