Problem G : Can I Find You
Description
Once by chance, Chris met a lovely girl Melinda. They had a very pleasant conversation. But Chris forgot Melinda's phone number. He only knows where Melinda lives. Chris wants to go to the village where Melinda lives and find her!
There are n villages totally. Chris is living in the village S, and Melinda is living in the village T. Some villages are connected by roads. There are m roads totally. Unfortunately, there are k roads that were destroyed not long ago. And Chris doesn't know which roads were destroyed.
He asks for your help to find whether could he arrive at the village T, no matter which roads were destroyed.
Input
There are several test cases.
For each test case, the first line contains 3 integers n, m and k (2 ≤ n ≤ 50, 0 ≤ m ≤ 200), denoting the number of villages, the number of roads and the number of destroyed roads.
The second line contains 2 integers S and T, where S is the village where Chris lives, and T is the village where Melinda lives.
The next m lines describe the roads between villages. Each line contains 2 numbers u and v (1 ≤ u ≤ n, 1 ≤ v ≤ n, u ≠ v), representing a bidirectional road between villages u and v. There is at most 1 road between any two villages.
Output
For each test case, if Chris can find Melinda, print "YES", otherwise print "NO".
Sample Input
5 5 1 1 3 1 4 1 2 2 3 3 4 4 5 5 6 2 1 3 1 4 1 2 2 3 3 4 4 5 1 3 6 8 3 2 3 2 6 2 3 3 6 2 4 3 4 4 5 5 6 1 2
Sample Output
YES YES NO
题意:有一些路断了,问最坏情况下,能不能从S去到T。
解题思路:最小割模板题。直接看看K是不是小于最小割就ok。最小割=最大流。注意是双向路。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 205005;
const int INF = 0x3f3f3f3f;
int N, M,K;
int S, T;
struct Edge
{
int v, next;
ll flow;
} e[MAXN << 1];
int head[MAXN], edge_num, layer[MAXN];
void addedge(int u, int v, ll w)
{
e[edge_num].v = v;
e[edge_num].flow = w;
e[edge_num].next = head[u];
head[u] = edge_num++;
e[edge_num].v = u;
e[edge_num].flow = 0;
e[edge_num].next = head[v];
head[v] = edge_num++;
}
bool bfs(int start, int End)
{
queue<int> Q;
Q.push(start);
memset(layer, 0, sizeof(layer));
layer[start] = 1;
while (Q.size())
{
int u = Q.front();
Q.pop();
if (u == End)
return true;
for (int j = head[u]; j != -1; j = e[j].next)
{
int v = e[j].v;
if (layer[v] == false && e[j].flow)
{
layer[v] = layer[u] + 1;
Q.push(v);
}
}
}
return false;
}
ll dfs(int u, ll MaxFlow, int End)
{
if (u == End)
return MaxFlow;
ll uflow = 0;
for (int j = head[u]; j != -1; j = e[j].next)
{
int v = e[j].v;
if (layer[v] - 1 == layer[u] && e[j].flow)
{
ll flow = min(MaxFlow - uflow, e[j].flow);
flow = dfs(v, flow, End);
e[j].flow -= flow;
e[j ^ 1].flow += flow;
uflow += flow;
if (uflow == MaxFlow)
break;
}
}
if (uflow == 0)
layer[u] = 0;
return uflow;
}
ll dinic(int start, int End)
{
ll MaxFlow = 0;
while (bfs(start, End))
MaxFlow += dfs(start, INF, End);
return MaxFlow;
}
int main()
{
while (~scanf("%d%d%d",&N,&M,&K))
{
memset(head, -1, sizeof(head));
edge_num = 0;
scanf("%d%d", &S, &T);
int u,v;
for(int i=0;i<M;i++){
scanf("%d%d",&u,&v);
addedge(u,v,1);
addedge(v,u,1);
}
if(K<dinic(S, T))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}