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
| /*
ID:du331691
PROG:telecow
LANG:C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
/*拆点*/
const int maxn=103;
const int maxm=603*2*2+maxn*2;
int n,m,s,t;
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}
template<typename T>
T Min(const T& a,const T& b){return a<b?a:b;}
const int inf=2147483647;
struct EDGE
{
int to,nxt,w,tw;
}edge[maxm];
int ek=0,node[maxn*2],cur[maxn*2];//x+n为出点
int to[maxm],tk=0;
bool del[maxm];
void addEdge(int from,int too,int ww)
{
++ek;
edge[ek].to=too;
edge[ek].w=edge[ek].tw=ww;
edge[ek].nxt=node[from];
node[from]=ek;
}
void input()
{
scanf("%d%d%d%d",&n,&m,&s,&t);
int x,y;
for(int i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
addEdge(x+n,y,inf);
addEdge(y,x+n,0);
addEdge(y+n,x,inf);
addEdge(x,y+n,0);
}
for(int i=1;i<=n;++i)
{
if(i==t||i==s)
addEdge(i,i+n,inf);
else addEdge(i,i+n,1);
if(i!=s&&i!=t)to[++tk]=ek;
addEdge(i+n,i,0);
}
m=m*2+n;
}
bool cmp(int a,int b)
{
return edge[a].w>edge[b].w;
}
int d[2*maxn];
template<typename T>
struct QUE
{
T ary[2*maxn];
int f,t;
QUE(){f=t=1;}
void clear(){f=t=1;}
void push(const T& w){ary[t++]=w;}
void pop(){++f;}
bool empty(){return f==t;}
T front(){return ary[f];}
};
QUE<int> q;
bool bfs()
{
memset(d,0,sizeof(d));
q.clear();
d[s]=1;
q.push(s);
while(!q.empty())
{
int ff=q.front();q.pop();
for(int i=node[ff];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(edge[i].w&&(!d[v])&&!del[i])
{
d[v]=d[ff]+1;
q.push(v);
}
}
}
return d[t];
}
int dfs(int k,int flow)//当前点,当前最大流量
{
if(k==t)
return flow;
int used=0;//当前已用流量
for(int i=cur[k];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(d[v]==d[k]+1&&!del[i])
{
int tmp=dfs(v,Min(flow-used,edge[i].w));//这条边往下最大流
edge[i].w-=tmp;
edge[i&1?i+1:i-1].w+=tmp;//反向边
used+=tmp;
if(edge[i].w)
cur[k]=i;//标记,这条边以前的边在这次都流满了,这条边还有可能压榨
if(used==flow)//已经流满
return flow;
}
}
if(!used)//这条边流不到汇点
d[k]=0;
return used;
}
int dinic()
{
int ans=0;
while(bfs())
{
memcpy(cur+1,node+1,n*2*sizeof(int));
//memcpy(cur,node,sizeof(node));
ans+=dfs(s,inf);
}
return ans;
}
void recover()
{
for(int i=1;i<=m*2;++i)
{
if(!del[i])
edge[i].w=edge[i].tw;
}
}
int maxflow;
int edgedel[maxm],edk=0;
void solve()
{
sort(to+1,to+tk+1);
maxflow=dinic();
//printf("%d ",maxflow);
for(int i=1;i<=tk&&maxflow;++i)
{
recover();
del[to[i]]=true;
del[to[i]&1?to[i]+1:to[i]-1]=true;
int tflow=dinic();
if(maxflow-tflow==edge[to[i]].tw)
{
edgedel[++edk]=edge[to[i]].to-n;
//cout<<edge[to[i]].tw<<endl;
maxflow=tflow;
}
else del[to[i]]=false,del[to[i]&1?to[i]+1:to[i]-1]=false;
}
}
void output()
{
sort(edgedel+1,edgedel+edk+1);
printf("%d\n",edk);
for(int i=1;i<=edk;++i)
{
printf("%d",edgedel[i]);
if(i!=edk)putchar(' ');
else putchar('\n');
}
}
int main()
{
freopen("telecow.in","r",stdin);
freopen("telecow.out","w",stdout);
input();
solve();
output();
return 0;
}
|