题解 CF1277E【Two Fairs】
Rainy_chen · · 个人记录
题目大意
给出一张无向图以及两个点
同时若
吐槽
虽然确实挺有意思,但是这么简单的题,出到Div2E我觉得有点不合适吧QAQ。
另外这一篇题解中您可能会大量看见某些词的重复使用。
题解
我们从
之后从
此时我们应当有三类点:只被
答案是第一类点的数量乘上第二类点的数量。
证明
我们考虑一对合法的点
之后我们思考一下
实际上这意味着
实际上
之后我们想证明什么? 我们想证明任何一条从
我们讨论一下一条从
实际上我们只需要考虑一下是否可以存在不同时经过
具体来说,我们假设存在一条路径使得其不经过
- 从
v 能不经过b 到达u 。 -
- 从
v 能不经过b 到达a
于是我们发现此时出现了矛盾,第二类点实际上是不能通过
证明不存在一条路径使其不经过
于是我们就证明了至少有这么多点对是可行的。
之后我们还需要证明其他的点对是不可行的。。
实际上这里的证明和上面的证明也是相似的所以略过。
然后就把这个做法的正确性证明出来了~
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long int_t;
char getch() {
static char buf[100000], *s, *t;
return (s == t) && (t = (s = buf) + fread(buf, 1, 100000, stdin)), s == t ? EOF : *s++;
}
#ifdef local
#define getch getchar
#endif
int_t read() {
int_t x = 0, w = 1; char ch = 0;
while(!isdigit(ch)) { ch = getch() ;if(ch == '-') w = -1;}
while(isdigit(ch)) x = x * 10 + ch - '0', ch = getch();
return x * w;
}
vector<int_t> G[200010];
int_t color[200010];
void bfs(int_t a, int_t b, int_t col) {
queue<int_t> que;
que.push(a);
while(!que.empty()) {
int_t rt = que.front();que.pop();
color[rt] |= col;
if(rt != b) for(int_t to : G[rt]) if(!(color[to] & col)) que.push(to);
}
}
int main() {
int_t T = read();
while(T--) {
int_t n = read(), m = read(), a = read(), b = read();
for(int_t i=1;i<=n;i++) G[i].clear(), color[i] = 0;
while(m--) {
int_t u = read(), v = read();
G[u].push_back(v); G[v].push_back(u);
}
bfs(a, b, 1); bfs(b, a, 2);
int_t ans1 = 0, ans2 = 0;
for(int_t i=1;i<=n;i++) if(color[i] == 1) ans1++; else if(color[i] == 2) ans2++;
cout<<ans1 * ans2<<endl;
}
}