-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1244 D.cpp
More file actions
132 lines (110 loc) · 1.97 KB
/
1244 D.cpp
File metadata and controls
132 lines (110 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair <int, int>
#define pll pair <long long, long long>
using namespace std;
ll sum, ans = 1e17;
int n, a, b, start;
int c[100009][5], vis[100009], ex[4], used[4];
vector <int> G[100009], x;
vector <pii> ans2;
void DFS(int v)
{
x.push_back(v);
vis[v] = 1;
for(int i = 0; i < G[v].size(); i++)
{
if(vis[G[v][i]] == 0)
{
DFS(G[v][i]);
}
}
}
void check()
{
sum = 0;
for(int i = 0; i < x.size(); i++)
{
int m = i % 3;
m = ex[m];
sum += c[x[i]][m];
}
if(sum < ans)
{
ans = sum;
used[0] = ex[0];
used[1] = ex[1];
used[2] = ex[2];
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for(int i = 1; i <= 3; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> c[j][i];
}
}
for(int i = 0; i < n - 1; i++)
{
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
for(int i = 1; i <= n; i++)
{
if(G[i].size() > 2)
{
cout << -1 << "\n";
return 0;
}
if(G[i].size() == 1)
{
start = i;
}
}
DFS(start);
ex[0] = 1;
ex[1] = 2;
ex[2] = 3;
check();
ex[0] = 1;
ex[1] = 3;
ex[2] = 2;
check();
ex[0] = 2;
ex[1] = 1;
ex[2] = 3;
check();
ex[0] = 2;
ex[1] = 3;
ex[2] = 1;
check();
ex[0] = 3;
ex[1] = 2;
ex[2] = 1;
check();
ex[0] = 3;
ex[1] = 1;
ex[2] = 2;
check();
cout << ans << "\n";
for(int i = 0; i < x.size(); i++)
{
int m = i % 3;
m = used[m];
ans2.push_back({ x[i], m});
}
sort(ans2.begin(), ans2.end());
for(int i = 0; i < ans2.size(); i++)
{
cout << ans2[i].second << " ";
}
cout << "\n";
}