USACO 2019 JAN Platinum 2 -- Exercise Route

摘要

选两条非树边,和树边形成简单环? Pickupwin 表示并不会做。

题面

官方题解

合法的两个非树边“覆盖”的路径必须有交,这也是充分条件。

那就是给定若干路径求有交的路径对数

把路径在LCA处折断,之后对于每个点考虑它到根这条链上的情况

一条链还是很好处理的

之后想个办法把折断的影响扣掉就好了

Pickupwin: 好难写啊,我自闭了

Code

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
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>

using std::map;
using std::vector;
using std::sort;
using std::swap;

const int MAXN=200011;

int N, M;

long long Ans=0LL;

struct Pair{
int a, b;
};

inline bool operator < (const Pair &A, const Pair &B){
return (A.a==B.a)?(A.b<B.b):(A.a<B.a);
}

inline bool operator != (const Pair &A, const Pair &B){
return A.a!=B.a || A.b!=B.b;
}

struct Vert{
int FE;
int Dep, Pre[19];
map<int, int> Map;
vector<Pair> Vis;
vector<int> Up;
} V[MAXN];

struct Edge{
int y, next;
} E[MAXN<<1];

int Ecnt;

void addE(int a, int b){
++Ecnt;
E[Ecnt].y=b;E[Ecnt].next=V[a].FE;V[a].FE=Ecnt;
}

void DFS(int at){
for(int k=V[at].FE, to;k;k=E[k].next){
to=E[k].y;
if(to==V[at].Pre[0]) continue;
V[to].Dep=V[at].Dep+1;
V[to].Pre[0]=at;
DFS(to);
}
}

int LCA(int a, int b){
if(V[a].Dep<V[b].Dep) swap(a, b);
for(int i=18;i>=0;--i)
if(V[V[a].Pre[i]].Dep>=V[b].Dep)
a=V[a].Pre[i];
if(a!=b){
for(int i=18;i>=0;--i)
if(V[a].Pre[i]!=V[b].Pre[i])
{a=V[a].Pre[i];b=V[b].Pre[i];}
a=V[a].Pre[0];b=V[b].Pre[0];
}
return a|b;
}

void Jump(int &a, const int &f){
for(int i=18;i>=0;--i)
if(V[V[a].Pre[i]].Dep>V[f].Dep)
a=V[a].Pre[i];
}

int Sum[MAXN];

long long C2(int n){
return 1LL*n*(n-1)/2LL;
}

void DFS_(int at){
for(int d:V[at].Up){
//printf("\t%d %d %d\n", at, d, Sum[V[at].Dep-1]-Sum[d]);
Ans+=Sum[V[at].Dep-1]-Sum[d];
}
for(int k=V[at].FE, to;k;k=E[k].next){
to=E[k].y;
if(to==V[at].Pre[0]) continue;
Sum[V[at].Dep]=Sum[V[at].Dep-1]+V[at].Map[to];
Ans+=C2(V[at].Map[to]);
DFS_(to);
}
}

void Push(int a, int b){
if(a==b) return;
V[b].Up.push_back(V[a].Dep);
}

void Deal(int a, int b){
int c=LCA(a, b);
Push(c, a);Push(c, b);
Jump(a, c);Jump(b, c);
++V[c].Map[a];++V[c].Map[b];
if(a>b) swap(a, b);
V[c].Vis.push_back((Pair){a, b});
//printf("\t\t %d %d %d\n", c, a, b);
}

long long Calc(vector<Pair> &V, const int &p){
long long ret=0LL;
sort(V.begin(), V.end());
for(int i=0, s=V.size(), c=0;i<s;++i){
++c;
if(i+1==s || V[i+1]!=V[i]){
//printf("%d %d\n", V[i].a, V[i].b);
if(V[i].a!=p && V[i].b!=p) ret+=C2(c);//printf("!!\t%d\n", c);
c=0;
}
}
return ret;
}

int main(){

freopen("exercise.in", "r", stdin);
freopen("exercise.out", "w", stdout);

scanf("%d%d", &N, &M);

for(int i=1, a, b;i<N;++i){
scanf("%d%d", &a, &b);
addE(a, b);addE(b, a);
}

V[1].Dep=1;
DFS(1);

for(int i=1;i<19;++i)
for(int j=1;j<=N;++j)
V[j].Pre[i]=V[V[j].Pre[i-1]].Pre[i-1];

for(int i=N, a, b;i<=M;++i){
scanf("%d%d", &a, &b);
Deal(a, b);
}

DFS_(1);

for(int i=1;i<=N;++i) Ans-=Calc(V[i].Vis, i);

printf("%lld\n", Ans);

return 0;
}