封锁阳光大学
Time Limits: 3000 ms Memory Limits: 65536 KB
Description
曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街。河蟹看到欢快的曹,感到不爽。河蟹决定封锁阳光大学,不让曹刷街。
阳光大学的校园是一张由N个点构成的无向图,N个点之间由M条道路连接。每只河蟹可以对一个点进行封锁,当某个点被封锁后,与这个点相连的道路就被封锁了,曹就无法在与这些道路上刷街了。非常悲剧的一点是,河蟹是一种不和谐的生物,当两只河蟹封锁了相邻的两个点时,他们会发生冲突。
询问:最少需要多少只河蟹,可以封锁所有道路并且不发生冲突。
Input
第一行:两个整数N,M
接下来M行:每行两个整数A,B,表示点A到点B之间有道路相连。
数据保证:
1<=N<=10000
1<=M<=100000
任意两点之间最多有一条道路。
Output
仅一行:如果河蟹无法封锁所有道路,则输出“Impossible”,否则输出一个整数,表示最少需要多少只河蟹。
Sample Input
Input1:
3 3
1 2
1 3
2 3
Input2:
3 2
1 2
2 3
Sample Output
Output1:
Impossible
Output2:
1
题目大意
这道题的意思是说,给出有n个点,编号为1~n,有m条路,每条路连接编号为Ai和编号为Bi的点,但这n个点不一定两两连通。
现在要你扔出一些河蟹去占领这m条路,如果第i条路Ai或Bi被占领,则成第i条路被占领,但是点Ai和点Bi不能同时被河蟹占领。
如果不能占领这m条路,就输出“Impossible”,否则输出最少扔出的河蟹数量。
解题思路
直接找每个连通块,对其中一点染成白色(赋值为0),再从这个点宽搜,往后每一个搜到的点就是上一个搜到的点相反的颜色,如果有矛盾,就输出“Impossible”,那么这一连通块最少用的河蟹数就是白色点数和黑色点数的最小值。
每一连通块最少用的河蟹数之和便为答案。
Codes
var
r:array[1..200000,1..3]of longint;
f,first,last:array[1..10000]of longint;
data:array[1..10000,1..2]of integer;
bz:array[1..10000]of shortint;
s:array[0..1]of integer;
n,m,i,x,y,ans:longint;
procedure insert(w,x,y:longint);//加入边集数组
begin
r[w,1]:=x;r[w,2]:=y;r[w,3]:=w;
if first[x]=0 then first[x]:=w else r[last[x],3]:=w;
last[x]:=w;
end;
procedure Build(w:longint);//给点染色
var h,t,o,x,now,k:longint;
begin
data[1,1]:=w;data[1,2]:=0;
h:=0;t:=1;bz[w]:=0;
while h<t do
begin
inc(h);
k:=data[h,2];now:=data[h,1];
inc(s[k]);o:=first[now];//记录黑白两色点的数目
while 1=1 do
begin
x:=r[o,2];
if bz[x]<>-1 then
begin
if bz[x]<>1-k then
begin writeln('Impossible');halt;end;//有矛盾的情况
if o=r[o,3] then break;
o:=r[o,3];continue;
end;
inc(t);
data[t,1]:=x;data[t,2]:=1-k;bz[x]:=1-k;
if o=r[o,3] then break;
o:=r[o,3];
end;
end;
end;
{Main}
begin
assign(input,'close.in'); reset(input);
assign(output,'close.out'); rewrite(output);
read(n,m);
for i:=1 to m do
begin read(x,y);insert(i,x,y);insert(i+m,y,x);end;
fillchar(bz,sizeof(bz),$ff);
for i:=1 to n do
if bz[i]<0then
if first[i]<>0 then//找到了一个新的连通块
begin
s[1]:=0;s[0]:=0;
Build(i);
if s[1]>s[0] then ans:=ans+s[0] else ans:=ans+s[1];
end;
writeln(ans);
end.