Submission #1807804
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
using int64 = long long;
struct CentroidPathDecomposition
{
struct Centroid
{
int ParIndex, ParDepth, Deep;
vector< int > node;
Centroid(int idx, int dep, int deep) : ParIndex(idx), ParDepth(dep), Deep(deep) {}
inline size_t size()
{
return (node.size());
}
inline int &operator[](int k)
{
return (node[k]);
}
inline pair< int, int > Up()
{
return (make_pair(ParIndex, ParDepth));
}
};
vector< vector< int > > graph;
vector< int > SubTreeSize, NextPath;
vector< int > TreeIndex, TreeDepth;
vector< Centroid > Centroids;
void BuildSubTreeSize()
{
stack< pair< int, int > > s;
s.emplace(0, -1);
while(!s.empty()) {
auto p = s.top();
s.pop();
if(~SubTreeSize[p.first]) {
NextPath[p.first] = -1;
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
SubTreeSize[p.first] += SubTreeSize[to];
if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
NextPath[p.first] = to;
}
}
} else {
s.push(p);
SubTreeSize[p.first] = 1;
for(auto &to : graph[p.first]) {
if(p.second != to) s.emplace(to, p.first);
}
}
}
}
void BuildPath()
{
stack< pair< int, int > > s;
Centroids.emplace_back(-1, -1, 0);
s.emplace(0, -1);
TreeIndex[0] = 0;
while(!s.empty()) {
auto p = s.top();
s.pop();
TreeDepth[p.first] = (int) Centroids[TreeIndex[p.first]].size();
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
if(to == NextPath[p.first]) { // Centroid-Path
TreeIndex[to] = TreeIndex[p.first];
} else { // Not Centroid-Path
TreeIndex[to] = (int) Centroids.size();
Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1);
}
s.emplace(to, p.first);
}
Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
}
}
void AddEdge(int x, int y)
{
graph[x].push_back(y);
graph[y].push_back(x);
}
virtual void Build()
{
BuildSubTreeSize();
BuildPath();
}
inline size_t size()
{
return (Centroids.size());
}
inline pair< int, int > Information(int idx)
{
return (make_pair(TreeIndex[idx], TreeDepth[idx]));
}
inline Centroid &operator[](int k)
{
return (Centroids[k]);
}
inline int LCA(int a, int b)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
return (Centroids[TreeIdxA][TreeDepthA]);
}
inline virtual void query(int a, int b, const function< void(int, int, int) > &f)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(TreeIdxA, 0, TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(TreeIdxB, 0, TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
}
CentroidPathDecomposition(int SZ)
{
graph.resize(SZ);
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
};
struct TreeArray : CentroidPathDecomposition
{
TreeArray(int sz) : CentroidPathDecomposition(sz) {}
vector< int > index;
void Build()
{
CentroidPathDecomposition::Build();
int ptr = 0;
for(auto ¢roid : Centroids) {
index.emplace_back(ptr);
ptr += centroid.size();
}
}
inline int get(int a)
{
auto p = Information(a);
return (index[p.first] + p.second);
}
inline void query(int a, int b, const function< void(int, int) > &f)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(index[TreeIdxA] + TreeDepthA + 1, index[TreeIdxA] + TreeDepthB + 1);
}
};
struct SegNode
{
int v;
SegNode(int v) : v(v) {}
SegNode operator*(const SegNode &r) const
{
return (v > r.v ? *this : r);
}
} e(0);
struct SegmentTree
{
int sz;
vector< SegNode > seg;
SegmentTree(int n)
{
sz = 1;
while(sz < n) sz <<= 1;
seg.assign(2 * sz - 1, e);
}
void update(int k, const SegNode &x)
{
k += sz - 1;
seg[k] = x;
while(k > 0) {
k = (k - 1) >> 1;
seg[k] = seg[2 * k + 1] * seg[2 * k + 2];
}
}
SegNode query(int a, int b, int k, int l, int r)
{
if(a >= r || b <= l) return (e);
if(a <= l && r <= b) return (seg[k]);
return (query(a, b, 2 * k + 1, l, (l + r) >> 1) * query(a, b, 2 * k + 2, (l + r) >> 1, r));
}
SegNode query(int a, int b)
{
return (query(a, b, 0, 0, sz));
}
};
struct UnionFind
{
vector< int > data;
UnionFind(int sz)
{
data.assign(sz, -1);
}
bool unite(int x, int y)
{
x = find(x), y = find(y);
if(x == y) return (false);
if(data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k)
{
if(data[k] < 0) return (k);
return (data[k] = find(data[k]));
}
int size(int k)
{
return (-data[find(k)]);
}
};
using int64 = long long;
int main()
{
int N, M, Q;
scanf("%d %d", &N, &M);
vector< tuple< int, int, int > > vs;
for(int i = 0; i < M; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
--a, --b;
vs.emplace_back(c, a, b);
}
sort(begin(vs), end(vs));
UnionFind uf(N);
TreeArray tree(N);
int64 ret = 0;
map< pair< int, int >, int > conv;
for(auto &e : vs) {
int cost;
int a, b;
tie(cost, a, b) = e;
if(uf.unite(a, b)) {
tree.AddEdge(a, b);
conv[minmax(a, b)] = cost;
ret += cost;
}
}
tree.Build();
SegmentTree seg(N);
for(auto &p : conv) {
seg.update(max(tree.get(p.first.first), tree.get(p.first.second)), p.second);
}
scanf("%d", &Q);
while(Q--) {
int a, b;
scanf("%d %d", &a, &b);
int sub = 0;
tree.query(--a, --b, [&](int x, int y)
{
sub = max(sub, seg.query(x, y).v);
});
printf("%lld\n", ret - sub);
}
}
Submission Info
Submission Time |
|
Task |
A - Graph |
User |
ei13333 |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
7609 Byte |
Status |
WA |
Exec Time |
236 ms |
Memory |
8560 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:282:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
^
./Main.cpp:286:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &a, &b, &c);
^
./Main.cpp:311:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
^
./Main.cpp:314:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
^
Judge Result
Set Name |
Sample |
subtask1 |
subtask2 |
All |
Score / Max Score |
0 / 0 |
0 / 200 |
0 / 300 |
0 / 200 |
Status |
|
|
|
|
Set Name |
Test Cases |
Sample |
sample_1.txt, sample_2.txt |
subtask1 |
sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt |
subtask2 |
sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt |
All |
sample_1.txt, sample_2.txt, sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt, subtask_3_1.txt, subtask_3_2.txt, subtask_3_3.txt, subtask_3_4.txt, subtask_3_5.txt, subtask_3_6.txt, subtask_3_7.txt, subtask_3_8.txt |
Case Name |
Status |
Exec Time |
Memory |
sample_1.txt |
AC |
2 ms |
380 KB |
sample_2.txt |
AC |
1 ms |
256 KB |
subtask_1_1.txt |
AC |
2 ms |
256 KB |
subtask_1_10.txt |
WA |
71 ms |
3444 KB |
subtask_1_11.txt |
AC |
1 ms |
256 KB |
subtask_1_2.txt |
AC |
17 ms |
1400 KB |
subtask_1_3.txt |
WA |
138 ms |
6896 KB |
subtask_1_4.txt |
AC |
5 ms |
1024 KB |
subtask_1_5.txt |
AC |
6 ms |
1024 KB |
subtask_1_6.txt |
WA |
37 ms |
2164 KB |
subtask_1_7.txt |
AC |
138 ms |
6896 KB |
subtask_1_8.txt |
WA |
5 ms |
1024 KB |
subtask_1_9.txt |
AC |
7 ms |
1088 KB |
subtask_2_1.txt |
WA |
141 ms |
8304 KB |
subtask_2_2.txt |
WA |
142 ms |
7536 KB |
subtask_2_3.txt |
WA |
140 ms |
8176 KB |
subtask_2_4.txt |
WA |
140 ms |
8048 KB |
subtask_2_5.txt |
WA |
8 ms |
1024 KB |
subtask_2_6.txt |
WA |
14 ms |
1276 KB |
subtask_2_7.txt |
WA |
40 ms |
2164 KB |
subtask_2_8.txt |
WA |
140 ms |
8560 KB |
subtask_3_1.txt |
WA |
236 ms |
8044 KB |
subtask_3_2.txt |
WA |
236 ms |
7664 KB |
subtask_3_3.txt |
WA |
99 ms |
2432 KB |
subtask_3_4.txt |
WA |
103 ms |
2368 KB |
subtask_3_5.txt |
WA |
166 ms |
4464 KB |
subtask_3_6.txt |
WA |
202 ms |
7536 KB |
subtask_3_7.txt |
WA |
234 ms |
7920 KB |
subtask_3_8.txt |
WA |
231 ms |
7792 KB |