-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathCentroid Decomposition Sample.cpp
157 lines (132 loc) · 2.33 KB
/
Centroid Decomposition Sample.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* You are given a tree consisting of n vertices. A number is written on each vertex;
the number on vertex i is equal to a[i].
Let, g(x,y) is the gcd of the numbers written on the vertices belonging to the path from
x to y, inclusive. For i in 1 to 200000, count number of pairs (x,y) (1<=x<=y) such
that g(x,y) equals to i.
Note that 1<=x<=y does not really matter.
*/
vi graph[MAX];
int n, a[MAX], sub[MAX], total, cnt[MAX], cent, upto[MAX];
ll ans[MAX];
bool done[MAX];
set<int> take[MAX];
void dfs(int u, int p)
{
sub[u] = 1;
total++;
for (auto v : graph[u])
{
if (v == p || done[v]) continue;
dfs(v, u);
sub[u] += sub[v];
}
}
int getCentroid(int u, int p)
{
// cout<<u<<" "<<sub[u]<<endl;
for (auto v : graph[u])
{
if (!done[v] && v != p && sub[v] > total / 2)
return getCentroid(v, u);
}
return u;
}
void go(int u, int p, int val)
{
ans[val]++;
take[cent].insert(val);
cnt[val]++;
for (auto v : graph[u])
{
if (!done[v] && v != p)
{
go(v, u, upto[v]);
}
}
}
void calc(int u, int p, int val)
{
for (auto it : take[cent])
{
int g = gcd(val, it);
ans[g] += cnt[it];
}
for (auto v : graph[u])
{
if (!done[v] && v != p)
{
calc(v, u, upto[v]);
}
}
}
void clean(int u, int p, int val)
{
cnt[val] = 0;
for (auto v : graph[u])
{
if (!done[v] && v != p)
{
clean(v, u, upto[v]);
}
}
}
void calcgcd(int u, int p, int val)
{
upto[u] = val;
for (auto v : graph[u])
{
if (!done[v] && v != p)
{
calcgcd(v, u, gcd(val, a[v]));
}
}
}
void solve(int u)
{
total = 0;
dfs(u, -1);
cent = getCentroid(u, -1);
calcgcd(cent, -1, a[cent]);
// debug("cent",cent);
done[cent] = true;
for (auto v : graph[cent])
{
if (done[v]) continue;
// cout<<"from centroid "<<cent<<" going to node: "<<v<<endl;
calc(v, cent, upto[v]);
go(v, cent, upto[v]);
}
for (auto v : graph[cent])
{
if (!done[v])
clean(v, cent, upto[v]);
}
for (auto v : graph[cent])
{
if (!done[v])
solve(v);
}
}
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt","r",stdin);
int test, cases = 1;
scanf("%d", &n);
FOR(i, 1, n + 1)
{
scanf("%d", &a[i]);
ans[a[i]]++;
}
int u, v;
FOR(i, 1, n)
{
scanf("%d%d", &u, &v);
graph[u].pb(v);
graph[v].pb(u);
}
solve(1);
FOR(i, 1, MAX) if (ans[i]) printf("%d %lld\n", i, ans[i]);
return 0;
}