QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#487235#5345. VivoParcPetroTarnavskyi#AC ✓0ms3996kbC++201.9kb2024-07-22 19:03:502024-07-22 19:03:51

Judging History

你现在查看的是最新测评结果

  • [2024-07-22 19:03:51]
  • 评测
  • 测评结果:AC
  • 用时:0ms
  • 内存:3996kb
  • [2024-07-22 19:03:50]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 147;
set<int> g[N];
int col[N]; // -1 ne pofarbovano, -2 ne pofarbovano ale yasno
VI yasno;
map<int, int> susids[N];
int n;

bool cmp(int u, int v)
{
	return MP(SZ(susids[u]), SZ(g[u])) > MP(SZ(susids[v]), SZ(g[v]));
}

void solve()
{
	int j = -1;
	FOR (i, 0, n)
	{
		if (col[i] == -1 && (j == -1 || cmp(i, j)))
			j = i;
	}
	if (j == -1)
	{
		RFOR (i, SZ(yasno), 0)
		{
			int v = yasno[i];
			set<int> val;
			for (auto to : g[v])
				val.insert(col[to]);
			FOR (c, 0, 4)
			{
				if (!val.count(c))
				{
					col[v] = c;
					break;
				}
			}
		}
		FOR (i, 0, n)
		{
			cout << i + 1 << ' ' << col[i] + 1 << '\n';
		}
		exit(0);
	}
	FOR (c, 0, 4)
	{
		if (susids[j].count(c))
			continue;
		for (auto to : g[j])
		{
			susids[to][c]++;
		}
		col[j] = c;
		solve();
		col[j] = -1;
		for (auto to : g[j])
		{
			susids[to][c]--;
			if (susids[to][c] == 0)
				susids[to].erase(c);
		}
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	string s;
	while (cin >> s)
	{
		int j = 0;
		while (s[j] != '-')
			j++;
		int u = stoi(s.substr(0, j));
		int v = stoi(s.substr(j + 1, SZ(s) - j - 1));
		u--, v--;
		g[u].insert(v);
		g[v].insert(u);
	}
	fill(col, col + n, -1);
	while (true)
	{
		int j = -1;
		FOR (i, 0, n)
		{
			if (col[i] == -1 && SZ(g[i]) <= 3)
			{
				j = i;
				break;
			}
		}
		if (j == -1)
			break;
		col[j] = -2;
		yasno.PB(j);
		for (auto v : g[j])
			g[v].erase(j);
	}
	solve();
	
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3536kb

input:

8
1-2
3-1
4-5
4-8
1-7
1-4
7-1
2-4
1-8
6-7
2-3
1-5
1-6
7-6
7-8
2-5
7-1
3-4
5-6
7-8

output:

1 3
2 1
3 2
4 4
5 2
6 1
7 2
8 1

result:

ok good solution

Test #2:

score: 0
Accepted
time: 0ms
memory: 3524kb

input:

25
1-3
1-6
1-7
1-8
1-10
1-11
1-12
1-15
1-17
1-19
1-23
1-25
2-3
2-6
2-7
2-8
2-10
2-11
2-15
2-17
2-19
2-20
2-25
3-4
3-5
3-6
3-8
3-9
3-11
3-15
3-18
3-19
3-20
3-21
3-22
3-23
3-24
4-6
4-7
4-8
4-10
4-11
4-12
4-15
4-19
4-20
4-24
4-25
5-6
5-7
5-8
5-10
5-11
5-12
5-15
5-17
5-19
5-20
5-23
5-24
5-25
6-7
6-8
6-9...

output:

1 4
2 4
3 2
4 4
5 4
6 3
7 2
8 1
9 4
10 2
11 3
12 2
13 4
14 4
15 3
16 4
17 2
18 4
19 1
20 1
21 4
22 4
23 1
24 3
25 2

result:

ok good solution

Test #3:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

50
1-2
1-9
1-10
1-12
1-21
1-22
1-37
37-1
1-40
1-42
1-46
2-3
2-9
2-10
2-11
2-14
2-22
2-26
2-30
2-33
33-2
2-34
2-36
2-38
2-39
2-40
2-43
2-47
3-7
3-8
3-11
11-3
3-12
3-16
3-20
3-21
3-26
3-27
3-28
3-29
3-32
3-33
3-35
3-39
39-3
3-40
3-46
3-47
3-49
4-7
4-8
4-9
4-10
4-11
4-19
4-20
20-4
4-21
4-26
4-27
4-30
3...

output:

1 4
2 2
3 4
4 4
5 3
6 4
7 1
8 2
9 3
10 1
11 1
12 3
13 3
14 1
15 4
16 2
17 4
18 2
19 3
20 2
21 1
22 1
23 1
24 2
25 2
26 3
27 2
28 2
29 3
30 3
31 4
32 1
33 3
34 4
35 1
36 4
37 2
38 4
39 3
40 3
41 4
42 3
43 4
44 1
45 4
46 2
47 3
48 4
49 3
50 3

result:

ok good solution

Test #4:

score: 0
Accepted
time: 0ms
memory: 3996kb

input:

100
1-2
1-4
1-5
1-6
1-7
1-8
1-9
1-10
1-11
1-12
1-14
14-1
1-15
1-16
1-17
1-18
1-20
1-21
1-22
1-24
1-26
1-27
1-28
1-29
1-31
1-32
1-33
33-1
1-35
1-37
1-38
1-40
1-44
1-45
1-46
1-47
1-48
1-51
1-52
1-53
1-54
1-55
1-56
1-57
57-1
1-58
1-59
1-60
1-61
61-1
1-63
1-65
65-1
1-66
1-67
1-68
1-69
1-70
1-72
1-73
1-7...

output:

1 3
2 1
3 3
4 4
5 1
6 2
7 2
8 2
9 2
10 1
11 4
12 4
13 3
14 4
15 4
16 2
17 4
18 2
19 3
20 4
21 2
22 1
23 3
24 4
25 3
26 1
27 2
28 1
29 4
30 3
31 2
32 2
33 1
34 3
35 1
36 3
37 4
38 4
39 3
40 2
41 3
42 3
43 3
44 4
45 1
46 4
47 4
48 4
49 3
50 3
51 4
52 4
53 2
54 1
55 1
56 2
57 4
58 2
59 4
60 4
61 4
62 3...

result:

ok good solution

Test #5:

score: 0
Accepted
time: 0ms
memory: 3696kb

input:

100
1-7
1-10
1-18
1-22
1-27
1-57
1-60
1-65
1-71
1-78
1-80
1-82
1-85
1-86
86-1
1-95
1-96
2-3
2-5
2-10
2-13
2-16
16-2
2-25
2-30
2-31
2-35
2-42
42-2
2-67
2-71
2-72
2-73
2-80
2-81
2-85
2-92
2-94
3-4
3-5
3-20
20-3
3-21
3-28
3-29
29-3
3-34
3-43
3-51
3-58
58-3
3-59
3-61
3-63
63-3
3-66
3-69
69-3
3-75
3-79
3...

output:

1 3
2 4
3 1
4 3
5 3
6 3
7 4
8 1
9 4
10 1
11 3
12 3
13 1
14 1
15 4
16 1
17 4
18 2
19 1
20 2
21 3
22 2
23 4
24 3
25 3
26 4
27 1
28 2
29 2
30 2
31 3
32 2
33 3
34 2
35 1
36 2
37 2
38 3
39 1
40 1
41 3
42 2
43 4
44 1
45 2
46 4
47 4
48 1
49 3
50 4
51 3
52 3
53 4
54 1
55 2
56 1
57 4
58 4
59 2
60 2
61 4
62 1...

result:

ok good solution