forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharborescence.cpp
227 lines (196 loc) · 4.15 KB
/
arborescence.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
//Without merge technique
struct UnionFind{
int n;
vector<int> r,p;
UnionFind(){}
UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}
int find(int x){
return (x==p[x]?x:p[x]=find(p[x]));
}
bool same(int x,int y){
return find(x)==find(y);
}
void unite(int x,int y){
x=find(x);y=find(y);
if(x==y) return;
r[x]+=r[y];
p[y]=x;
}
};
template<typename T, typename E>
struct SkewHeap{
using G = function<T(T,E)>;
using H = function<E(E,E)>;
using C = function<bool(T,T)>;
G g;
H h;
C c;
T INF;
E ei;
SkewHeap(G g,H h,C c,T INF,E ei):g(g),h(h),c(c),INF(INF),ei(ei){}
struct Node{
Node *l,*r;
T val;
E add;
Node(T val,E add):val(val),add(add){l=r=nullptr;}
};
void eval(Node *a){
if(a==nullptr) return;
if(a->add==ei) return;
if(a->l) a->l->add=h(a->l->add,a->add);
if(a->r) a->r->add=h(a->r->add,a->add);
a->val=g(a->val,a->add);
a->add=ei;
}
T top(Node *a){
return a!=nullptr?g(a->val,a->add):INF;
}
T snd(Node *a){
eval(a);
return a!=nullptr?min(top(a->l),top(a->r)):INF;
}
Node* add(Node *a,E d){
if(a!=nullptr) a->add=h(a->add,d);
return a;
}
Node* push(T v){
return new Node(v,ei);
}
Node* meld(Node *a,Node *b){
if(a==nullptr) return b;
if(b==nullptr) return a;
if(c(top(a),top(b))) swap(a,b);
eval(a);
a->r=meld(a->r,b);
swap(a->l,a->r);
return a;
}
Node* pop(Node* a){
eval(a);
auto res=meld(a->l,a->r);
delete a;
return res;
}
};
//INSERT ABOVE HERE
template<typename T>
struct Arborescence{
typedef pair<T, int> P;
using Heap = SkewHeap<P, T>;
struct edge{
int from,to;
T cost;
edge(){}
edge(int from,int to,T cost):from(from),to(to),cost(cost){}
};
int n;
P INF;
UnionFind uf;
vector<edge> edges;
vector<typename Heap::Node*> come;
vector<int> used,from;
vector<T> cost;
Arborescence(int n,T INF):n(n),INF(INF,-1),uf(n),come(n,NULL),
used(n,0),from(n,-1),cost(n,-1){};
void add_edge(int from,int to,int cost){
edges.emplace_back(from,to,cost);
}
void input(int m,int offset=0){
for(int i=0;i<m;i++){
int u,v;
T c;
cin>>u>>v>>c;
add_edge(u+offset,v+offset,c);
}
}
T build(int r){
typename Heap::G g=[](P a,T b){return P(a.first+b,a.second);};
typename Heap::H h=[](T a,T b){return a+b;};
typename Heap::C c=[](P a,P b){return a>b;};
Heap heap(g,h,c,INF,0);
used[r]=2;
for(int i=0;i<(int)edges.size();i++){
edge &e=edges[i];
come[e.to]=heap.meld(come[e.to],heap.push(P(e.cost,i)));
}
T res=0;
for(int i=0;i<n;i++){
if(used[i]) continue;
int v=i;
vector<int> l;
while(used[v]!=2){
used[v]=1;
l.emplace_back(v);
if(!come[v]) return T(-1);
from[v]=uf.find(edges[come[v]->val.second].from);
cost[v]=heap.top(come[v]).first;
come[v]=heap.pop(come[v]);
if(from[v]==v) continue;
res+=cost[v];
if(used[from[v]]==1){
int p=v;
do{
if(come[p]!=nullptr) heap.add(come[p],-cost[p]);
if(p!=v){
uf.unite(v,p);
come[v]=heap.meld(come[v],come[p]);
}
p=uf.find(from[p]);
}while(p!=v);
}else{
v=from[v];
}
}
for(int u:l) used[u]=2;
}
return res;
}
};
//END CUT HERE
struct AOJ_GRL_2B{
signed solve(){
int n,m,r;
cin>>n>>m>>r;
const int INF = 1e8;
Arborescence<int> G(n,INF);
G.input(m);
cout<<G.build(r)<<endl;
return 0;
}
};
/*
verified on 2018/03/04
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_B&lang=jp
*/
struct UVA11183{
signed solve(){
Int T;
cin>>T;
for(Int t=1;t<=T;t++){
Int n,m;
cin>>n>>m;
const Int INF = 1e15;
Arborescence<Int> G(n,INF);
G.input(m);
Int ans=G.build(0);
cout<<"Case #"<<t<<": ";
if(ans<0) cout<<"Possums!"<<endl;
else cout<<ans<<endl;
}
return 0;
}
};
/*
verified on 2018/03/04
https://vjudge.net/problem/UVA-11183
*/
signed main(){
AOJ_GRL_2B ans;
//UVA11183 ans;
ans.solve();
return 0;
}